Tôi sẽ sử dụng độ hiểu của trình tạo trong danh sách được liệt kê để tạo ra các bộ [character, 'x'*[index + 1]]
và sau đó làm phẳng kết quả.
[character, 'x'*[index + 1]]
tuples and then flatten the result.
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> [x for tup in [[c, 'x'*i] for i, c in enumerate[lst, 1]] for x in tup]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
Bây giờ điều này có thể hơi khó đọc và có lẽ được thực hiện tốt nhất trong hai bước, với việc làm phẳng được ủy quyền cho itertools.chain.from_iterable
.
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
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
Bàn luận : test_list = [7, “Gfg”, “best”, 9], delim = “*”
Đưa ra một danh sách các yếu tố, chuyển đổi nó thành chuỗi phân tách phân tách. : 7*Gfg*best*9*
Đầu vào: test_list = [7, Hồi GFG, tốt nhất, 9], delim = Hồi*Hồi & nbsp; : All elements are concatenated with “*” as joiner.
Đầu ra: 7*gfg*tốt nhất*9*& nbsp; : test_list = [7, “Gfg”, “best”, 9], delim = “#”
Giải thích: Tất cả các yếu tố được liên kết với các**với tư cách là người tham gia. & Nbsp; : 7#Gfg#best#9#
Đầu vào: test_list = [7, Hồi GFG, tốt nhất, 9], Delim = Hồi##& NBSP; : All elements are concatenated with “#” as joiner.
Đầu ra: 7#gfg#tốt nhất#9#& nbsp;
Giải thích: Tất cả các yếu tố được liên kết với người tham gia.
Python3
Phương pháp số 1: Sử dụng LOOP + STR []
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chạy một vòng lặp để thêm dấu phân cách ở cuối mỗi phần tử, sau khi chuyển đổi từng phần tử thành chuỗi & nbsp; chuỗi.
test_list
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
1>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
2>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
4>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
6>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
8>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3______The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*6
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*9
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*90____10
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*92
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*95
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*96
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*97
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*98
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*99
Đầu ra
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*
The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589
0The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9
3
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9
3The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
7 The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
8The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589
6The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785891
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785894
Python3
Phương pháp số 1: Sử dụng LOOP + STR []
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chạy một vòng lặp để thêm dấu phân cách ở cuối mỗi phần tử, sau khi chuyển đổi từng phần tử thành chuỗi & nbsp; chuỗi.
test_list
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
1>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
2>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
4>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
6>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
8>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
3______The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*6
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*9
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*90____10
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*92
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*96
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*97
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*98
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*99
Đầu ra
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9
The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785890The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*930>>> from itertools import chain >>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]] >>> list[chain.from_iterable[pairs]] ['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785896The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7O[n]Không gian phụ trợ: O [n]O[n]
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
Bàn luận : test_str = “1@6@7@8”, delim = ‘@’
Output : 1678
Explanation : Joined elements after removing delim “@”
Input : test_str = “1!6!7!8”, delim = ‘!’
Output : 1678
Explanation : Joined elements after removing delim “!”
Cho một chuỗi có số phân tách phân tách, Concatenate để tạo thành số nguyên sau khi xóa Delimiter.
Đầu vào: test_str = Hồi 1@6@7@8, delim = '@' & nbsp; đầu ra: 1678 & nbsp; giải thích: các phần tử tham gia sau khi xóa delim = '!' & NBSP; đầu ra: 1678 & nbsp;
Python3
Phương pháp số 1: Sử dụng Loop + Split [] + int []
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chia chuỗi trên Delimiter và sau đó chạy một vòng lặp thành Concat, kết quả cuối cùng được chuyển đổi thành int [].
test_list
6____10 test_list
8
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
01 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
04The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*90____10
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
07[character, 'x'*[index + 1]]
9
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
10The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*95
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*96
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*97
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*98
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
17The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785890
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
23The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9
3
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
26>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
27>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
The original string is : 1@6@7@8@5@8@9 Constructed integer : 1678589
The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
4The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
5
30 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
7 The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
The resultant string : 7*Gfg*8*is*best*9*
8The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589
4
Đầu ra
Python3
Phương pháp số 1: Sử dụng Loop + Split [] + int []
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi chia chuỗi trên Delimiter và sau đó chạy một vòng lặp thành Concat, kết quả cuối cùng được chuyển đổi thành int [].
test_list
6____10 test_list
8
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
01 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
04The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785890
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
23Đầu ra
The original string is : 1@6@7@8@5@8@9 Constructed integer : 1678589
Phương pháp số 3: Sử dụng phương thức thay thế [].
Thay thế dấu phân cách trong chuỗi bằng chuỗi trống
Python3
test_list
6____10 test_list
8
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
01 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
04The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*90____10
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
07The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*93
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
0 >>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
70The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*4
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*5
>>> from itertools import chain
>>> pairs = [[c, 'x'*i] for i, c in enumerate[lst, 1]]
>>> list[chain.from_iterable[pairs]]
['a', 'x', 'b', 'xx', 'c', 'xxx', 'd', 'xxxx', 'e', 'xxxxx', 'f', 'xxxxxx', 'g', 'xxxxxxx', 'h', 'xxxxxxxx']
30 The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*7
The original list is : [7, 'Gfg', 8, 'is', 'best', 9] The resultant string : 7*Gfg*8*is*best*9*8
The original string is : 1@6@7@8@5@8@9 Constructed integer : 16785894
Đầu ra
The original string is : 1@6@7@8@5@8@9 Constructed integer : 1678589
Độ 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]
Không gian phụ trợ: O [n] O[n]