简体   繁体   中英

How can i remove specifics and repeat words from Python List?

Hello i hope yall good im trying to deleted some items and words from a List.

ListA = ['', 'dulce_renaca', 'pastelerialuly', 'cons.bakery', 'kukkichile', 'panaderiasanjoaquin', 'ceciliabolocco\nVerified', '', 'coasthealthyfood', 'bajozeroalimentos', 'dulce_renaca', 'kukkichile']

I want to eliminate all the empty items like " " and the repeated words like "dulce_renaca". and also remove or split the ones that contains the word "\nVerified.

Expect Result:

ListB = ['dulce_renaca', 'pastelerialuly', 'cons.bakery', 'kukkichile', 'panaderiasanjoaquin', 'coasthealthyfood', 'bajozeroalimentos']

Please help me and teach me how to do it. Thanks a lot!

ListA = ['', 'dulce_renaca', 'pastelerialuly', 'cons.bakery', 'kukkichile', 'panaderiasanjoaquin',
         'ceciliabolocco\nVerified', '', 'coasthealthyfood', 'bajozeroalimentos',
         'dulce_renaca', 'kukkichile']

v = [list(n for n in x.split("\n")) if "\n" in x else x for x in list(set(filter(None,ListA)))]
d = []
for b in v:
    fun = lambda x: d.extend(x) if len(x) < min([len(v) for v in list(set(filter(None,ListA)))]) else d.append(x)
    fun(b)

Filter is to filter out the none values and set is used to remove the duplicates. Then we use for and lambda function to flatten out the list.

d
Out[101]: 
['dulce_renaca',
 'coasthealthyfood',
 'bajozeroalimentos',
 'cons.bakery',
 'kukkichile',
 'pastelerialuly',
 'panaderiasanjoaquin',
 'ceciliabolocco',
 'Verified']

The Output...The Above code and output if you want to split the value that had "\n" in it...

If you want to remove that value

ListA = ['', 'dulce_renaca', 'pastelerialuly', 'cons.bakery', 'kukkichile', 'panaderiasanjoaquin',
         'ceciliabolocco\nVerified', '', 'coasthealthyfood', 'bajozeroalimentos',
         'dulce_renaca', 'kukkichile']

v = [x  for x in list(set(filter(None,ListA))) if "\n" not in x]

The above code will work

v
Out[106]: 
['dulce_renaca',
 'coasthealthyfood',
 'bajozeroalimentos',
 'cons.bakery',
 'kukkichile',
 'pastelerialuly',
 'panaderiasanjoaquin']

The above is the output....

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