从列表中删除非数字字符。

huangapple go评论95阅读模式
英文:

Remove non numeric characters from a list

问题

Amount = [35, 45, 25]

英文:

I have a list that looks like this:

  1. Amount = [USD $35 m, CAD $ 45 Millon, AED 25Mil ]

I am looking for an output:

  1. Amount = [35,45,25]

I appreciate your help!!

答案1

得分: 4

你可以使用正则表达式来实现你想要的效果:

  1. import re
  2. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  3. print([re.sub(r'[^\d\.]', '', a) for a in amount])

这个代码考虑了浮点数,如果不需要可以使用以下代码替代:

  1. re.sub(r'[^\d]', '', a)
英文:

You can use a regex to achieve what you want :

  1. import re
  2. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  3. 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 :

  1. re.sub(r'[^\d]', '', a)

答案2

得分: 2

You can use the following code to achieve this:

  1. import re
  2. Amount = ["USD $35 m", "CAD $ 45 Millon", "AED 25Mil"]
  3. result = [int(re.search(r'\d+', el).group()) for el in Amount]
  4. print(result)

Output:

  1. [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:

  1. Amount = ["USD $35 m", "CAD $ 45 Millon", "AED 25Mil"]

You can do this:

  1. print([int("".join([ch for ch in el if ch in "0123456789"])) for el in Amount])

or

  1. print([int("".join([ch for ch in el if ch.isdigit()])) for el in Amount])

Output:

  1. [35, 45, 25]

IMPORTANT: If you have float values please use regex solution! This is NOT working for such element:

  1. "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]

英文:
  1. amount = ["$36", "Test", 46, "26€"]
  2. output = []
  3. for x in amount:
  4. number = ""
  5. for char in [*str(x)]:
  6. if char.isnumeric():
  7. number += str(char)
  8. if number != '':
  9. output.append(int(number))
  10. print(output)

Output will be: [36, 46, 26]

You're welcome 从列表中删除非数字字符。

答案4

得分: 0

假设金额列表中的值是字符串,您可以像这样操作:

  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  2. result = []
  3. for i in amount:
  4. num = ''
  5. for j in i:
  6. if j.isnumeric():
  7. num = num + j
  8. result.append(num)
  9. print(result) # ['35', '45', '25']

您还可以将其封装为一个函数,如下:

  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  2. def get_nums(amount_list):
  3. result = []
  4. for i in amount_list:
  5. num = ''
  6. for j in i:
  7. if j.isnumeric():
  8. num = num + j
  9. result.append(num)
  10. return result
  11. 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:

  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  2. result = []
  3. for i in amount:
  4. num = ''
  5. for j in i:
  6. if j.isnumeric():
  7. num = num + j
  8. result.append(num)
  9. print(result) # ['35', '45', '25']

You can also make it a function. Like this:

  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil' ]
  2. def get_nums(amount_list):
  3. result = []
  4. for i in amount_list:
  5. num = ''
  6. for j in i:
  7. if j.isnumeric():
  8. num = num + j
  9. result.append(num)
  10. return result
  11. 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

  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil']
  2. output = []
  3. for element in amount:
  4. curr = ''
  5. for char in element:
  6. if char.isdigit():
  7. curr += char
  8. output.append(curr)
  9. output = [int(str_num) for str_num in output]
  10. print(output)
英文:
  1. amount = ['USD $35 m', 'CAD $ 45 Millon', 'AED 25Mil']
  2. output = []
  3. for element in amount:
  4. curr = ''
  5. for char in element:
  6. if char.isdigit():
  7. curr += char
  8. output.append(curr)
  9. output = [int(str_num) for str_num in output]
  10. print(output)

huangapple
  • 本文由 发表于 2023年1月9日 04:53:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定