2014-10-16 14 views
1

我試圖掰開一個高度嵌套組包含我需要用下面的代碼數據列表:破解這個嵌套列表的列表?

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]] 

在這個例子中,我遍歷每個包含v在我的代碼中依次打印y並依次打印。我曾嘗試過各種循環,併爲此列出解析,但未能獲得可行的結果。

有人可以提出答案嗎?

由於使用索引和迭代子列表

回答

1

剛剛訪問:

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]] 
+0

喜,帕德里克。那是做了我想要的,謝謝。 – gdogg371 2014-10-16 17:42:32

+0

好人,反正好運 – 2014-10-16 17:58:34