Hướng dẫn increment each character in the string by 1 python - tăng mỗi ký tự trong chuỗi thêm 1 python

Tôi đã tìm kiếm cách làm điều này trong Python và tôi không thể tìm thấy câu trả lời. Nếu bạn có một chuỗi:

>>> value = 'abc' 

Làm thế nào bạn sẽ tăng tất cả các ký tự trong một chuỗi 1? Vì vậy, đầu vào mà tôi đang tìm kiếm là:

>>> value = 'bcd' 

Tôi biết tôi có thể làm điều đó với một ký tự bằng cách sử dụng ord và chr:

>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b

Nhưng

>>> value = 'bcd' 
2 và
>>> value = 'bcd' 
3 chỉ có thể lấy một ký tự. Nếu tôi sử dụng cùng một câu lệnh ở trên với một chuỗi nhiều hơn một ký tự. Tôi sẽ gặp lỗi:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 3 found 

Có cách nào để làm việc này không?

Đã hỏi ngày 5 tháng 3 năm 2016 lúc 22:24Mar 5, 2016 at 22:24

Hướng dẫn increment each character in the string by 1 python - tăng mỗi ký tự trong chuỗi thêm 1 python

2

Bạn có thể sử dụng biểu thức máy phát với

>>> value = 'bcd' 
4 như sau:

In [153]: value = 'abc'

In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)

In [155]: value_altered
Out[155]: 'bcd'

Trình tạo lặp lại trên mỗi

>>> value = 'bcd' 
5 trong chuỗi
>>> value = 'bcd' 
6 và tăng thêm một bằng cách sử dụng phương pháp
>>> value = 'bcd' 
7 được đề xuất trong câu hỏi của bạn. Sau đó, nó sử dụng
>>> value = 'bcd' 
4 để chuyển đổi các chữ cái trong trình tạo trở lại thành một chuỗi.

Đã trả lời ngày 5 tháng 3 năm 2016 lúc 22:26Mar 5, 2016 at 22:26

Hướng dẫn increment each character in the string by 1 python - tăng mỗi ký tự trong chuỗi thêm 1 python

Gtlambertgtlambertgtlambert

11.5K2 Huy hiệu vàng29 Huy hiệu bạc48 Huy hiệu đồng2 gold badges29 silver badges48 bronze badges

2

Khi Gtllambert đánh bại tôi với câu trả lời ban đầu của tôi, tôi đang đăng một giải pháp thay thế. Bạn cũng có thể sử dụng

>>> value = 'bcd' 
9 và biểu thức Lambda để đạt được điều tương tự. Biểu thức Lambda sử dụng
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
0 và
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
1 để tăng mỗi ký tự bằng một và
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
0 được sử dụng để chuyển đổi nó trở lại một ký tự.

value = 'abc'
''.join(map(lambda x:chr(ord(x)+1),value))

Hướng dẫn increment each character in the string by 1 python - tăng mỗi ký tự trong chuỗi thêm 1 python

Gtlambert

11.5K2 Huy hiệu vàng29 Huy hiệu bạc48 Huy hiệu đồng2 gold badges29 silver badges48 bronze badges

Khi Gtllambert đánh bại tôi với câu trả lời ban đầu của tôi, tôi đang đăng một giải pháp thay thế. Bạn cũng có thể sử dụng

>>> value = 'bcd' 
9 và biểu thức Lambda để đạt được điều tương tự. Biểu thức Lambda sử dụng
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
0 và
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
1 để tăng mỗi ký tự bằng một và
>>> value = 'a'
>>> print (chr(ord(value)+1)) 
>>> b
0 được sử dụng để chuyển đổi nó trở lại một ký tự.Mar 5, 2016 at 22:32

GtlambertAlex

Đã trả lời ngày 5 tháng 3 năm 2016 lúc 22:3210 gold badges61 silver badges71 bronze badges

value = 'abc'
newVar=(chr(ord(value[0])+1))
newVar1=(chr(ord(value[1])+1))
newVar2=(chr(ord(value[2])+1))
value=newVar+newVar1+newVar2
print(value)

Alexalex

20.7k10 Huy hiệu vàng61 Huy hiệu bạc71 Huy hiệu đồngSep 6, 2016 at 0:09

1

Đây là những gì tôi nghĩ ra không thể tin rằng nó thực sự hoạt động nhờ vào thử thách khi sử dụng Python 3

finalMessage=""
for x in range (0,len(value)):
    finalMessage+=(chr(ord(value[x])+1))
print(finalMessage)

Đã trả lời ngày 6 tháng 9 năm 2016 lúc 0:09

value="abc testing testing, or sdrshmf"
finalMessage=""
for x in range(0,len(value)):
    if ord(value[x]) in range(97,123):
        finalMessage+=(chr(((ord(value[x])-96)%26)+97))
    elif ord(value[x]) in range(65,91):
        finalMessage+=(chr(((ord(value[x])-64)%26)+65))
    else:
        finalMessage+=value[x]
print(finalMessage)

Phần bốn dòng rất đơn giản của mã:Mar 5, 2016 at 23:18

Hướng dẫn increment each character in the string by 1 python - tăng mỗi ký tự trong chuỗi thêm 1 python

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    ĐọcAdding 1 to a character, if we require to increment the character, an error instructing type conflicts occur, hence other ways need to be formulated to increment the characters.
     

    Bàn luận

    Trong Python không có khái niệm ngầm về các loại dữ liệu, mặc dù có thể chuyển đổi rõ ràng các loại dữ liệu, nhưng chúng tôi không dễ dàng hướng dẫn nhà điều hành hoạt động theo cách và hiểu loại dữ liệu của toán hạng và thao tác theo đó. Ví dụ, thêm 1 vào một ký tự, nếu chúng ta yêu cầu tăng ký tự, một lỗi hướng dẫn xung đột loại, do đó các cách khác cần được xây dựng để tăng các ký tự. & NBSP;

    Python

    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    3
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    5

    Output:  
     

    Traceback (most recent call last):
      File "/home/fabc221bf999b96195c763bf3c03ddca.py", line 9, in 
        s = s + 1
    TypeError: cannot concatenate 'str' and 'int' objects

    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    3
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    3
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    9
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    0

    Python3

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    2

    Sử dụng ord () + chr ()

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    3
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    5

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    6
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    9
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    1
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    1
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    9
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    3
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    4

    Output:  
     

    >>> value = 'bcd' 
    
    0

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    9
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    7
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    8
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    value = 'abc'
    ''.join(map(lambda x:chr(ord(x)+1),value))
    
    0
    ord() returns the corresponding ASCII value of character and after adding integer to it, chr() again converts it into character.
     

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    value = 'abc'
    ''.join(map(lambda x:chr(ord(x)+1),value))
    
    2

    Python3

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    2

    Sử dụng ord () + chr ()

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    3
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    5

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    6
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    9
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    1
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    1
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    9
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    3
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    4

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    9
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    7
    In [153]: value = 'abc'
    
    In [154]: value_altered = ''.join(chr(ord(letter)+1) for letter in value)
    
    In [155]: value_altered
    Out[155]: 'bcd'
    
    8
    >>> value = 'a'
    >>> print (chr(ord(value)+1)) 
    >>> b
    
    4
    value = 'abc'
    ''.join(map(lambda x:chr(ord(x)+1),value))
    
    0

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ord() expected a character, but string of length 3 found 
    
    1
    value = 'abc'
    ''.join(map(lambda x:chr(ord(x)+1),value))
    
    2

    Output:  
     

    >>> value = 'bcd' 
    
    1

    Giải thích: Ord () trả về giá trị ASCII tương ứng của ký tự và sau khi thêm số nguyên vào nó, chr () một lần nữa chuyển đổi nó thành ký tự. & NBSP;The character is converted to byte string , incremented, and then again converted to string form with prefix “‘b”, hence 3rd value gives the correct output.
    This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    Có I ++ trong Python không?

    Python, theo thiết kế, không cho phép sử dụng toán tử ++.Thuật ngữ ++, được gọi là toán tử tăng trong C ++ / Java, không có vị trí trong Python.does not allow the use of the ++ “operator”. The ++ term, is called the increment operator in C++ / Java, does not have a place in Python.

    Tại sao AA ở Python?

    Họ theo thứ tự từ điển.Thứ tự bảng chữ cái đầu tiên, chiều dài như một breaker tie.Giống như trong một từ điển, một AA đi trước trước z.Lưu câu trả lời này.a precedes aa which precedes z . Save this answer.

    Có nhà điều hành gia tăng trong Python?

    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.