Hướng dẫn how do you remove digits in python? - làm thế nào để bạn loại bỏ các chữ số trong python?

Làm cách nào để xóa các chữ số khỏi chuỗi?

Hướng dẫn how do you remove digits in python? - làm thế nào để bạn loại bỏ các chữ số trong python?

Congusbongus

12.6K6 Huy hiệu vàng69 Huy hiệu bạc95 Huy hiệu Đồng6 gold badges69 silver badges95 bronze badges

Hỏi ngày 12 tháng 10 năm 2012 lúc 3:27Oct 12, 2012 at 3:27

8

Điều này sẽ làm việc cho tình huống của bạn?

>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'

Điều này sử dụng sự hiểu biết danh sách và những gì đang xảy ra ở đây tương tự như cấu trúc này:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)

Vì @ashwinichaudhary và @KirkStrauser chỉ ra, bạn thực sự không cần sử dụng dấu ngoặc trong một lớp lót, làm cho phần bên trong dấu ngoặc đơn trở thành một biểu thức của trình tạo (hiệu quả hơn so với khả năng hiểu danh sách). Ngay cả khi điều này không phù hợp với các yêu cầu cho nhiệm vụ của bạn, đó là điều bạn nên đọc về cuối cùng :):

>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 3:34Oct 12, 2012 at 3:34

RocketdonkeyrocketdonkeyRocketDonkey

Phim huy hiệu vàng 35,5K77 gold badges78 silver badges84 bronze badges

7

Và, chỉ để ném nó vào hỗn hợp, là

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
7 bị lãng quên sẽ hoạt động nhanh hơn rất nhiều so với các biểu thức lặp/thông thường:

Cho Python 2:

from string import digits

s = 'abc123def456ghi789zero0'
res = s.translate(None, digits)
# 'abcdefghizero'

Cho Python 3:

from string import digits

s = 'abc123def456ghi789zero0'
remove_digits = str.maketrans('', '', digits)
res = s.translate(remove_digits)
# 'abcdefghizero'

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 9:46Oct 12, 2012 at 9:46

Hướng dẫn how do you remove digits in python? - làm thế nào để bạn loại bỏ các chữ số trong python?

Jon Clements ♦ Jon ClementsJon Clements

135K32 Huy hiệu vàng242 Huy hiệu bạc275 Huy hiệu Đồng32 gold badges242 silver badges275 bronze badges

3

Không chắc chắn nếu giáo viên của bạn cho phép bạn sử dụng bộ lọc nhưng ...

filter(lambda x: x.isalpha(), "a1a2a3s3d4f5fg6h")

returns-

'aaasdffgh'

Hiệu quả hơn nhiều so với vòng lặp ...

Example:

for i in range(10):
  a.replace(str(i),'')

Hướng dẫn how do you remove digits in python? - làm thế nào để bạn loại bỏ các chữ số trong python?

Fredtantini

15.2k8 Huy hiệu vàng47 Huy hiệu bạc55 Huy hiệu Đồng8 gold badges47 silver badges55 bronze badges

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 4:19Oct 12, 2012 at 4:19

Alain Nisamalain NisamAlain Nisam

6623 Huy hiệu bạc13 Huy hiệu Đồng3 silver badges13 bronze badges

2

Cái này thì sao:

out_string = filter(lambda c: not c.isdigit(), in_string)

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 3:44Oct 12, 2012 at 3:44

Pavel Paulaupavel PaulauPavel Paulau

7661 Huy hiệu vàng7 Huy hiệu bạc19 Huy hiệu đồng1 gold badge7 silver badges19 bronze badges

2

Chỉ một vài (những người khác đã đề xuất một số trong số này)

Phương pháp 1:

''.join(i for i in myStr if not i.isdigit())

Phương pháp 2:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
0

Phương pháp 3:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
1

Phương pháp 4:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
2

Phương pháp 5:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
3

3

Nói ST là chuỗi không được định dạng của bạn, sau đó chạy

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
4

Như được đề cập ở trên. Nhưng tôi đoán rằng bạn cần một cái gì đó rất đơn giản vì vậy hãy nói s là chuỗi của bạn và st_res là một chuỗi không có chữ số, thì đây là mã của bạns is your string and st_res is a string without digits, then here is your code

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
5

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 4:09Oct 12, 2012 at 4:09

iddqdiddqdiddqd

1.2991 Huy hiệu vàng8 Huy hiệu bạc9 Huy hiệu đồng1 gold badge8 silver badges9 bronze badges

Tôi muốn sử dụng Regex để thực hiện điều này, nhưng vì bạn chỉ có thể sử dụng danh sách, vòng lặp, chức năng, v.v.

Đây là những gì tôi nghĩ ra:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
6

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 3:34Oct 12, 2012 at 3:34

Hướng dẫn how do you remove digits in python? - làm thế nào để bạn loại bỏ các chữ số trong python?

RocketdonkeyrocketdonkeySean Johnson

Phim huy hiệu vàng 35,5K72 gold badges15 silver badges22 bronze badges

Và, chỉ để ném nó vào hỗn hợp, là

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)
7 bị lãng quên sẽ hoạt động nhanh hơn rất nhiều so với các biểu thức lặp/thông thường:

Đã trả lời ngày 12 tháng 10 năm 2012 lúc 3:34Oct 12, 2012 at 3:34

RocketdonkeyrocketdonkeyBaahubali

Phim huy hiệu vàng 35,5K76 gold badges31 silver badges71 bronze badges

1