类的Python问题 [英] Python Issues with a Class
本文介绍了类的Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的下课有问题。我一直得到下面的回溯,但是我不确定我做错了什么。我希望看到带有照片标签的字典。任何帮助都会很棒。
I am having issues with my below class. I keep getting the below traceback, butI am not sure were I am going wrong. I am expecting to see a dictionary with photo tags. Any help would be great.
跟踪:
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
Photo Diction的某些内容,它包含几个词典的列表。
Some Contents of Photo Diction, its a list that contains several dictionaries.
[
{
u'photo':{
u'people':{
u'haspeople':0
},
u'dateuploaded':u'1492780609',
u'owner':{
u'username':u'Patrick Foto ;)',
u'realname':u'',
u'nsid':u'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
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'59600577@N07',
u'raw':u'skyscraper',
u'authorname':u'Patrick Foto ;)',
u'id':u'59579247-33334692904-1306'
},
推荐答案
我在这里看到两个问题。
I see two issues here.
-
您的照片命令是一个列表。您需要:
Your photo_dictation is a list. You need either:
for dictation in photo_diction[0]["photo"]["tags"]["tag"]:
或双循环:
for photo in photo_dictation:
for dictation in photo["photo"]["tags"]["tag"]
您将在循环中返回。我想您可能想要:
you are returning within your loop. I think you probably want:
class Photo(object):
def __init__(self,photo_diction):
self.tags = []
for diction in photo_diction["photo"]["tags"]["tag"]:
self.tags.append(diction)
return
这篇关于类的Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文