我想将列表中字符串的某些部分切成另一个列表,
例如考虑他们是列表数据:
data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
我想将每个组件切成三部分,
product = ["xbox 360","playstation 4","playstation 3","playstation 2"]
cost = ["13000","30000","30000","30000"]
condition = ["new","new","old","old"]
请帮我
最佳答案
下面的代码使用普通的zip(*...)
换位模式,同时在适当的分隔符上分割字符串:
>>> prd, cst, cnd = zip(*(s.split(' | ') for s in data))
>>> prd
('xbox 360', 'playstation 4', 'playstation 3', 'playstation 2')
>>> cst
('13000', '30000', '30000', '30000')
>>> cnd
('new', 'new', 'old', 'old')