Print a search term that does not exist in the list list comprehension

I am starting out with Python and trying to do something in one line of code.

resourceProperties = 'test test token test' rpAttrs = [ ['ajgagag', 'knkagga'], ['token', 'knkagga'], ['ajgagag', 'knkagga'] ] for value in rpAttrs if not list(re.finditer(value[0], resourceProperties)) : print value[0] 

I get the following error and am not sure what exactly is wrong, because I saw similar Python code where it immediately follows a list for a list.

Syntax Error: invalid syntax

The error indicates if .

My goal is to print every search request in rpAttrs that doesn't appear in resourceProperties . I am curious to try to do this in only one line of code. Can someone tell me what I am doing wrong?

+4
source share
4 answers
 list_comp = [value[0] for value in rpAttrs if not list(re.finditer(value[0], resourceProperties))] >> ['ajgagag', 'ajgagag'] 

is the right way to make an understanding

+3
source

You can try this with a slightly larger pythonic version of your loop and conditional:

 import re resourceProperties = 'test test token test' rpAttrs = [['ajgagag', 'knkagga'], ['token', 'knkagga'], ['ajgagag', 'knkagga']] print('\n'.join(value[0] for value in rpAttrs if not list(re.finditer(value[0], resourceProperties)))) 

It is output:

 ajgagag ajgagag 
+1
source

You are missing python python awaiting for syntax. You must break your blocks for / if, this makes it easier to read and catch errors like this. This should work:

 for value in rpAttrs: if not list(re.finditer(value[0], resourceProperties)): print value[0] 

As a side note, there are no concepts in lists, just a list of lists. Understanding a list is the syntax for working in a list (or as a list) to create a new list, for example:

 oddSquares = [number**2 for number in myListOfNumbers if (number % 2) == 1] 
+1
source

Based on OP confirmation that only a โ€œcontainmentโ€ check is required

 >>> print [value[0] for value in rpAttrs if not value[0] in resourceProperties] ['ajgagag', 'ajgagag'] 
+1
source

Source: https://habr.com/ru/post/1490543/


All Articles