如果不是第一次运行,则向for语句添加条件

2024-04-27 02:47:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下声明:

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

但是,我不需要在第一次运行时运行, cursor=current_cursor。你知道吗

我能想到的唯一方法是使用if语句来实现两个独立的for语句。你知道吗

有没有不那么草率的方法?你知道吗


Tags: 方法nameinapi声明foritems语句
2条回答
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。你知道吗

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

相关问题 更多 >