[英]how to slice strings inside array into another array using python
I want to slice certain parts of a string inside a list into another list, for example consider their is the list data: 我想将一个列表中的字符串的某些部分切成另一个列表,例如考虑它们是列表数据:
data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
I want to slice each component into three, 我想将每个组件切成三部分,
product = ["xbox 360","playstation 4","playstation 3","playstation 2"]
cost = ["13000","30000","30000","30000"]
condition = ["new","new","old","old"]
please help me 请帮我
The following uses the common zip(*...)
transpositioning pattern while splitting the strings on an appropriate separator: 下面的方法使用普通的
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')
You could loop through your list of data and then split each element. 您可以遍历数据列表,然后拆分每个元素。 Then you can add that data to the
product
, cost
and condition
lists by using .append()
: 然后,您可以使用
.append()
将数据添加到product
, cost
和condition
列表中:
data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
product = []
cost = []
condition = []
for string in data:
strSplit = string.split(" | ")
product.append(strSplit[0])
cost.append(strSplit[1])
condition.append(strSplit[2])
print(product)
print(cost)
print(condition)
Result of the following code: 以下代码的结果:
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
['13000', '30000', '30000', '30000']
['new', 'new', 'old', 'old']
@surya , you can try any one of the below 2 approaches. @surya ,您可以尝试以下两种方法中的任何一种。 The 1st one is very short which will give your all 3 lists just with an execution of one line statement.
第一个很短,只需执行一条line语句即可给出所有3个列表。 I have used he concept of list comprehension and reduce() function.
我用过他的列表理解和reduce()函数的概念。
Use split() to get the words separated with
|
使用split()获得用
|
分隔的单词 for each of the list items.对于每个列表项。
Use strip() to remove leading/trailing whitespaces.
使用strip()删除前导/尾随空格。
Just use
product, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]);
只需使用
product, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]);
and it will give your lists.它将列出您的清单。
>>> data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
>>>
>>> product, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]);
>>>
>>> product
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
>>>
>>> cost
['13000', '30000', '30000', '30000']
>>>
>>> condition
['new', 'new', 'old', 'old']
>>>
>>> data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
>>>
>>> product = []
>>> cost = []
>>> condition = []
>>>
>>> for s in data:
... l = [item.strip() for item in s.split("|")]
... product.append(l[0])
... cost.append(l[1])
... condition.append(l[2])
...
>>> product
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
>>>
>>> cost
['13000', '30000', '30000', '30000']
>>>
>>> condition
['new', 'new', 'old', 'old']
>>>
>>>
data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
product = [y.split("|")[0].strip() for y in data]
cost = [y.split("|")[1].strip() for y in data]
condition = [y.split("|")[2].strip() for y in data]
print(product)
print(cost)
print(condition)
Output 输出量
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
['13000', '30000', '30000', '30000']
['new', 'new', 'old', 'old']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.