python - 如何根据位置值拆分列表元素
问题描述
我正在尝试从一组数字中分割年份、月份和控制数字。
s = ['201911007', '201203008']
my expected output
year=['2019', '2012']
month=['11','01']
controlnum=['007','008']
解决方案
您可以使用列表推导生成每个部分的元组,然后 zip 将元组分解为单独的列表:
s = ['201911007', '201203008']
year,month,controlNum = map(list,zip(*( (v[:4],v[4:6],v[6:]) for v in s )))
输出:
print(year)
print(month)
print(controlNum)
['2019', '2012']
['11', '03']
['007', '008']
推荐阅读
- python - for 循环在运行后将值重置为 10
- javascript - 在 React Native 中使用文本转语音
- swift - iOS 14 小部件意图
- angular - 如何从 json 文件中填充下拉列表?
- sql - 边界条件问题中的 DATEDIFF 函数
- python - 如何在 tff.simulation.FilePerUserClientData 中编写正确的 dataset_fn?
- gulp - 任务从未定义:未定义
- azure-ad-b2c - How do I create a user in code, add it Azure AD B2C, then log in as that user using Azure AD B2C OAuth (MSAL)
- python - How can I use sys and argparse in same python script without getting unrecognized argument error?
- python - Atom NameError: name '' is not defined