Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

python - TypeError: 'ItemIterator' object is not reversible

I try to iterate in reverse order, but I got error "TypeError: 'ItemIterator' object is not reversible"

here is my code :

# Collect tweets
tweets = tw.Cursor(api.search,
              q=search_words, 
              lang="id",
              since=date_since).items(10)

# Iterate and print tweets
for tweet in reversed(tweets):
 print( tweet.user.screen_name, tweet.created_at, tweet.text)

any idea how to reversed the object?

question from:https://stackoverflow.com/questions/65880489/typeerror-itemiterator-object-is-not-reversible

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

tweets is a generator. so to use reversed you need to convert generator to a list.

so reversed(list(tweets)) can help you to reverse the tweets

# Collect tweets
tweets = tw.Cursor(api.search,
          q=search_words, 
          lang="id",
          since=date_since).items(10)

# Iterate and print tweets
for tweet in reversed(list(tweets)):
    print( tweet.user.screen_name, tweet.created_at, tweet.text)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...