Điều này còn hơn một chút, nhưng bạn có thể mở rộng biểu thức regex để tính toán ký hiệu khoa học quá.
import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
Cho tất cả tốt!
Ngoài ra, bạn có thể nhìn vào Regex tích hợp keo AWS
số = [].
cho từ trong a_string. tách ra[]:.
Nếu từ. isDigit []:.
số = [].
cho từ trong a_string. tách ra[]:.
Nếu từ. isDigit []:.
số. ỨNG DỤNG [int [từ]].
Làm cách nào để trích xuất số từ một chuỗi trong một python dataframe?
This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.
Cách trích xuất tất cả các số từ một cột chuỗi trong Python Pandas.
Đây là cách bạn có thể chạy để trả về một cột mới chỉ với các số: df ['chỉ số'] = df ['số và văn bản']. ASTYPE ['str']. ] .unstack []. fillna ['']. sum [trục = 1] .Arype [int] ....
Phá vỡ. .Arype ['str'] ....
Xem thảo luận
Đầu ra:
The original string : There are 2 apples for 4 persons The numbers list is : [2, 4]
Cải thiện bài viết
This particular problem can also be solved using python regex, we can use the findall function to check for the numeric occurrences using matching regex string.
Lưu bài viết
Cách trích xuất tất cả các số từ một cột chuỗi trong Python Pandas.
Đây là cách bạn có thể chạy để trả về một cột mới chỉ với các số: df ['chỉ số'] = df ['số và văn bản']. ASTYPE ['str']. ] .unstack []. fillna ['']. sum [trục = 1] .Arype [int] ....
Phá vỡ. .Arype ['str'] ....
Xem thảo luận
Xem thảo luận
Đầu ra:
The original string : There are 2 apples for 4 persons The numbers list is : [2, 4]
Đây là cách bạn có thể chạy để trả về một cột mới chỉ với các số: df ['chỉ số'] = df ['số và văn bản']. ASTYPE ['str']. ] .unstack []. fillna ['']. sum [trục = 1] .Arype [int] ....
- Phá vỡ. .Arype ['str'] ....
- Nội dung
- Ví dụ 2: Lấy danh sách tất cả các chữ số liên tục trong một chuỗi
- Ví dụ 2: Lấy danh sách tất cả các chữ số liên tục trong một chuỗi
Trong ví dụ sau, chúng tôi sẽ lấy một chuỗi, import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
7 và tìm tất cả các số, import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
8, có trong chuỗi.
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
Bản tóm tắt‘[0-9]+’ with re.findall[] method. [0-9] represents a regular expression to match a single digit in the string. [0-9]+ represents continuous digit sequences of any length.
numbers = re.findall['[0-9]+', str]
Trong hướng dẫn này về các ví dụ Python, chúng tôi đã học cách lấy tất cả các số tạo thành một chuỗi làm danh sách, sử dụng các biểu thức thông thường của Python, với sự trợ giúp của các chương trình ví dụ.str is the string in which we need to find the numbers. re.findall[] returns list of strings that are matched with the regular expression.
Nội dung
Trong ví dụ sau, chúng tôi sẽ lấy một chuỗi,
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
5 và tìm tất cả các số, import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
6, có trong chuỗi.Chương trình Python
import re
str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
Chạy
Đầu ra
['9', '162', '666688888']
Ví dụ 2: Lấy danh sách tất cả các chữ số liên tục trong một chuỗi
Trong ví dụ sau, chúng tôi sẽ lấy một chuỗi,
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
7 và tìm tất cả các số, import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
8, có trong chuỗi.Chương trình Python
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
Chạy
Đầu ra
['2', '248', '88796451', '52']
Ví dụ 2: Lấy danh sách tất cả các chữ số liên tục trong một chuỗi
Trong ví dụ sau, chúng tôi sẽ lấy một chuỗi,
import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
7 và tìm tất cả các số, import re
str = 'We four guys, live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
#search using regex
x = re.findall['[0-9]+', str]
print[x]
8, có trong chuỗi.