Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何才能从该代码中获取列表索引超出范围的错误?_Python_List - Fatal编程技术网

Python 如何才能从该代码中获取列表索引超出范围的错误?

Python 如何才能从该代码中获取列表索引超出范围的错误?,python,list,Python,List,我从2个推特账户下载追随者,并将他们放入字典列表中。我从account1下载了10个追随者,从account2下载了10个追随者。使用下面的代码,我获取Account1的前4个跟随者和account2的前4个跟随者并显示它们 twitter_accounts = ["account1", "account2"] res = {} follower = [] pbar = tqdm_notebook(total=len(twitter_accounts)) for twitter_account

我从2个推特账户下载追随者,并将他们放入字典列表中。我从account1下载了10个追随者,从account2下载了10个追随者。使用下面的代码,我获取Account1的前4个跟随者和account2的前4个跟随者并显示它们

twitter_accounts = ["account1", "account2"]
res = {}
follower = []
pbar = tqdm_notebook(total=len(twitter_accounts))

for twitter_account in twitter_accounts:
    inner_structure = []
    for page in tweepy.Cursor(api.followers, screen_name=twitter_account,
                              skip_status=True, include_user_entities=False).items(10):
        val = page._json
        inner_dict = {}
        inner_dict["name"] = val["name"] 
        inner_dict["screen_name"] = val["screen_name"]
        if inner_dict not in inner_structure:
            inner_structure.append(inner_dict)

    res[twitter_account] = inner_structure
    pbar.update(1)

pbar.close()

for twitter_account in twitter_accounts:  
        for i in range(4):
            display(res[twitter_account][i]['screen_name'])
因此,最终结果将显示acc1的前4个跟随者和acc2的前4个跟随者

但我真正需要做的是获取这8个字符串,而不是显示它们并将它们存储到数组中

我尝试了这种方法,但我得到了一个索引超出范围的错误

for twitter_account in twitter_accounts:
        for i in range(4):
            for j in range(8):
            follower[j]= res[twitter_account][i]['screen_name']


如何将它们存储在数组中而不出现错误?

您可以先声明列表,然后将元素附加到列表中:

followers = []
for twitter_account in twitter_accounts:
        for i in range(4):
            followers.append(res[twitter_account][i]['screen_name'])
或者直接使用列表理解(我相信这是可行的,但我现在无法测试):


无论你发现哪个更清晰、可读性更强(

请尝试使用此列表理解:
followers=[res[twitter\u account][i]['screen\u name']for i in range(4)for twitter\u accounts in twitter\u accounts in twitter\u accounts]
打印(followers)
是否有效?或者如果你想让followers中的followers一个一个
:显示(followers)
@Guimoute这样做只需要1个字符串,而不是全部8个。第一个只返回1个追随者名称,这让我觉得它只存储了1个。第二个连续8次返回相同的名称。我甚至尝试了范围内的i(8)追随者[i]但是结果是追随者姓名的前8个字母问题是
追随者
是一个空列表,
追随者[j]
超出范围。我现在尝试第一个,看看它是否有效。我建议将
范围内的i改为
(4):
为res[twitter\u账户]中的用户改为
然后你可以做
用户['screen\u name']
followers = [res[twitter_account][i]['screen_name'] for i in range(4) for twitter_account in twitter_accounts]