Check that list of tuples has tuple with 1st element as defined string

江枫思渺然 提交于 2019-12-10 15:43:23

问题


I'm parsing HTML and I need to get only tags with selector like div.content.

For parsing I'm using HTMLParser. I'm so far that I get list of tags' attributes.

It looks something like this:

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

The problem is that I don't know how to check that:

  1. List have tuple with 1st element called class,
  2. Values of tuples 1st element (it will be 2nd element) is content;

I know this is easy question, but I'm quite new with Python as well. Thanks in any advice!


回答1:


When looping through your elements:

if ('class', 'content') in element_attributes:
    #do stuff



回答2:


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

('class', 'content') in l

returns True, because there is at least one tuple with 'class' as first and 'content' as second element.

You can now use it:

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



回答3:


It's worth noting that HTML 'class' attributes are allowed to be a space separated list of css classes. E.g., you can do <span class='green big'>...</span>. It sounds like what you really want to know is whether a given HTML element has a specific CSS class (given a list of (attribute,value) pairs). In that 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 certain that all elements you care about will have only one CSS class, then sr2222's answer is better/simpler.




回答4:


For check if one of the tuple element has some value you could use 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 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



回答5:


1st question)

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

2nd question)

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

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




回答6:


Try this:

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


来源:https://stackoverflow.com/questions/9703088/check-that-list-of-tuples-has-tuple-with-1st-element-as-defined-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!