Tôi hiện đang cố gắng thực hiện thử thách này trong đó nó mang lại cho tôi một chuỗi, chẳng hạn như 'Python'
và tôi cần xóa mọi ký tự thứ ba hoặc mọi ký tự mà chỉ số chia hết cho 3. Trong ví dụ này, đầu ra sẽ là ____77. Cho đến nay, tôi có phần word[::3]
để tìm mỗi ký tự thứ 3, nhưng làm thế nào tôi có thể lấy từng nhân vật này và loại bỏ chúng khỏi chuỗi?
Mã số
word = str[input[]]
newWord = word[::3]
print[newWord] #for testing
Khi được hỏi ngày 24 tháng 5 năm 2018 lúc 2:04May 24, 2018 at 2:04
2
Một chuỗi đầu vào là bất biến, nhưng chuyển đổi nó thành một danh sách và bạn có thể chỉnh sửa nó:
>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
Bắt đầu từ một nhân vật khác:
>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
Đã trả lời ngày 24 tháng 5 năm 2018 lúc 2:14May 24, 2018 at 2:14
Mark Tolonenmark TolonenMark Tolonen
157K24 Huy hiệu vàng163 Huy hiệu bạc238 Huy hiệu Đồng24 gold badges163 silver badges238 bronze badges
Kiểm tra điều này:
>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
Đã trả lời ngày 24 tháng 5 năm 2018 lúc 2:12May 24, 2018 at 2:12
mshsayemmshsayemmshsayem
17.2k11 Huy hiệu vàng61 Huy hiệu bạc67 Huy hiệu đồng11 gold badges61 silver badges67 bronze badges
Đây là câu trả lời của tôi:
s = '0123456'
print s[::3]
# 036
# first way[understandable] create a new str
s_new = ''
for i in xrange[len[s]]:
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate[s]]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join[s_lst]
print s_new
# 1245
# you can put it in single line
s_new = ''.join[[c if i % 3 else '' for i, c in enumerate[s]]]
print s_new
# 1245
# third way
s_idx = filter[lambda x: x[0] % 3, enumerate[s]]
print s_idx
# [[1, '1'], [2, '2'], [4, '4'], [5, '5']]
print ''.join[[x[1] for x in s_idx]]
# 1245
Đã trả lời ngày 24 tháng 5 năm 2018 lúc 2:24May 24, 2018 at 2:24
JayhellojayhelloJayhello
5.3553 Huy hiệu vàng46 Huy hiệu bạc54 Huy hiệu đồng3 gold badges46 silver badges54 bronze badges
Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để thay thế mọi ký tự thứ n trong một chuỗi bằng giá trị đã cho K.
Examples:
Đầu vào: test_str = Hồi GeekSforGeek là tốt nhất cho tất cả các geek, k = ‘$, n = 5test_str = “geeksforgeeks is best for all geeks”, K = ‘$’, N = 5
Đầu ra: Geek $ orge $ ks i $ bes $ cho $ tất cả $ eeks geeks$orge$ks i$ bes$ for$all $eeks
Giải thích: Mỗi ký tự thứ 5 được chuyển đổi thành $. Every 5th character is converted to $.
Đầu vào: test_str = Hồi GeekSforGeek là tốt nhất cho tất cả các geek, k = ‘*, n = 5: test_str = “geeksforgeeks is best for all geeks”, K = ‘*’, N = 5
Đầu ra: Geek*orge*ks i*bes*for*all*eeks: geeks*orge*ks i* bes* for*all *eeks
Giải thích: Mỗi lần xuất hiện thứ 5 được chuyển đổi thành *.Every 5th occurrence is converted to *.
Phương pháp 1: Sử dụng vòng lặp và liệt kê []Using loop and enumerate[]
Trong đó, chúng tôi thực hiện một lần lặp của từng ký tự và kiểm tra xem thứ n của nó bằng cách thực hiện modulo, tức là tìm phần còn lại của N. Nếu xảy ra thứ n của nó, ký tự được thay thế bởi K.
Thí dụ
Python3
test_str
____10
>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
2>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
4 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
6>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
7>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
8>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
0>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
3>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
7 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
31>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
9 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
33>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
9>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
36>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
37__Các
>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
2>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3'Python'
2 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
6>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
70Đầu ra
>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
2The original string is : geeksforgeeks is best for all geeks String after replacement : geeks$orge$ks i$ bes$ for$all $eeks2
s = '0123456'
print s[::3]
# 036
# first way[understandable] create a new str
s_new = ''
for i in xrange[len[s]]:
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate[s]]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join[s_lst]
print s_new
# 1245
# you can put it in single line
s_new = ''.join[[c if i % 3 else '' for i, c in enumerate[s]]]
print s_new
# 1245
# third way
s_idx = filter[lambda x: x[0] % 3, enumerate[s]]
print s_idx
# [[1, '1'], [2, '2'], [4, '4'], [5, '5']]
print ''.join[[x[1] for x in s_idx]]
# 1245
4Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để thay thế mọi ký tự thứ n trong một chuỗi bằng giá trị đã cho K.
Đầu vào: test_str = Hồi GeekSforGeek là tốt nhất cho tất cả các geek, k = ‘$, n = 5
Output:
Đầu ra: Geek $ orge $ ks i $ bes $ cho $ tất cả $ eeks
Chuỗi sau khi thay thế: Geek $ orge $ ks i $ bes $ cho $ tất cả $ eeks
Giải thích: Mỗi ký tự thứ 5 được chuyển đổi thành $.Using generator expression, join[] and enumerate[]
Đầu vào: test_str = Hồi GeekSforGeek là tốt nhất cho tất cả các geek, k = ‘*, n = 5
Thí dụ
Python3
test_str
____10
>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
2>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
4 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
6>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
7>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
8>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
0>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
3>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
7 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
31>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
9 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
33>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
9>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
36>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
37__Các
Đầu vào: test_str = Hồi GeekSforGeek là tốt nhất cho tất cả các geek, k = ‘$, n = 5
Output:
Đầu ra: Geek $ orge $ ks i $ bes $ cho $ tất cả $ eeks
Chuỗi sau khi thay thế: Geek $ orge $ ks i $ bes $ cho $ tất cả $ eeks
Độ phức tạp về thời gian và không gian của tất cả các phương pháp là như nhau:
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [n]O[n]
Phương pháp 3: Sử dụng danh sách
Python3
test_str
____10
>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
2>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
4 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
6>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
7>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
8>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
0>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
1>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0 >>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
3>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
23>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
25>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
26>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
27>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
29>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
7 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
31>>> word = list[input[]]
>>> del word[2::3]
>>> ''.join[word]
'12121212'
9 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
33>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
9>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
36>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
37__Các
s = '0123456'
print s[::3]
# 036
# first way[understandable] create a new str
s_new = ''
for i in xrange[len[s]]:
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate[s]]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join[s_lst]
print s_new
# 1245
# you can put it in single line
s_new = ''.join[[c if i % 3 else '' for i, c in enumerate[s]]]
print s_new
# 1245
# third way
s_idx = filter[lambda x: x[0] % 3, enumerate[s]]
print s_idx
# [[1, '1'], [2, '2'], [4, '4'], [5, '5']]
print ''.join[[x[1] for x in s_idx]]
# 1245
5>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
27>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0The original string is : geeksforgeeks is best for all geeks String after replacement : geeks$orge$ks i$ bes$ for$all $eeks0
>>> word = 'Python For All'
>>> new_word = ''.join[character for index, character in enumerate[word] if index%3 != 0]
>>> new_word
'ytonFo Al'
2The original string is : geeksforgeeks is best for all geeks String after replacement : geeks$orge$ks i$ bes$ for$all $eeks2
s = '0123456'
print s[::3]
# 036
# first way[understandable] create a new str
s_new = ''
for i in xrange[len[s]]:
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate[s]]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join[s_lst]
print s_new
# 1245
# you can put it in single line
s_new = ''.join[[c if i % 3 else '' for i, c in enumerate[s]]]
print s_new
# 1245
# third way
s_idx = filter[lambda x: x[0] % 3, enumerate[s]]
print s_idx
# [[1, '1'], [2, '2'], [4, '4'], [5, '5']]
print ''.join[[x[1] for x in s_idx]]
# 1245
4s = '0123456'
print s[::3]
# 036
# first way[understandable] create a new str
s_new = ''
for i in xrange[len[s]]:
if i % 3 != 0:
s_new += s[i]
print s_new
# 1245
# second way
s_lst = [c if i % 3 else '' for i, c in enumerate[s]]
print s_lst
# ['', '1', '2', '', '4', '5', '']
s_new = ''.join[s_lst]
print s_new
# 1245
# you can put it in single line
s_new = ''.join[[c if i % 3 else '' for i, c in enumerate[s]]]
print s_new
# 1245
# third way
s_idx = filter[lambda x: x[0] % 3, enumerate[s]]
print s_idx
# [[1, '1'], [2, '2'], [4, '4'], [5, '5']]
print ''.join[[x[1] for x in s_idx]]
# 1245
5>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
27>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
0>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
64>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
2>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
3'Python'
2 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
5 >>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
6>>> word = list[input[]] # Read in a word
abcdefghijklmnop
>>> del word[::3] # delete every third character
>>> ''.join[word] # join the characters together for the result
'bcefhiklno'
70Đầu ra
The original string is : geeksforgeeks is best for all geeks String after replacement : geeks$orge$ks i$ bes$ for$all $eeks