Verify that the tuple list has a tuple with the 1st element as a specific row

I am parsing HTML and I only need to get tags with a div.content type div.content .

For parsing, I use HTMLParser . I am so far away that I get a list of tag attributes.

It looks something like this:

[('class', 'content'), ('title', 'source')]

The problem is that I do not know how to check that:

  • The list has a tuple with a 1st element called class ,
  • The values โ€‹โ€‹of the tuples of the 1st element (this will be the 2nd element) content ;

I know this is a simple question, but I'm completely new with Python. Thanks in any advice!

+4
source share
6 answers

When passing through your elements:

 if ('class', 'content') in element_attributes: #do stuff 
+9
source
 l = [('class', 'content'), ('title', 'source')] ('class', 'content') in l 

returns True because there is at least one tuple with a "class" as the first, and "content" as the second.

Now you can use it:

 if ('class', 'content') in l: # do something 
+2
source

It is worth noting that the "class" attributes of the HTML class are allowed to be a space-separated list of css classes. For example, you can do <span class='green big'>...</span> . It looks like you really want to know if a given HTML element has a specific CSS class (given a list of pairs (attribute, value)). In this case, I would use something like this:

 element_attributes = [('class', 'content'), ('title', 'source')] is_content = any((attr=='class') and ('content' in val.split()) for (attr, val) in element_attributes) 

Of course, if you know for sure that all the elements you care about will have only one CSS class, then the sr2222 answer will be better / simpler.

+2
source

To check if one of the tuple elements has any value, you can use the filter function:

 tuples_list = [('class', 'content'), ('title', 'source')] if filter(lambda a: a[0] == 'class', tuples_list): # your code goes here if filter(lambda a: a[1] == 'content', tuples_list): # your code goes here 

The filter gives you all the tuples that match your conditions:

 values = filter(lambda a: a[1] == 'content', tuples_list) # values == [('class', 'content')] 

If you are sure that they are in the same tuple:

 if ('class', 'content') in tuples_list: # your code goes here 
+1
source

1st question)

 if len(list) > 1: if list[0][0] == 'class': return True` 

Second question)

 for elem in list: if elem[1] == 'content': return True 

Note: from what I understood, the second question means that if ONE of the 2nd tuple is 'content', you want true.

0
source

Try the following:

 l = [('class', 'content'), ('title', 'source')] check = False for item in l: if item[0] == 'class': check=True print item[1] print "List have tuple with 1st element called class: %s" check 
0
source

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


All Articles