0

I´m relatively new to python and I´m using python 3.8 if it matters. I´ve been looking for an answer to how to interpret this. I understand the basics of list comprehension but this nested list comprehension is very challenging to understand for me:

filtred = [str for str in decode.split() if not any(i in str for i in list)]

My interpretation which is wrong is something like this:

list1 = []
filtred=[]
for i in list:
    if i in str:
        list1.append(True)
    else:
        list1.append(False)
for str in text.split():
    if not any(list1):
        filtred.append(str)

I got this from here, where the context is extracting emojis from text:

https://stackoverflow.com/a/50530149/13100800:

EDIT to provide more context

Step 1: Make sure that your text it's decoded on utf-8 text.decode('utf-8')

Step 2: Locate all emoji from your text, you must separate the text character by character [str for str in decode]

Step 3: Saves all emoji in a list [c for c in allchars if c in emoji.UNICODE_EMOJI] full example bellow:

>>> import emoji
>>> text     = "  me así, bla es se  ds "
>>> decode   = text.decode('utf-8')
>>> allchars = [str for str in decode]
>>> list     = [c for c in allchars if c in emoji.UNICODE_EMOJI]
>>> print list
[u'\U0001f914', u'\U0001f648', u'\U0001f60c', u'\U0001f495', u'\U0001f46d', u'\U0001f459']

if you want to remove from text:

>>> filtred  = [str for str in decode.split() if not any(i in str for i in list)]
>>> clean_text = ' '.join(filtred)
>>> print clean_text
me así, bla es se ds

Could someone be kind enough to write the code in the long format?

Thanks in advance

camaron
  • 9
  • 1
  • It is difficult to interpret something without knowing what is supposed to be the content of `list` or `decode`. However, it seems that what you are doing there is to create a list of words (`filtered`) containing only the words in `decode` in which all characters do not appear in `list`. So let's say if `list = ['a', 'b', 'c']`, any word containing any of those three characters will be left out of the new list `filtered`. – Leafar Apr 17 '20 at 23:11
  • @Leafar, yeah you are right. To add more context I have copied and pasted the code. Hopeyfully that will clear things up. – camaron Apr 18 '20 at 07:52

2 Answers2

0

No, the given list comprehension with a generator expression in the any function is more equivalent to:

filtered = []
for s in decode.split():
    has_any = False
    for i in list:
        if i in s:
            has_any = True
            break
    if not has_any:
        filtered.append(s)

Or with a for-else construct, you can avoid using a flag instead:

filtered = []
for s in decode.split():
    for i in list:
        if i in s:
            break
    else:
        filtered.append(s)
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

It is the equivalent of

for str in decode.split():
     add = True
     for i in list:
         if i in str:
              add = False
              break
     if add:
         filtred.append(str)

->

 for str in decode.split():
     if not any(i in str for i in list):
         filtred.append(str)

->

[str for str in decode.split() if not any(i in str for i in list)] 

Note that these are bad variable names because they override builtins

modesitt
  • 7,052
  • 2
  • 34
  • 64