我有一个任意长度的列表,我需要将它分成相同大小的块并对其进行操作.有一些明显的方法可以做到这一点,比如保留一个计数器和两个列表,当第二个列表填满时,将它添加到第一个列表并清空下一轮数据的第二个列表,但这可能非常昂贵.
我想知道是否有人对任何长度的列表都有一个很好的解决方案,例如使用生成器.
我一直在寻找有用的东西,itertools但我找不到任何明显有用的东西.但是可能会错过它.
如何从可迭代对象一次生成多个项目?
例如,对于任意长度的序列,如何在每次迭代的X个连续项的组中迭代序列中的项?
我对使用游标类的tweepy和分页相当新.我一直在尝试使用光标类来获取特定Twitter用户的所有关注者,但我不断得到它所说的错误"tweepy.error.TweepError: This method does not perform pagination"
因此我真的很感激任何帮助,如果有人可以帮助我实现这个任务获得所有的追随者一个特殊的Twitter用户,使用tweepy进行分页.我到目前为止的代码如下:
import tweepy
consumer_key='xyz'
consumer_secret='xyz'
access_token='abc'
access_token_secret='def'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.get_user('somehandle')
print user.name
followers = tweepy.Cursor(user.followers)
temp=[]
for user in followers.items():
temp.append(user)
print temp
#the following part works fine but that is without pagination so I will be able to retrieve at #most 100 followers
aDict = user.followers()
for friend in aDict:
friendDict = friend.__getstate__()
print friendDict['screen_name']
Run Code Online (Sandbox Code Playgroud)