아래 클래스에 문제가 있습니다. 나는 아래의 추적을 계속 간다. 그러나 나는 틀리게 가고있다라고 확신하지 않는다. 나는 사진 태그가있는 사전을보기를 기대하고있다. 어떤 도움이라도 좋을 것입니다.클래스와 관련된 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'
},
당신의 역 추적이 어디에 있습니까? – latsha
@latsha 방금 내가 포함 된 게시물을 편집했습니다! 감사! – sammie1991
문제는 수업과 관련이 없습니다. 'photo_diction'의 구조를 보여줄 필요가 있습니다. 어딘가에 키에 저장된리스트가 있습니다. "tags"는 복수형이기 때문에 나의 추측은'photo_diction [ "photo"] [ "tags"] [0] [ "tag"]'첫 번째 태그에 접근하는 것이 가능할 것이다 ... 그러나 그것은 우리가 ' 그것을 보아라. – roganjosh