对不起,如果这已被回答(我看,没有找到任何东西。)请让我知道,我会立即删除。组织列表的动态列表
我正在编写一个程序,使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"]]
不过说真的,我不知道在这种情况下使用什么类型的存储。我想过使用字典的字典,但我不知道密钥是否可迭代。我觉得这是一个简单的答案,我错过了。
你的例子太抽象了,没有什么意义。你能给出一些实际输入数据的例子,并给出你需要的输出的另一个例子。展示你自己尝试过的任何东西也不会有什么伤害。 –
嗨保罗,我试图澄清更多:( – Sean