gpt4 book ai didi

python - 破解这个嵌套列表列表?

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:30 24 4
gpt4 key购买 nike

我正在尝试使用以下代码分解一组高度嵌套的列表,其中包含我需要的数据:

url = 'http://www.whoscored.com/stagestatfeed/9155/stageteams/'
url = str(''.join(url[0:3]))
params = {
'against': '0',
'field': '0',
'teamId': '-1',
'type': '8'
}

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.whoscored.com',
'Referer': 'http://www.whoscored.com/'}

responser = requests.get(url, params=params, headers=headers)

responser = json.loads(responser.text.replace("'", '"').decode('cp1252'))

results = defaultdict(int)
for match in responser:
for num_events, team, events in match:
print "w = ", num_events
print "x = ", team

for y in events:
print "y = ", events

示例输出如下:

w =  13
x = Arsenal
y = [[[[u'goal', u'openplay', u'header', [1]], [u'goal', u'openplay', u'leftfoot', [1]],
[u'goal', u'openplay', u'rightfoot', [3]], [u'goal', u'owngoal', u'rightfoot', [1]], [u'miss',
u'corner', u'header', [2]], [u'miss', u'corner', u'leftfoot', [3]], [u'miss', u'corner',
u'rightfoot', [2]], [u'miss', u'crossedfreekick', u'rightfoot', [2]], [u'miss',
u'directfreekick',
u'leftfoot', [1]], [u'miss', u'openplay', u'header', [2]], [u'miss', u'openplay',
u'leftfoot', [16]], [u'miss', u'openplay', u'rightfoot', [23]]]]]
...
...
...


w = 171
x = Queens Park Rangers
y = [[[[u'goal', u'openplay', u'leftfoot', [1]], [u'miss', u'corner', u'header', [5]], [u'miss',
u'crossedfreekick', u'header', [1]], [u'miss', u'directfreekick', u'rightfoot', [2]],
[u'miss', u'openplay', u'header', [1]], [u'miss', u'openplay', u'leftfoot', [4]], [u'miss',
u'openplay', u'rightfoot', [23]], [u'miss', u'throwin', u'header', [1]]]]]

我想要得到的是每个团队的输出如下:

w =  13
x = Arsenal
y = [u'goal', u'openplay', u'header', [1]]
y = [u'goal', u'openplay', u'leftfoot', [1]]
y = [u'goal', u'openplay', u'rightfoot', [3]]
...
...
...
y = [u'miss', u'openplay', u'rightfoot', [23]]

在此示例中,我将迭代代码中包含变量 y 的每个嵌套列表,并依次打印每个嵌套列表。我已经尝试了各种循环和列表理解,但无法得到有效的东西。

有人可以提出答案吗?

谢谢

最佳答案

只需使用索引进行访问并迭代子列表:

for y in events[0]:
for sub in y:
print ("y = ", sub)

w = 162
x = Crystal Palace
y = [u'goal', u'corner', u'rightfoot', [1]]
y = [u'goal', u'crossedfreekick', u'header', [1]]
y = [u'goal', u'openplay', u'rightfoot', [1]]
y = [u'miss', u'corner', u'header', [6]]
y = [u'miss', u'corner', u'rightfoot', [1]]
y = [u'miss', u'crossedfreekick', u'header', [2]]
y = [u'miss', u'crossedfreekick', u'leftfoot', [1]]
y = [u'miss', u'crossedfreekick', u'rightfoot', [3]]
y = [u'miss', u'openplay', u'header', [2]]
y = [u'miss', u'openplay', u'leftfoot', [9]]
y = [u'miss', u'openplay', u'rightfoot', [14]]
w = 175
x = West Bromwich Albion
y = [u'goal', u'corner', u'header', [2]]
y = [u'goal', u'openplay', u'rightfoot', [3]]
y = [u'goal', u'penalty', u'rightfoot', [1]]
y = [u'miss', u'corner', u'header', [1]]
y = [u'miss', u'directfreekick', u'rightfoot', [3]]
y = [u'miss', u'openplay', u'leftfoot', [12]]
y = [u'miss', u'openplay', u'rightfoot', [21]]

关于python - 破解这个嵌套列表列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26410214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com