如何在 Python 中对推文进行无替换采样

问题描述 投票:0回答:0

我有一个使用 Twitter API 的小企业,我需要随机发送推文。 20 年前我曾经编程过,我想我会尝试使用 ChatGPT 来指导我。它做得相当好,当它确实出错时,我能够快速修复或修改它,但它会随机选择一条推文并将其添加回列表中。我需要将其从列表中删除直到它通过所有这些,然后重新开始(不放回的抽样)。所以我最终得到了以下程序:

import tweepy
import random
import time

# Enter your Twitter API credentials
API_key = "secret"
API_secret = "secret"
Access_token = "secret-token"
Access_token_secret = "secret"

# Authenticate with Twitter
auth = tweepy.OAuthHandler(API_key, API_secret)
auth.set_access_token(Access_token, Access_token_secret)

# Create the API object
api = tweepy.API(auth)

# List of tweets to post
tweets = [
"""This is tweet number 1""",
"""This is tweet number 2""",
"""This is tweet number 3""",
"""This is tweet number 4""",
"""This is tweet number 5""",
]

# Initialize the list of used tweets
used_tweets = []

# Loop through the tweets and post them twice an hour, around the clock, 90 days
for i in range(90):
for j in range(24):
        minute = random.randint(1, 1800)   // randomly in 30 min incriments
        # Sample a random tweet from the list without replacement
        tweet = random.sample(set(tweets) - set(used_tweets), 1)[0]
        # Add the tweet to the list of used tweets
        used_tweets.append(tweet)
        # If all tweets have been used, start over
        if len(used_tweets) == len(tweets):
        used_tweets = []
        print(f"NEXT TO PRINT: {tweet}\n")
        # Schedule the tweet
        api.update_status(status=tweet)
        print(f"TWEETED: {tweet}\n")
        print(f"Minutes to wait: {minute/60}\n")
        # Wait for the random time to pass before tweeting again
        time.sleep(m

这给了我一条错误消息,“Population must be a sequence or set”,我假设是因为“used_tweets”列表,但我不知道。谁能帮我让这个简单的程序运行起来?

python random twitter sampling openai-api
© www.soinside.com 2019 - 2024. All rights reserved.