英文:
Remove non numeric characters from a list
问题
Amount = [35, 45, 25]
英文:
I have a list that looks like this:
Amount = [USD $35 m, CAD $ 45 Millon, AED 25Mil ]
I am looking for an output:
Amount = [35,45,25]
I appreciate your help!!
答案1
得分: 4
你可以使用正则表达式来实现你想要的效果:
import re
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
print([re.sub(r'[^\d\.]', '', a) for a in amount])
这个代码考虑了浮点数,如果不需要可以使用以下代码替代:
re.sub(r'[^\d]', '', a)
英文:
You can use a regex to achieve what you want :
import re
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
print([re.sub(r'[^\d\.]', '', a) for a in amount])
This one takes floating point number in consideration, if not change it with this one :
re.sub(r'[^\d]', '', a)
答案2
得分: 2
You can use the following code to achieve this:
import re
Amount = ["USD $35 m", "CAD $ 45 Millon", "AED 25Mil"]
result = [int(re.search(r'\d+', el).group()) for el in Amount]
print(result)
Output:
[35, 45, 25]
IMPORTANT: If you have float values, you should use a regex solution to handle them correctly. The code above will work for integer values, as shown in your provided examples.
英文:
For:
Amount = ["USD $35 m", "CAD $ 45 Millon", "AED 25Mil"]
You can do this:
print([int("".join([ch for ch in el if ch in "0123456789"])) for el in Amount])
or
print([int("".join([ch for ch in el if ch.isdigit()])) for el in Amount])
Output:
[35, 45, 25]
IMPORTANT: If you have float values please use regex solution! This is NOT working for such element:
"Value 2.5 M€" >> 25
答案3
得分: 0
amount = ["$36", "Test", 46, "26€"]
output = []
for x in amount:
number = ""
for char in str(x):
if char.isnumeric():
number += char
if number != '':
output.append(int(number))
print(output)
Output will be: [36, 46, 26]
英文:
amount = ["$36", "Test", 46, "26€"]
output = []
for x in amount:
number = ""
for char in [*str(x)]:
if char.isnumeric():
number += str(char)
if number != '':
output.append(int(number))
print(output)
Output will be: [36, 46, 26]
You're welcome
答案4
得分: 0
假设金额列表中的值是字符串,您可以像这样操作:
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
result = []
for i in amount:
num = ''
for j in i:
if j.isnumeric():
num = num + j
result.append(num)
print(result) # ['35', '45', '25']
您还可以将其封装为一个函数,如下:
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
def get_nums(amount_list):
result = []
for i in amount_list:
num = ''
for j in i:
if j.isnumeric():
num = num + j
result.append(num)
return result
print(get_nums(amount)) # ['35', '45', '25']
关于isdigit()
、isnumeric()
和isdecimal()
在Python中的更多信息,请参考此链接,这可能对您的项目有用。
英文:
assuming that the values in the amount list are strings you can do like this:
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
result = []
for i in amount:
num = ''
for j in i:
if j.isnumeric():
num = num + j
result.append(num)
print(result) # ['35', '45', '25']
You can also make it a function. Like this:
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
def get_nums(amount_list):
result = []
for i in amount_list:
num = ''
for j in i:
if j.isnumeric():
num = num + j
result.append(num)
return result
print(get_nums(amount)) # ['35', '45', '25']
• See this link to more information about isdigit()
, isnumeric()
, and isdecimal()
in python. It maybe be useful to your project.
答案5
得分: -1
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil']
output = []
for element in amount:
curr = ''
for char in element:
if char.isdigit():
curr += char
output.append(curr)
output = [int(str_num) for str_num in output]
print(output)
英文:
amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil']
output = []
for element in amount:
curr = ''
for char in element:
if char.isdigit():
curr += char
output.append(curr)
output = [int(str_num) for str_num in output]
print(output)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论