简体   繁体   中英

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:

>>> 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() :

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. The 1st one is very short which will give your all 3 lists just with an execution of one line statement. I have used he concept of list comprehension and reduce() function.

Use split() to get the words separated with | for each of the list items.

Use strip() to remove leading/trailing whitespaces.

1st way (one line statement)

Just use 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']
>>>

2nd way

>>> 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM