Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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/8/python-3.x/19.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_Python 3.x_For Loop_Twitter - Fatal编程技术网

Python 双环

Python 双环,python,python-3.x,for-loop,twitter,Python,Python 3.x,For Loop,Twitter,我正在抓取推特趋势主题,并想做三个单独的抓取: 1.)按地理位置 2.)使用Hashtags 3.)没有标签 因此,我表面上需要一个双for循环,但其复杂性使其变得困难 这就是我所拥有的: i = 0 locations = [23424977,2459115, 2487956] onoff = [1,2] for location in locations: for switch in onoff: i += 1 if i < 4:

我正在抓取推特趋势主题,并想做三个单独的抓取:

1.)按地理位置
2.)使用Hashtags
3.)没有标签

因此,我表面上需要一个双for循环,但其复杂性使其变得困难

这就是我所拥有的:

i = 0
locations = [23424977,2459115, 2487956]
onoff = [1,2]
for location in locations:
    for switch in onoff:
         i += 1
         if i < 4:
             trends1 = api.trends_place(location,include='hashtags')
         if i >=4:
             trends1 = api.trends_place(location,exclude='hashtags')
         print(trends1)

我认为您不需要开关和I:

locations = [23424977,2459115, 2487956]
onoff = [1,2]
for switch in onoff:
    for location in locations:
        if onoff == 1:
             trends1 = api.trends_place(location,include='hashtags')
        else:
             trends1 = api.trends_place(location,exclude='hashtags')
        print(trends1)
简单易行的解决方案——我的原始代码颠倒了for循环的顺序。由于i变量在循环的每次迭代中更新为+1,因此前三个循环具有hashtags,因为i=4

i=0
地点=[234249772459115,2487956]
onoff=[1,2]
对于接通断开开关:
对于位置中的位置:
i+=1
如果i<4:
trends1=api.trends\u位置(位置,包括class='hashtags')
如果i>=4:
trends1=api.trends\u位置(位置,排除class='hashtags')

我怀疑您的瓶颈来自网络延迟。为什么不每个geo发出一个请求,然后自己拆分它呢?i的想法是嵌套for循环应该总共迭代6次(3个geos X 2个hashtag状态)。每次代码在循环中运行时,i变量的值都是+1——我的逻辑是在geo include hashtags的前三个循环中(这是一个基本的示例,但我会在以后保存输出——问题是这个for循环不会在所有的geos中迭代。对不起,您的描述让我有点困惑没有在您的解决方案中初始化,所以我会这样做,看看您的结果是否更好。将这与保存结果结合起来,而不是像您现在这样丢弃它们,也许它会做您想做的事情。希望我的代码能让您更近距离。它确实会在GEO中迭代。自您每个循环都过度写入相同的变量。覆盖不是问题。我在每个循环后打印结果。我编辑了初始答案,以包括我希望如何设置for循环
locations = [23424977,2459115, 2487956]
onoff = [1,2]
for switch in onoff:
    for location in locations:
        if onoff == 1:
             trends1 = api.trends_place(location,include='hashtags')
        else:
             trends1 = api.trends_place(location,exclude='hashtags')
        print(trends1)
i = 0
locations = [23424977,2459115, 2487956]
onoff = [1,2]
for switch in onoff:
    for location in locations:

     i += 1
     if i < 4:
         trends1 = api.trends_place(location,include='hashtags')
     if i >=4:
         trends1 = api.trends_place(location,exclude='hashtags')