简体   繁体   中英

Nested List - Removing Quotations from Inner List

I currently have a list which looks something like:

[["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]

I'm hoping to get a results like:

[['MILK','EGGS','BUTTER','CHEESE'],['MILK', 'BUTTER', 'EGGS'],['EGGS', 'BUTTER']]

It was created from a single-column dataframe using:

value_list = value_df.values.tolist()

I was trying to remove the double quotes around the list to no-avail - I've tried accessing the sublist using something like:

for sublist in value_list:
    sublist[:] = [s.strip('"') for s in sublist] 

But it doesn't seem to be affecting those quotes pre-and-post content.

Any help would be greatly appreciated! Thank you!

Anytime, when talking about removing quotation from string, the ast.literal_eval comes to my mind. The lib was born to do the task.

import ast
l = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]

newL = [list(ast.literal_eval(i[0])) for i in l]
print(newL)

[['MILK', 'EGGS', 'BUTTER', 'CHEESE'],
 ['MILK', 'BUTTER', 'EGGS'],
 ['EGGS', 'BUTTER']]

You can try this simple approach:

list_a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
list_b = []
for a in list_a:
    for aa in a:
        list_b.append(aa.replace("'","").split(","))
print(list_b)

Or more simpler code despite using the same approach:

list_a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
list_b = [aa.replace("'","").split(",") for a in list_a for aa in a]
print(list_b)

If you run the code, the output will be like this:

[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]
abc = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]
final_list=[]
[final_list.append(a.replace("'","").split(",")) for i in abc for a in i]
print(final_list)
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]
  1. create an empty list a2
  2. iterate the lists a (containing l elemnts aka lists)
  3. create a3 empty list
  4. each l element of a has 1 item, a string (so... l[0] gets it)
  5. split that string l[0] by the ',' creating a new temporary list
  6. iterate this sublist, replace the "\\'" and store in a3 the resulting items
  7. at the end of the loop of l[0].split(",") store a3 list into a2 as sublist
  8. at the end of the main loop you have the desired output
a = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]

a2 = []
for l in a:
    a3 = []
    for sub in l[0].split(","):
        sub = sub.replace("\'","")
        a3.append(sub)
    a2.append(a3)

>>> print(a2)
[['MILK', 'EGGS', 'BUTTER', 'CHEESE'], ['MILK', ' BUTTER', ' EGGS'], ['EGGS', ' BUTTER']]

在此处输入图片说明

Following should do.

lst = [["'MILK','EGGS','BUTTER','CHEESE'"],["'MILK', 'BUTTER', 'EGGS'"],["'EGGS', 'BUTTER'"]]

[list(eval(i[0])) for i in lst]

Output:

[['MILK', 'EGGS', 'BUTTER', 'CHEESE'],
 ['MILK', 'BUTTER', 'EGGS'],
 ['EGGS', 'BUTTER']]

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