2017-04-21 21 views
0

我遇到了我的下課有問題。我一直得到下面的追溯,但我不知道我錯了。我期待看到帶照片標籤的字典。任何幫助都會很棒。Python課題

回溯:

File "project.py", line 231, in <module> 
    new_photo = Photo(result_get_photo_data) 
    File "project.py", line 228, in __init__ 
    for diction in photo_diction["photo"]["tags"]["tag"]: 
TypeError: list indices must be integers, not st 

我的代碼:

class Photo(object): 
    def __init__(self,photo_diction): 
     self.tags = [] 
     for diction in photo_diction["photo"]["tags"]["tag"]: 
      return self.tags.append(diction) 

new_photo = Photo(result_get_photo_data) 
print new_photo.tags 

照片文辭,包含多個詞典的列表的若干內容。

[ 
    { 
     u'photo':{ 
     u'people':{ 
      u'haspeople':0 
     }, 
     u'dateuploaded':u'1492780609', 
     u'owner':{ 
      u'username':u'Patrick Foto ;)', 
      u'realname':u'', 
      u'nsid':u'[email protected]', 
      u'iconserver':u'2943', 
      u'location':u'Bangkok, 
      Thailand', 
      u'path_alias':u'patrick_foto', 
      u'iconfarm':3 
     }, 
     u'publiceditability':{ 
      u'canaddmeta':0, 
      u'cancomment':1 
     }, 
     u'id':u'33334692904', 
     u'title':{ 
      u'_content':u'Kuala Lumpur skyline' 
     }, 
     u'media':u'photo', 
     u'tags':{ 
      u'tag':[ 
       { 
        u'machine_tag':False, 
        u'_content':u'aerialview', 
        u'author':u'[email protected]', 
        u'raw':u'Aerial View', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-8319' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'buildingexterior', 
        u'author':u'[email protected]', 
        u'raw':u'Building Exterior', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1727027' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'businessfinanceandindustry', 
        u'author':u'[email protected]', 
        u'raw':u'Business Finance and Industry', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-263370815' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'capitalcities', 
        u'author':u'[email protected]', 
        u'raw':u'Capital Cities', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-711565' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'citylife', 
        u'author':u'[email protected]', 
        u'raw':u'City Life', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-7631' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'cloudsky', 
        u'author':u'[email protected]', 
        u'raw':u'Cloud - Sky', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-834640' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'colorimage', 
        u'author':u'[email protected]', 
        u'raw':u'Color Image', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-872992' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'downtowndistrict', 
        u'author':u'[email protected]', 
        u'raw':u'Downtown District', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-5634066' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'futuristic', 
        u'author':u'[email protected]', 
        u'raw':u'Futuristic', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-61798' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'nopeople', 
        u'author':u'[email protected]', 
        u'raw':u'No People', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-9003' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'photography', 
        u'author':u'[email protected]', 
        u'raw':u'Photography', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1935' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'traveldestinations', 
        u'author':u'[email protected]', 
        u'raw':u'Travel Destinations', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-872991' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'urbanskyline', 
        u'author':u'[email protected]', 
        u'raw':u'Urban Skyline', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-153762' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'architecture', 
        u'author':u'[email protected]', 
        u'raw':u'architecture', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1174' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'business', 
        u'author':u'[email protected]', 
        u'raw':u'business', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-11698' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'city', 
        u'author':u'[email protected]', 
        u'raw':u'city', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-103' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'cityscape', 
        u'author':u'[email protected]', 
        u'raw':u'cityscape', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-3643' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'day', 
        u'author':u'[email protected]', 
        u'raw':u'day', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-3511' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'growth', 
        u'author':u'[email protected]', 
        u'raw':u'growth', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-12192' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'horizontal', 
        u'author':u'[email protected]', 
        u'raw':u'horizontal', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1049' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'kualalumpur', 
        u'author':u'[email protected]', 
        u'raw':u'kuala lumpur', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-24092' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'malaysia', 
        u'author':u'[email protected]', 
        u'raw':u'malaysia', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-15385' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'modern', 
        u'author':u'[email protected]', 
        u'raw':u'modern', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1180' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'outdoors', 
        u'author':u'[email protected]', 
        u'raw':u'outdoors', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1860' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'panoramic', 
        u'author':u'[email protected]', 
        u'raw':u'panoramic', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-9696' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'sky', 
        u'author':u'[email protected]', 
        u'raw':u'sky', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-282' 
       }, 
       { 
        u'machine_tag':False, 
        u'_content':u'skyscraper', 
        u'author':u'[email protected]', 
        u'raw':u'skyscraper', 
        u'authorname':u'Patrick Foto ;)', 
        u'id':u'59579247-33334692904-1306' 
       }, 
+0

哪裏是你回溯? – latsha

+0

@latsha我剛剛編輯我的文章,包括它!謝謝! – sammie1991

+3

該問題與您的班級無關。你需要顯示'photo_diction'的結構,在那裏你有一個存儲在一個鍵上的列表。我的猜測是'photo_diction [「photo」] [「tags」] [0] [「tag」]'可以訪問第一個標籤,因爲「tags」是複數......但這是一個猜測,因爲我們可以'沒有看到它。 – roganjosh

回答

0

我在這裏看到兩個問題。

  1. Your photo_dictation是一個列表。您需要用:

    for dictation in photo_diction[0]["photo"]["tags"]["tag"]: 
    

    或雙循環:

    for photo in photo_dictation: 
        for dictation in photo["photo"]["tags"]["tag"] 
    
  2. 你是你的循環中返回。我想你可能想:

    class Photo(object): 
        def __init__(self,photo_diction): 
         self.tags = [] 
         for diction in photo_diction["photo"]["tags"]["tag"]: 
          self.tags.append(diction) 
         return 
    
+0

非常感謝@菲爾羅森菲爾德,做到了這一招! – sammie1991

+0

'self.tags = list(photo_diction [「photo」] [「tags」] [「tag」])' –