我正在尝试从网页中删除此列表中的“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:列表索引必须是整数,而不是元组
我怎么能解决这个问题?
答案 0 :(得分:3)
我会使用列表理解:
>>> 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']
不确定pandas
连接是什么。如果您尝试从MultiIndex
中提取列,则有更简单的方法。
答案 1 :(得分:2)
你可以使用列表理解(通常更常见,因为它清晰,简洁并被认为是Pythonic):
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]
或者您可以使用for
上的x
循环:
for i in range(len(x)):
x[i] = x[i][0][1:]
正如原始答案之后所指出的那样,我使用Python的del
语句(例如del x[0][0][0]
)的其他原始建议也不起作用,因为tuple
不支持项目删除
答案 2 :(得分:1)
试一试:
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 ])
答案 3 :(得分:1)
其他解决方案:
>>> 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']
>>>