如果没有首次运行,则为for语句添加条件

时间:2014-01-06 10:06:38

标签: python python-2.7

我有以下声明:

for user in tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor).items():

但是,我不需要在第一次运行时运行, cursor=current_cursor

我能想到这样做的唯一方法是使用if语句的两个单独的for语句。

是否有一种不那么草率的方式呢?

2 个答案:

答案 0 :(得分:2)

tweepy.Cursor(api.followers, screen_name=s, cursor=None if first_time else current_cursor)

答案 1 :(得分:0)

if first_time:
    first_time = False
    cursor = tweepy.Cursor(api.followers, screen_names)
else:
    cursor = tweepy.Cursor(api.followers, screen_name=s, cursor=current_cursor)

for item in cursor.items():
    whatever(item)

在适当的地方将first_time初始化为True

相关问题