Hướng dẫn how to add delimiter in python - cách thêm dấu phân cách trong python

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

  • Đọc
  • Bàn luận
  • 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*9
    0____10
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    2

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    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

    Đầ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
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    3
    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 : 1678589
    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*
    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 : 1678589
    1
    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 : 1678589
    4

    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*9
    0____10
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    2

    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

    Đầ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
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    3
    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 : 1678589
    6
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9*
    7
    O(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

  • Đọc
  • Bàn luận
  • 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']
    
    04

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    0____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']
    
    10

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    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
    >>> 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']
    
    17

    The original string is : 1@6@7@8@5@8@9
    Constructed integer : 1678589
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    3
    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

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    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

    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*
    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 : 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']
    
    04

    The original string is : 1@6@7@8@5@8@9
    Constructed integer : 1678589
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    0
    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    3
    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']
    
    04

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    0____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

    The original list is : [7, 'Gfg', 8, 'is', 'best', 9]
    The resultant string : 7*Gfg*8*is*best*9
    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']
    
    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']
    
    70

    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']
    
    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 : 1678589
    4

    Đầ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)


    Làm thế nào để bạn thêm dấu phân cách vào một chuỗi trong Python?

    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.Using loop + str() This is one of the ways in which this task can be performed. In this, we run a loop to add delimiter at end of each element, after converting each element to string.

    Làm thế nào để bạn phân định một danh sách trong Python?

    Phương thức Split String () Python cho phép bạn chia một chuỗi thành một danh sách tại một phân tách được chỉ định.Chẳng hạn, bạn có thể sử dụng split () để chia một chuỗi cho dấu phẩy (,) hoặc bằng chữ j.. For instance, you could use split() to divide a string by commas (,), or by the letter J .

    Delimiter làm gì trong Python?

    LƯU Ý: Một dấu phân cách là một chuỗi của một hoặc nhiều ký tự được sử dụng để chỉ định ranh giới giữa các vùng riêng biệt, độc lập trong văn bản thuần túy hoặc các luồng dữ liệu khác.Một ví dụ về dấu phân cách là ký tự dấu phẩy, hoạt động như một dấu phân cách trường trong một chuỗi các giá trị được phân tách bằng dấu phẩy.to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.

    Delimiter ở đâu trong Python?

    Installation..
    Cú pháp: Phát hiện (văn bản: str, văn bản: str, default = none, whitelist = [',', ';', ':', '|', '\ t'], blacklist = none).
    Văn bản: Chuỗi đầu vào để kiểm tra cho dấu phân cách ..
    Mặc định: Giá trị mặc định cho đầu ra trong trường hợp không tìm thấy dấu phân cách hợp lệ nào ..