【问题标题】:Randomizing Sentences In Python在 Python 中随机化句子
【发布时间】:2021-04-18 18:48:24
【问题描述】:

我不久前完成了我的项目,该项目根据 YouTube 上的关键字对视频进行了 cmets,它将使用随机库选择随机评论。 当“随机”将 cmets 添加到 YouTube视频。我开始认为我可能需要提高随机性,事实是 它选择了第 4 条评论(共 9 条)7 次第 9 条评论 3 次。这两个 结果一个接一个地发生,换句话说,它连续发送相同的消息 当它意味着随机选择而不是重复时。 有什么办法可以增加随机性吗?如果你知道,请告诉我,我将不胜感激!

PS:这更多的是阻止它在 YouTube 上重复同一句话。

代码:

if __name__ == "__main__":
    from googleapiclient.errors import HttpError
    import random
    import time
    import sys
    
    # Comments are getting loaded
    comments = load_comments('Comments.txt')
    # Getting the number of comments you want to add
    number_of_comments = int(input('Enter the number of comments: '))
    count, cycle, videoid_store = 0, 1, []
    # Getting the keyword
    keyword = input('Enter the Keyword: ')
    
    
    # This loop keeps running until all comments have been added
    youtube = authentication()
    while count < number_of_comments:
        
        print("Searching for videos ..  (Cycle:%d)" %cycle)
        time.sleep(10)
        
        random.shuffle(comments)
        

【问题讨论】:

    标签: python python-3.x random youtube youtube-api


    【解决方案1】:

    试试 random.choice()

    x=random.choice(['option1','option2'])
    

    【讨论】:

    • 请问您能帮我解决这个问题吗?我没有机会了解选择的基本原理。 (它可以去哪里以及如何使用)
    • 您可以将 cmets 存储在列表中,使用 random.choice 可以将评论存储在变量中
    【解决方案2】:

    这是(均匀)随机的。

    如果您想保持随机性但避免重复/频繁再次发生,则必须保留以前结果的历史记录,以便您可以将几率调整为不太频繁的结果。

    具体例子:

    1. 随机播放结果,然后遍历所有。重复。
    2. 记录结果。如果你画了一个已经发生的结果,重新画。尝试与此结果已经出现或类似的次数一样多。

    代码,对于上面的例子:

    import random
    import collections
    
    class ShuffleAndExhaust(): # cycle in random order each time
    
        def __init__(self,N):
    
            self.N = N # number of possible outcomes
            self.queue = list()
    
        def draw(self):
    
            if not self.queue:
                self.queue.extend(range(self.N))
                random.shuffle(self.queue)
                
            return self.queue.pop()
    
    class RememberAndRetry(): # re-draw in case of reoccurring outcomes
    
        def __init__(self,N):
    
            self.N = N # number of possible outcomes
            self.history = collections.deque(maxlen=3*self.N) # maxlen = rule-of-thumb constant * N
    
        def draw(self):
    
            triesleft = collections.Counter(self.history)
            while True:
                n = random.randint(0,self.N-1)
                if triesleft[n] > 0:
                    triesleft[n] -= 1
                else: break
            self.history.append(n)
    
            return n
    

    测试 100 次抽取 10 种可能的选择:

    >>> random.seed(0) # for reproducibility in testing only
    >>> r = ShuffleAndExhaust(10)
    >>> print(*(r.draw() for i in range(100)))
    6 9 0 2 4 3 5 1 8 7 5 3 2 7 1 0 6 8 4 9 4 1 8 6 5 2 3 9 0 7 5 6 9 4 7 1 3 8 2
    0 0 8 9 7 5 3 6 2 1 4 5 3 9 7 0 1 4 6 2 8 8 7 1 0 2 4 3 6 9 5 8 4 1 9 2 6 7 5
    3 0 7 1 6 2 4 8 9 0 3 5 2 0 4 3 8 5 1 7 9 6
    >>> r = RememberAndRetry(10)
    >>> print(*(r.draw() for i in range(100)))
    1 8 6 4 3 9 7 5 1 3 0 2 0 9 1 0 4 6 2 7 8 5 9 3 6 7 0 1 4 5 2 5 4 8 7 6 3 1 7
    9 8 9 4 0 7 9 1 0 8 6 1 5 3 2 5 2 3 6 6 2 7 4 0 4 1 8 6 7 0 1 5 3 9 8 0 9 5 4
    7 7 1 6 8 3 8 5 2 3 9 0 5 0 2 6 2 1 4 4 7 4
    

    使用 N = cmets 的数量和每次抽奖作为要选择的评论的索引。

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 2020-02-16
      • 2013-04-21
      相关资源
      最近更新 更多