【问题标题】:Keep Python from Providing Duplicate Outputs防止 Python 提供重复的输出
【发布时间】:2019-11-12 19:45:28
【问题描述】:

我的目标是让一系列项目提供尽可能多的输出,但没有重复的输出。我提供的代码是我正在处理的一个小示例。对于我较大的数据集,我注意到脚本运行时重复输出困扰着 CSV 文件,所以我想知道是否有办法在保持高范围(100、250、400 等)的同时保持重复输出不被处理?

import random
Saying = ["I Like"]

Food = ['Coffee', 'Pineapples', 'Avocado', 'Bacon']
Holiday = ['on the 4th of July', 'on April Fools', 'during Autumn', 'on Christmas']

for x in range(10):
    One = random.choice(Saying)
    Two = random.choice(Food)
    Three = random.choice(Holiday)
    print(f'{One} {Two} {Three}')

感谢您的帮助!

【问题讨论】:

标签: python duplicates output


【解决方案1】:

问题是你的机器人(我猜?)没有记忆到目前为止的输出,所以真的没有办法检查你的代码。

试试这个:

import random
Saying = ["I Like"]

Food = ['Coffee', 'Pineapples', 'Avocado', 'Bacon']
Holiday = ['on the 4th of July', 'on April Fools', 'during Autumn', 'on Christmas']

memory=[]
done = False

while not done:
    One = random.choice(Saying)
    Two = random.choice(Food)
    Three = random.choice(Holiday)

    if f'{One} {Two} {Three}' not in memory:
        memory.append(f'{One} {Two} {Three}')
        if len(memory) == 10:
            done = True

[print(item) for item in memory]

所以现在我们不是在创建 10 个词组时花费 10 个技巧,而是尽可能多地创建 10 个不同的词组。

【讨论】:

  • 谢谢大家!我非常感谢这个社区对我的问题的帮助。它有效,耶!
【解决方案2】:

您可以将set 与您已经看到的元素一起使用,然后检查您是否看到set 中的元素,平均复杂度为O(1)。

另一种选择是随机播放列表并弹出一些元素:

import random

random.shuffle(lst)

while lst:
    element = x.pop()

【讨论】:

  • 仍然效率低下,因为您将创建许多重复项
【解决方案3】:

您可以将np.random.choice 与参数replace=False 一起使用。此外,您可以使用size 参数对尽可能多的样本进行采样。

import numpy as np

Food = ['Coffee', 'Pineapples', 'Avocado', 'Bacon']
Holiday = ['on the 4th of July', 'on April Fools', 'during Autumn', 'on Christmas']

np.random.choice(Food, size=4, replace=False)
>>> array(['Avocado', 'Coffee', 'Pineapples', 'Bacon'], dtype='<U10')

np.random.choice(Holiday, size=4, replace=False)
>>> array(['on April Fools', 'on the 4th of July', 'during Autumn',
       'on Christmas'], dtype='<U18')

【讨论】:

    【解决方案4】:

    您可以通过以下方式在保持非冗余数据的同时生成随机输出:

    1. 首先创建一个列表permutations,它基本上是要排列的列表的产物。
    permutations = list(itertools.product(*Statement))
    ## Example - [('I Like', 'Coffee', 'on the 4th of July'), ('I Like', 'Coffee', 'on April Fools'), ('I Like', 'Coffee', 'during Autumn'), ('I Like', 'Coffee', 'on Christmas')]
    
    1. 通过随机选择索引并打印从permutations 中挑选元素。
     num = int(random.random() * total_elements)
     print '{} {} {}'.format(permutations[num][0], permutations[num][1], permutations[num][2])
    
    1. 接下来,我们从列表permutations 中删除元素以避免冗余。
    del permutations[num]
    

    完整代码:

    import itertools, random
    Saying = ["I Like"]
    
    Food = ['Coffee', 'Pineapples', 'Avocado', 'Bacon']
    Holiday = ['on the 4th of July', 'on April Fools', 'during Autumn', 'on Christmas']
    
    Statements = [Saying, Food, Holiday]
    
    permutations = list(itertools.product(*Statements))
    
    random.seed()
    
    total_elements = len(Saying) * len(Food) * len(Holiday)
    
    while total_elements > 0:
        num = int(random.random() * total_elements)
        print '{} {} {}'.format(permutations[num][0], permutations[num][1], permutations[num][2])
        del permutations[num]
        total_elements = total_elements - 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多