【发布时间】:2016-04-08 17:35:59
【问题描述】:
如果这个问题已经得到解答,我很抱歉(我查看并没有找到任何东西。)请告诉我,我会立即删除。
我正在编写一个程序来进行 API 调用,该调用返回多个不同长度的列表,具体取决于调用 (例如 facebook API 调用。输入人员姓名并返回图片列表,每张图片都有一个谁“喜欢”每张照片的列表。我想存储这些“喜欢”列表的列表)。
#Import urllib for API request
import urllib.request
import urllib.parse
#First I have a function that takes two arguments, first and last name
#Function will return a list of all photos the person has been tagged in facebook
def id_list_generator(first,last):
#Please note I don't actually know facebook API, this part wil not be reproducible
pic_id_request = urllib.request.open('www.facebook.com/pics/id/term={first}+{last}[person]')
pic_id_list = pic_id_request.read()
for i in pic_id_list:
id_list.append(i)
return(id_list)
#Now, for each ID of a picture, I will generate a list of people who "liked" that picture.
#This is where I have trouble. I don't know how to store these list of lists.
for i in id_list:
pic_list = urllib.request.open('www.facebook.com/pics/id/like/term={i}[likes]')
print pic_list
这将为标记此人的每张照片打印多个“喜欢”列表:
foo, bar
bar, baz
baz, foo, qux
norf
我真的不知道如何诚实地存储这些。
我正在考虑使用附加后看起来像这样的列表:
foo = [["foo", "bar"], ["bar","baz"],["baz","foo","qux"],["norf"]]
但实际上我不确定在这种情况下使用哪种类型的存储。我想过使用字典的字典,但我不知道键是否可以迭代。我觉得我缺少一个简单的答案。
【问题讨论】:
-
你的例子有点太抽象了,没有多大意义。您能否举一个实际输入数据的示例,并再举一个您所需输出的示例。展示您自己尝试过的任何东西也没有什么坏处。
-
嗨,保罗,我试图澄清更多:(
标签: python list python-2.7 dictionary