取消列出python列表并删除元素

取消列出python列表并删除元素,python,pandas,web-scraping,Python,Pandas,Web Scraping,我正在尝试从我从web上抓取的列表中删除title元素: x = [[(u'title', u'Goals for')], [(u'title', u'Goals against')], [(u'title', u'Penalty goal')], [(u'title', u'Goals for average')], [(u'title', u'Matches Played')], [(u'title', u'Shots on goal')], [(u'title', u'Shots Wid

我正在尝试从我从web上抓取的列表中删除title元素:

x = 
[[(u'title', u'Goals for')], [(u'title', u'Goals against')], [(u'title', u'Penalty goal')], [(u'title', u'Goals for average')], [(u'title', u'Matches Played')], [(u'title', u'Shots on goal')], [(u'title', u'Shots Wide')], [(u'title', u'Free Kicks Received')], [(u'title', u'Offsides')], [(u'title', u'Corner kicks')], [(u'title', u'Wins')], [(u'title', u'Draws')], [(u'title', u'Losses')]]
我希望我的期末考试是

result = ['Goals for', 'Goals against','Penalty goal','Goals for average',....]
但我可以做y=x[1][0][1]=>“为目标” 我无法执行x[I][0][1],因为它是我的for循环语句中的索引,我得到了错误

TypeError:列表索引必须是整数,而不是元组


我怎样才能解决这个问题

我会使用列表:

>>> new = [sublist[0][1] for sublist in x]
>>> pprint.pprint(new)
[u'Goals for',
 u'Goals against',
 u'Penalty goal',
 u'Goals for average',
 u'Matches Played',
 u'Shots on goal',
 u'Shots Wide',
 u'Free Kicks Received',
 u'Offsides',
 u'Corner kicks',
 u'Wins',
 u'Draws',
 u'Losses']

不过,我不确定熊猫之间的联系是什么。如果您试图从多索引中提取列,有更简单的方法

列表理解通常更常用,因为它清晰、简洁,而且被认为是一种通俗的理解方式:

x = [[(u'title', u'Goals for')], [(u'title', u'Goals against')], [(u'title', u'Penalty goal')], [(u'title', u'Goals for average')], [(u'title', u'Matches Played')], [(u'title', u'Shots on goal')], [(u'title', u'Shots Wide')], [(u'title', u'Free Kicks Received')], [(u'title', u'Offsides')], [(u'title', u'Corner kicks')], [(u'title', u'Wins')], [(u'title', u'Draws')], [(u'title', u'Losses')]]
x = [i[0][1:] for i in x]
或者,可以使用长度为x的for循环:

正如在原始答案之后指出的那样,我的另一个原始建议是使用Python的del语句,例如del x[0][0][0],也不起作用,因为元组不支持项删除。

试试看:

x = [[('title', 'Goals for')], [('title', 'Goals against')], [('title', 'Penalty goal')], [('title', 'Goals for average')], [('title', 'Matches Played')], [('title', 'Shots on goal')], [('title', 'Shots Wide')], [('title', 'Free Kicks Received')], [('title', 'Offsides')], [('title', 'Corner kicks')], [('title', 'Wins')], [('title', 'Draws')], [('title', 'Losses')]]
print([element[0][1] for element in x ])
其他解决方案:

>>> map(lambda a: a[0][1], x)
... [u'Goals for', u'Goals against', u'Penalty goal', u'Goals for average', u'Matches Played', u'Shots on goal', u'Shots Wide', u'Free Kicks Received', u'Offsides', u'Corner kicks', u'Wins', u'Draws', u'Losses']
>>>

您的listcomp将生成一组空列表:^接得好,帝斯曼!我没有注意到第三层筑巢;
>>> map(lambda a: a[0][1], x)
... [u'Goals for', u'Goals against', u'Penalty goal', u'Goals for average', u'Matches Played', u'Shots on goal', u'Shots Wide', u'Free Kicks Received', u'Offsides', u'Corner kicks', u'Wins', u'Draws', u'Losses']
>>>