Hướng dẫn increment number in string python - số gia tăng trong chuỗi python

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

    Đọche task is to write a Python program to increment the number which is at end of the string.

    Bàn luận : test_str = ‘geeks006’ 
    Output : geeks7 
    Explanation : Suffix 006 incremented to 7.

    Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để tăng số ở cuối chuỗi. : test_str = ‘geeks007’ 
    Output : geeks8 
    Explanation : Suffix 007 incremented to 8. 
     

    Đầu vào: test_str = ‘geek006, & nbsp; đầu ra: geek7 & nbsp; giải thích: hậu tố 006 tăng lên 7.join[] + replace[]

    Đầu vào: test_str = ‘geek007, & nbsp; đầu ra: geek8 & nbsp; giải thích: hậu tố 007 tăng lên 8. & nbsp; & nbsp;

    Python3

    Phương thức số 1: Sử dụng findall [] + tham gia [] + thay thế []

    Trong đó, chiến lược, chúng tôi thực hiện nhiệm vụ tìm số bằng cách sử dụng findall [], sau đó thực hiện nhiệm vụ phân tách chuỗi số và chuỗi tiền tố, sau đó thực hiện tăng chuỗi số được thực hiện. Cuối cùng, chuỗi được nối để có được một tiền tố theo sau là một số được tăng lên.

    import re

    test_str = 'geeks006'

    print____1010____11

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    4

    The original string is : geeks006
    Incremented numeric String : geeks007
    5=
    The original string is : geeks006
    Incremented numeric String : geeks007
    7
    The original string is : geeks006
    Incremented numeric String : geeks007
    8
    The original string is : geeks006
    Incremented numeric String : geeks007
    9
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    0
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    1

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    2=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    4

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    5=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    7
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    8
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    9
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    0

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    1=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    3

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    4=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    6
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    7
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    9

    Output:

    The original string is : geeks006
    Incremented numeric String : geeks61

    import0=

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    1
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3import5

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0import8
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Python3

    Phương thức số 1: Sử dụng findall [] + tham gia [] + thay thế []

    Trong đó, chiến lược, chúng tôi thực hiện nhiệm vụ tìm số bằng cách sử dụng findall [], sau đó thực hiện nhiệm vụ phân tách chuỗi số và chuỗi tiền tố, sau đó thực hiện tăng chuỗi số được thực hiện. Cuối cùng, chuỗi được nối để có được một tiền tố theo sau là một số được tăng lên.

    import re

    test_str = 'geeks006'

    print____1010____11

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    4

    test_str 8=4

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    4=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    6
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    7
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    9

    Output:

    The original string is : geeks006
    Incremented numeric String : geeks007

    import0=

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    1
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3import5

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0import8
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1
    O[n]

    Phương thức số 2: Sử dụng sub [] + nhóm [] + zfill []O[n]


    Độ 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:

    Examples:

    Độ phức tạp về thời gian: O [n] : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 6 
    Output : [‘gfg’, ‘240’, ‘is’, ‘104’, ‘129’, ‘best’, ’10’] 
    Explanation : 234, 98 and 4 are incremented by 6 in result.

    Độ phức tạp không gian: O [n] : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 4 
    Output : [‘gfg’, ‘238’, ‘is’, ‘102’, ‘129’, ‘best’, ‘8’] 
    Explanation : 234, 98 and 4 are incremented by 4 in result. 

    Phương pháp số 4: Sử dụng phương thức isNumeric [] str[] + int[] + loop + isdigit[]

    import0=

    The original string is : geeks006
    Incremented numeric String : geeks007
    09

    Python3

    'geeks006'1= 'geeks006'3'geeks006'4'geeks006'5'geeks006'6'geeks006'5'geeks006'8'geeks006'55____90'geeks006'5print2

    print____1010100

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    03

    The original string is : geeks006
    Incremented numeric String : geeks007
    04=
    The original string is : geeks006
    Incremented numeric String : geeks007
    06

    import0= 'geeks006'3

    The original string is : geeks006
    Incremented numeric String : geeks007
    3__

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]

    Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    26
    The original string is : geeks006
    Incremented numeric String : geeks007
    27

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original string is : geeks006
    Incremented numeric String : geeks007
    29

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Output:

    Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]

    Phương pháp số 2: Sử dụng danh sách hiểu + isDigit []list comprehension + isdigit[]

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Tương tự như ở trên, chỉ một dòng thay thế để nén giải pháp bằng cách sử dụng danh sách hiểu.

    Python3

    'geeks006'1= 'geeks006'3'geeks006'4'geeks006'5'geeks006'6'geeks006'5'geeks006'8'geeks006'55____90'geeks006'5print2

    print____1010100

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    03

    The original string is : geeks006
    Incremented numeric String : geeks007
    04=
    The original string is : geeks006
    Incremented numeric String : geeks007
    06

    import0= 'geeks006'3

    The original string is : geeks006
    Incremented numeric String : geeks007
    3__

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Output:

    Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]

    Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào

    Python3

    The original string is : geeks006
    Incremented numeric String : geeks007
    85
    The original string is : geeks006
    Incremented numeric String : geeks007
    86

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    88=
    The original string is : geeks006
    Incremented numeric String : geeks007
    90

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    92=
    The original string is : geeks006
    Incremented numeric String : geeks007
    94

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    10
    The original string is : geeks006
    Incremented numeric String : geeks007
    97
    The original string is : geeks006
    Incremented numeric String : geeks007
    12
    The original string is : geeks006
    Incremented numeric String : geeks007
    99

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original string is : geeks006
    Incremented numeric String : geeks007
    15
    The original string is : geeks006
    Incremented numeric String : geeks007
    97
    The original string is : geeks006
    Incremented numeric String : geeks007
    12
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    04

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    05
    The original string is : geeks006
    Incremented numeric String : geeks007
    92
    The original string is : geeks006
    Incremented numeric String : geeks007
    2=
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    9

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    15
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    12==
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    15
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    16

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    18
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    19

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    18
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    22

    'geeks006'1= 'geeks006'3'geeks006'4'geeks006'5'geeks006'6'geeks006'5'geeks006'8'geeks006'55____90'geeks006'5print2

    print____1010100

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    03

    The original string is : geeks006
    Incremented numeric String : geeks007
    04=
    The original string is : geeks006
    Incremented numeric String : geeks007
    06

    import0= 'geeks006'3

    The original string is : geeks006
    Incremented numeric String : geeks007
    3__

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    15
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    58

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    60
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    6
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    64
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    24

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    26
    The original string is : geeks006
    Incremented numeric String : geeks007
    27

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    71

    Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]

    Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']

    The original string is : geeks006
    Incremented numeric String : geeks007
    85
    The original string is : geeks006
    Incremented numeric String : geeks007
    86

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    10
    The original string is : geeks006
    Incremented numeric String : geeks007
    97
    The original string is : geeks006
    Incremented numeric String : geeks007
    12
    The original string is : geeks006
    Incremented numeric String : geeks007
    99
    O[n]

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original string is : geeks006
    Incremented numeric String : geeks007
    15
    The original string is : geeks006
    Incremented numeric String : geeks007
    97
    The original string is : geeks006
    Incremented numeric String : geeks007
    12
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    04
    O[n]

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    18
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    19
    Using isnumeric[] method

    Python3

    'geeks006'1= 'geeks006'3'geeks006'4'geeks006'5'geeks006'6'geeks006'5'geeks006'8'geeks006'55____90'geeks006'5print2

    print____1010100

    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3
    The original string is : geeks006
    Incremented numeric String : geeks007
    03

    The original string is : geeks006
    Incremented numeric String : geeks007
    04=
    The original string is : geeks006
    Incremented numeric String : geeks007
    06

    import0= 'geeks006'3

    The original string is : geeks006
    Incremented numeric String : geeks007
    3__

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]

    Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào

    The original string is : geeks006
    Incremented numeric String : geeks007
    14
    The original string is : geeks006
    Incremented numeric String : geeks007
    26
    The original string is : geeks006
    Incremented numeric String : geeks007
    27

    The original string is : geeks006
    Incremented numeric String : geeks007
    17
    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
    26

    print

    The original string is : geeks006
    Incremented numeric String : geeks007
    0
    The original string is : geeks006
    Incremented numeric String : geeks007
    32
    The original string is : geeks006
    Incremented numeric String : geeks007
    2
    The original string is : geeks006
    Incremented numeric String : geeks007
    3re1

    Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào

    The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
    Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']


    Bạn có thể viết ++ trong Python không?

    Python không có toán tử tăng trước và sau.Sẽ gán lại B thành B+1.Đó không phải là một toán tử gia tăng, bởi vì nó không tăng B, nó chỉ định lại nó.. Which will reassign b to b+1 . That is not an increment operator, because it does not increment b , it reassigns it.

    ++ i trong python là gì?

    Bạn không viết những thứ như: for [int i = 0; i

    Bài Viết Liên Quan

    Chủ Đề