是python的新手 我在这附近有一个嵌套列表,
[['Elevation 3,000 feet'], [' Ground Snow Load is0 '], ['Elevation > 3,000 and 4,500 feet'], [' Ground Snow Load is5 '], ['Elevation > 4,500 and 5,400 feet'], [' Ground Snow Load is10 ']]
从上面的嵌套列表中我想从列表中得到基本数字。
我的最终列表应该是,
[['3000'], ['0'], ['3000','4500'], ['5'], ['4500' , '5400'], ['10']]
提前谢谢你们。
答案 0 :(得分:2)
你没有写任何代码,所以我不会给你一个完整的解决方案。
>>> data = [['Elevation 3,000 feet'], [' Ground Snow Load is0 '], ['Elevation > 3,000 and 4,500 feet'], [' Ground Snow Load is5 '], ['Elevation > 4,500 and 5,400 feet'], [' Ground Snow Load is10 ']]
>>> import re
>>> [re.findall('\d+',l[0]) for l in data]
[['3', '000'], ['0'], ['3', '000', '4', '500'], ['5'], ['4', '500', '5', '400'], ['10']]
现在您只需首先删除,
,或在正则表达式中接受混合数字和,
。
答案 1 :(得分:1)
包含太多,但有效。 (假设"列表"中的每个列表中只有一个项目。它可以修复。)
lists = [['Elevation 3,000 feet'], [' Ground Snow Load is0 '], ['Elevation > 3,000 and 4,500 feet'], [' Ground Snow Load is5 '], ['Elevation > 4,500 and 5,400 feet'], [' Ground Snow Load is10 ']]
result = []
for list in lists:
numbers = []
str = list[0].replace(",", "").replace(".", "")
for word in str.split(): #separate from space
number = ""
for char in word:
if char.isdigit():
number += char
if number:
numbers.append(number)
result.append(numbers)
print (result)
输出:
[['3000'], ['0'], ['3000', '4500'], ['5'], ['4500', '5400'], ['10']]