Cách đúng đắn để làm điều này là chỉ cắt chuỗi, như trong các câu trả lời khác.
Nhưng nếu bạn muốn một cách ngắn gọn hơn để viết mã của mình, điều này sẽ hoạt động cho các vấn đề tương tự không đơn giản như cắt lát, có hai thủ thuật: toàn diện và hàm enumerate
.
Đầu tiên, vòng lặp này:
for i in range[len[foo]]:
value = foo[i]
something with value and i
… có thể được viết như:
for i, value in enumerate[foo]:
something with value and i
Vì vậy, trong trường hợp của bạn:
for i, c in enumerate[s]:
if [i%2]==0:
b+=c
Tiếp theo, bất kỳ vòng lặp nào bắt đầu bằng một đối tượng trống, đi qua một điều khác [chuỗi, danh sách, iterator, v.v.] và đặt các giá trị vào một ITable mới, có thể chạy các giá trị thông qua bộ lọc if
hoặc biểu thức biến đổi chúng, có thể được biến thành một sự hiểu biết rất dễ dàng.
Mặc dù Python có sự hiểu biết cho các danh sách, bộ, dicts và iterators, nhưng nó không có sự hiểu biết cho các chuỗi, nhưng
for i, value in enumerate[foo]:
something with value and i
0 giải quyết điều đó.Vì vậy, đặt nó lại với nhau:
b = "".join[c for i, c in enumerate[s] if i%2 == 0]
Gần như không súc tích hoặc có thể đọc được như ________ 11, nhưng tốt hơn rất nhiều so với những gì bạn bắt đầu với Thư với
for i, value in enumerate[foo]:
something with value and i
3 [có thể được thực hiện bằng cách kết hợp hai lát, nhưng điều đó không rõ ràng ngay lập tức], v.v. Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Method #1 : Using upper[] + lower[] + loop
This task can be performed in brute force method in a way that we iterate through the string and convert odd elements to uppercase and even to lower case using upper[] and lower[] respectively.
Python3
Bàn luận
Vấn đề thay đổi trường hợp Một chuỗi là khá phổ biến và đã được thảo luận nhiều lần. Đôi khi, chúng ta có thể có một vấn đề như thế này trong đó chúng ta cần chuyển đổi ký tự lẻ của chuỗi thành vỏ trên và thậm chí các ký tự được định vị thành chữ thường. Hãy thảo luận về một số cách nhất định trong đó điều này có thể được thực hiện. và thậm chí với chữ thường bằng cách sử dụng trên [] và thấp hơn [] tương ứng. & nbsp;
for i, value in enumerate[foo]:
something with value and i
4for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
6for i, value in enumerate[foo]:
something with value and i
7for i, value in enumerate[foo]:
something with value and i
8for i, value in enumerate[foo]:
something with value and i
9 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
1for i, c in enumerate[s]:
if [i%2]==0:
b+=c
2for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, value in enumerate[foo]:
something with value and i
5 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
5for i, c in enumerate[s]:
if [i%2]==0:
b+=c
6 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
7for i, c in enumerate[s]:
if [i%2]==0:
b+=c
8 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
9for i, value in enumerate[foo]:
something with value and i
8b = "".join[c for i, c in enumerate[s] if i%2 == 0]
1b = "".join[c for i, c in enumerate[s] if i%2 == 0]
2b = "".join[c for i, c in enumerate[s] if i%2 == 0]
3The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS7
b = "".join[c for i, c in enumerate[s] if i%2 == 0]
9b = "".join[c for i, c in enumerate[s] if i%2 == 0]
3if
b = "".join[c for i, c in enumerate[s] if i%2 == 0]
5 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
7b = "".join[c for i, c in enumerate[s] if i%2 == 0]
7 b = "".join[c for i, c in enumerate[s] if i%2 == 0]
8 b = "".join[c for i, c in enumerate[s] if i%2 == 0]
9The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS0
for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, value in enumerate[foo]:
something with value and i
5 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS5
The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS
0
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
5 for i, value in enumerate[foo]:
something with value and i
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS
4
The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS
for i, value in enumerate[foo]:
something with value and i
7for i, value in enumerate[foo]:
something with value and i
8The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS7
for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
1The original string is : geeksforgeeks String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s0
Method #2 : Using list comprehension
This is the shortened one liner approach to this problem. It uses same logic as above but in much more compact way.
Python3
Bàn luận
Vấn đề thay đổi trường hợp Một chuỗi là khá phổ biến và đã được thảo luận nhiều lần. Đôi khi, chúng ta có thể có một vấn đề như thế này trong đó chúng ta cần chuyển đổi ký tự lẻ của chuỗi thành vỏ trên và thậm chí các ký tự được định vị thành chữ thường. Hãy thảo luận về một số cách nhất định trong đó điều này có thể được thực hiện. và thậm chí với chữ thường bằng cách sử dụng trên [] và thấp hơn [] tương ứng. & nbsp;
for i, value in enumerate[foo]:
something with value and i
4for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
6for i, value in enumerate[foo]:
something with value and i
7for i, value in enumerate[foo]:
something with value and i
8for i, value in enumerate[foo]:
something with value and i
9 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
1for i, c in enumerate[s]:
if [i%2]==0:
b+=c
2The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS0
for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, value in enumerate[foo]:
something with value and i
5 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS5
The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS
0
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
5 for i, value in enumerate[foo]:
something with value and i
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS
4
The original string is : geeksforgeeks The alternate case string is : GeEkSfOrGeEkS
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Method #1 : Using loop
This is brute force way in which this task can be performed. In this, we iterate for each element and insert the character required. We erase the last stray character.
Python3
Bàn luận
Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thêm một ký tự sau mỗi ký tự trong chuỗi. Loại vấn đề này có thể có ứng dụng của nó trong nhiều lĩnh vực lập trình ngày. Hãy thảo luận về một số cách nhất định trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho từng yếu tố và chèn ký tự cần thiết. Chúng tôi xóa nhân vật đi lạc cuối cùng. & NBSP;
for i, value in enumerate[foo]:
something with value and i
4for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
6for i, value in enumerate[foo]:
something with value and i
7for i, value in enumerate[foo]:
something with value and i
8for i, value in enumerate[foo]:
something with value and i
9 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, value in enumerate[foo]:
something with value and i
01for i, value in enumerate[foo]:
something with value and i
02for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
04for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
07for i, c in enumerate[s]:
if [i%2]==0:
b+=c
6 for i, value in enumerate[foo]:
something with value and i
09for i, c in enumerate[s]:
if [i%2]==0:
b+=c
8 for i, value in enumerate[foo]:
something with value and i
11b = "".join[c for i, c in enumerate[s] if i%2 == 0]
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
09for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, value in enumerate[foo]:
something with value and i
18Đầu ra: & nbsp;
The original string is : geeksforgeeks String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s
for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
21for i, value in enumerate[foo]:
something with value and i
222____123for i, value in enumerate[foo]:
something with value and i
24Method #2 : Using join[]
This is easiest, elegant and recommended way to perform this task. In this, we just use one line to perform this task. The join[] is used to perform it.
Python3
Bàn luận
Đôi khi, trong khi làm việc với Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thêm một ký tự sau mỗi ký tự trong chuỗi. Loại vấn đề này có thể có ứng dụng của nó trong nhiều lĩnh vực lập trình ngày. Hãy thảo luận về một số cách nhất định trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho từng yếu tố và chèn ký tự cần thiết. Chúng tôi xóa nhân vật đi lạc cuối cùng. & NBSP;
for i, value in enumerate[foo]:
something with value and i
4for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
6for i, value in enumerate[foo]:
something with value and i
7for i, value in enumerate[foo]:
something with value and i
8for i, value in enumerate[foo]:
something with value and i
9 for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, value in enumerate[foo]:
something with value and i
01b = "".join[c for i, c in enumerate[s] if i%2 == 0]
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
3for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0for i, value in enumerate[foo]:
something with value and i
5 for i, value in enumerate[foo]:
something with value and i
09for i, c in enumerate[s]:
if [i%2]==0:
b+=c
0 for i, value in enumerate[foo]:
something with value and i
18Đầu ra: & nbsp;
The original string is : geeksforgeeks String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:
Độ phức tạp về thời gian: O [n]O[n]
Độ phức tạp không gian: O [n]O[n]