简体   繁体   中英

Extract elements from list in Python to create nested list

I have a list such as this (this is pulled from some html texts using bs4):

test = ['Raji GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 'Comp Vortex GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 "Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price\n$70.00\nSale price$14.95\n\n                Save 79%",]

len(test) # 3

I want to loop through each line in the list (list of 3), and extract the 0,2nd and 4th indexed item for each line. so that the output looks like this:

This nested list contains all the items that I want to see.

out = [['Raji GlovesSixSixOneRegular price',
 'Sale price$9.95',
 '                Save 67%'],['Comp Vortex GlovesSixSixOneRegular price',
     'Sale price$9.95',
     '                Save 67%'],["Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price",
 'Sale price$14.95',
 '                Save 79%']]

I know that I can extract the items from the first line like this:

item1 = test[0]
item1 = item1.split(sep = '\n')
item1
indices = [0,2,4]
values =[]
for i in indices:
    print(item1[i])
    values.append(item1[i])
    
values 


['Raji GlovesSixSixOneRegular price',
 'Sale price$9.95',
 '                Save 67%']

I am new to python, and I struggling to pull out these items from each line, and append them back in a nested list (see out above).

Any idea's for how to achieve this?

Simple list comprehension

test = ['Raji GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 'Comp Vortex GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 "Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price\n$70.00\nSale price$14.95\n\n                Save 79%",]

values = [i.split(sep = '\n')[0:5:2] for i in test]

What about something like:

out = []
indices = [0, 2, 4]

for elem in test:
    aux = []
    elem_split = elem.split('\n')
    for index in indices:
        item = elem_split[index]
        aux.append(item)
    out.append(aux)

You can do it as below

values = []
for item1 in test:
    item1 = item1.split(sep = '\n')
    indices = [0,2,4]
    value = []
    for i in indices:
        value.append(item1[i])
    values.append(value)
print(values)

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