Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong 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

    Đọc

    Bàn luận : test_str = ‘G4G is 4 all No. 1 Geeks’, K = ‘#’ 
    Output : G#G is # all No. # Geeks 
    Explanation : All numbers replaced by K. 

    Cho một chuỗi chứa số, thay thế từng số bằng K. : test_str = ‘G4G is 4 all No. Geeks’, K = ‘#’ 
    Output : G#G is # all No. Geeks 
    Explanation : All numbers replaced by K.

    Đầu vào: test_str = ‘g4g là 4 tất cả các geek số 1, k =‘ #, & nbsp; đầu ra: g # g là # tất cả số # geek & nbsp; Giải thích: Tất cả các số được thay thế bởi K. & NBSP;

    Đầu vào: test_str = ‘g4g là 4 tất cả số geek, k =‘#, & nbsp; đầu ra: g#g là#tất cả số geek & nbsp; Giải thích: Tất cả các số được thay thế bởi K.

    Python3

    Phương thức số 1: Sử dụng thay thế () + isDigit ()

    Trong đó, chúng tôi kiểm tra các số bằng isDigit () và thay thế () được sử dụng để thực hiện nhiệm vụ thay thế các số bằng K.

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    4

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    4
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    7

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    0

    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    4

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    Đầu ra

    Phương thức số 2: Sử dụng regex () + sub ()

    Trong đó, regex thích hợp được sử dụng để xác định các chữ số và sub () được sử dụng để thực hiện thay thế.

    Phương thức số 1: Sử dụng thay thế () + isDigit ()

    Trong đó, chúng tôi kiểm tra các số bằng isDigit () và thay thế () được sử dụng để thực hiện nhiệm vụ thay thế các số bằng K.

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    4

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3

    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    4

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    Python3

    Phương thức số 1: Sử dụng thay thế () + isDigit ()

    Trong đó, chúng tôi kiểm tra các số bằng isDigit () và thay thế () được sử dụng để thực hiện nhiệm vụ thay thế các số bằng K.

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    4

    >>> s = "_u1_v1"
    >>> ''.join("%d" if c.isdigit() else c for c in s)
    '_u%d_v%d'
    
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    >>> from itertools import groupby, chain
    >>> s = "_u1_v13"
    >>> grouped = groupby(s, str.isdigit)
    >>> ''.join(chain.from_iterable("%d" if k else g for k,g in grouped))
    '_u%d_v%d'
    
    0

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    4
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    7

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    0

    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    1
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    2
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    3
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    4

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    5
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    6
    Name.replace( "_u1_v1" , "_u%d_v%d") 
    
    7
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    8
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    9
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    Đầu raO(n)
    Auxiliary Space: O(n)


    Tôi muốn tạo một chuỗi mới bằng cách thay thế các chữ số bằng

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    00 ví dụ:

    Name.replace( "_u1_v1" , "_u%d_v%d") 
    

    ... Nhưng số

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    01 có thể là bất kỳ chữ số nào chẳng hạn
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    02

    Tôi có thể cho

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    03 một ký tự đại diện để mong đợi bất kỳ chữ số nào không? Như
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    04

    Hay tôi phải tạo ra một biểu hiện thông thường?

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    Johnsyweb

    Huy hiệu vàng 133K2323 gold badges180 silver badges244 bronze badges

    hỏi ngày 29 tháng 9 năm 2013 lúc 23:23Sep 29, 2013 at 23:23

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    1

    Sử dụng các biểu thức thông thường:

    >>> import re
    >>> s = "_u1_v1"
    >>> print re.sub('\d', '%d', s)
    _u%d_v%d
    

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    05 khớp với bất kỳ số 0-9.
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    06 thay thế (các) số bằng
    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    00

    Đã trả lời ngày 29 tháng 9 năm 2013 lúc 23:24Sep 29, 2013 at 23:24

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    TerryaterryaTerryA

    57.4K11 Huy hiệu vàng117 Huy hiệu bạc140 Huy hiệu đồng11 gold badges117 silver badges140 bronze badges

    Bạn không thể;

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    08 chỉ làm việc với văn bản theo nghĩa đen.

    Để thay thế các mẫu, hãy sử dụng các biểu thức thông thường:

    re.sub(r'_u\d_v\d', '_u%d_v%d', inputtext)
    

    Demo:

    >>> import re
    >>> inputtext = '42_u2_v3.txt'
    >>> re.sub(r'_u\d_v\d', '_u%d_v%d', inputtext)
    '42_u%d_v%d.txt'
    

    Đã trả lời ngày 29 tháng 9 năm 2013 lúc 23:24Sep 29, 2013 at 23:24

    Martijn Pieters ♦ Martijn PietersMartijn Pieters

    997K279 Huy hiệu vàng3923 Huy hiệu bạc3264 Huy hiệu Đồng279 gold badges3923 silver badges3264 bronze badges

    Chỉ cho sự đa dạng, một số phương pháp không phải là REGEX:

    >>> s = "_u1_v1"
    >>> ''.join("%d" if c.isdigit() else c for c in s)
    '_u%d_v%d'
    

    Hoặc nếu bạn cần nhóm nhiều chữ số:

    >>> from itertools import groupby, chain
    >>> s = "_u1_v13"
    >>> grouped = groupby(s, str.isdigit)
    >>> ''.join(chain.from_iterable("%d" if k else g for k,g in grouped))
    '_u%d_v%d'
    

    .

    Đã trả lời ngày 29 tháng 9 năm 2013 lúc 23:30Sep 29, 2013 at 23:30

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    DSMDSMDSM

    330K62 Huy hiệu vàng575 Huy hiệu bạc481 Huy hiệu Đồng62 gold badges575 silver badges481 bronze badges

    Một giải pháp sử dụng

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    09 (nguồn):

    remove_digits = str.maketrans('0123456789', '%%%%%%%%%%')
    '_u1_v1'.translate(remove_digits)  # '_u%_v%'
    

    Đã trả lời ngày 4 tháng 12 năm 2019 lúc 23:30Dec 4, 2019 at 23:30

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    LomaphlomaphLoMaPh

    1.2362 huy hiệu vàng17 Huy hiệu bạc32 Huy hiệu đồng2 gold badges17 silver badges32 bronze badges

    Nếu bạn muốn xóa tất cả các chữ số trong chuỗi, bạn có thể thực hiện bằng cách sử dụng

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    09 (xóa số khỏi chuỗi):

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    0

    Tất cả tín dụng được chuyển đến @lomaph

    Hướng dẫn replace single digit numbers in text with word equivalent in python - thay thế các số có một chữ số trong văn bản bằng từ tương đương trong python

    Đã trả lời ngày 25 tháng 4 năm 2020 lúc 16:16Apr 25, 2020 at 16:16

    AhmedahmedAhmed

    Phù hiệu đồng 4177 bronze badges

    The original string is : G4G is 4 all No. 1 Geeks
    The resultant string : G@G is @ all No. @ Geeks
    1

    Đã trả lời ngày 15 tháng 4 năm 2020 lúc 7:24Apr 15, 2020 at 7:24

    2

    Làm thế nào tôi có thể thay thế số bằng chuỗi bằng Python?

    Phương thức số 1: Sử dụng thay thế () + isDigit () Trong này, chúng tôi kiểm tra số bằng isDigit () và thay thế () được sử dụng để thực hiện nhiệm vụ thay thế các số bằng K.Using replace() + isdigit() In this, we check for numerics using isdigit() and replace() is used to perform the task of replacing the numbers by K.

    Có phải thay thế công việc với các chuỗi trong Python?

    Thay thế () Phương thức Python, bạn có thể thay thế mọi phiên bản của một ký tự cụ thể bằng một ký tự mới.Bạn thậm chí có thể thay thế toàn bộ chuỗi văn bản bằng một dòng văn bản mới mà bạn chỉ định.Phương thức .replace () trả về một bản sao của một chuỗi.The . replace() method returns a copy of a string.