sud*_*ini 0 python parsing list
我有一个列表,如
[['2 Cups Fresh breadcrumbs'], ['1/3 Cup Parmesan cheese, grated'], ['For frying Vegetable Oil']]
Run Code Online (Sandbox Code Playgroud)
我想从列表元素中获取数字,例如在获取列表之后应该看起来像这样
[['2', 'Cups Fresh breadcrumbs'], ['1/3', 'Cup Parmesan cheese, grated'], ['','For frying Vegetable Oil']]
Run Code Online (Sandbox Code Playgroud)
我试图从列表中的字符串解析数字.
语言 - 蟒蛇
您可以使用re具有此模式的模块:
data = [['2 Cups Fresh breadcrumbs'], ['1/3 Cup Parmesan cheese, grated'], ['For frying Vegetable Oil']]
pattern = '([0-9].*?)?\s(.*)'
[[item for found in re.findall(pattern, i[0]) for item in found] for i in data]
#[['2', 'Cups Fresh breadcrumbs'], ['1/3', 'Cup Parmesan cheese, grated'], ['','For frying Vegetable Oil']]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |