Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python随机化单词列表_Python - Fatal编程技术网

用Python随机化单词列表

用Python随机化单词列表,python,Python,我需要帮助解决一个问题。我有8个单词的单子 我需要实现的是生成所有可能的变体,其中正好包含3个单词。所有这些变体都需要保存在不同的.txt文件中。相同但位置不同的单词应视为另一个变体 它需要在Python中的Raspberry Pi上完成 老实说,我甚至不知道从哪里开始 我完全不懂任何编程 有什么提示吗?您可以使用itertools轻松解决此问题。在下面的示例中,我将为列表l生成3个元素的所有可能组合: 导入itertools >>>l=[1,2,3,4,5] >>>列表(itertools.排

我需要帮助解决一个问题。我有8个单词的单子

我需要实现的是生成所有可能的变体,其中正好包含3个单词。所有这些变体都需要保存在不同的.txt文件中。相同但位置不同的单词应视为另一个变体

它需要在Python中的Raspberry Pi上完成

老实说,我甚至不知道从哪里开始

我完全不懂任何编程


有什么提示吗?

您可以使用
itertools
轻松解决此问题。在下面的示例中,我将为列表
l
生成3个元素的所有可能组合:

导入itertools >>>l=[1,2,3,4,5] >>>列表(itertools.排列(l,3)) [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), ... ... (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5), ... ... (5, 4, 2), (5, 4, 3)] 现在,如果要将这些值保存在其他文本文件中,应执行以下操作:

for i, e in enumerate(itertools.permutations(l, 3)):
    with open(f"file_{i}.txt","w+") as f:
        f.write(e)


尝试使用
random.choice()

随机导入
#你的代码
word=[]
对于范围(0,7)内的x
添加(随机选择(单词))
file.write(word)

使用for循环重复
word=[]
之后的所有内容,您甚至可以使用所述方法检查它们是否相同

@lmiguelvargasf的答案只涵盖了一半的问题,因此下面是将单词组合保存到单个文件的部分

import itertools
import random
import string

class fileNames:
    def __init__(self):
        self.file_names = []

    def randomString(self,stringLength):
        letters = string.ascii_lowercase
        file_name = ''.join(random.choice(letters) for i in range(stringLength))
        if file_name in self.file_names:
            randomString(stringLength)
        self.file_names.append(file_name)
        return file_name

# Original word list 
l = [1, 2, 3]
# Create a new list, containing the combinations
word_combinations = list(itertools.permutations(l, 3))

# Creating an instance of fileNames() class
files = fileNames()

# Specifying the number of characters
n = 5

# For each of these combinations, save in a file
for word_comb in word_combinations:
    # The file will be named by a random string containing n characters
    with open('{}.txt'.format(files.randomString(n)), 'w') as f:
            # The file will contain each word seperated by a space, change the string below as desired
            f.write('{} {} {}'.format(word_comb[0], word_comb[1], word_comb[2]))

如果希望文件名是一个整数,每个文件增加1,请将最后一部分替换为:

# For each of these combinations, save in a file
for n, word_comb in enumerate(word_combinations):
    # The file will be named by an integer
    with open('{}.txt'.format(n), 'w') as f:
            # The file will contain each word seperated by a space, change the string below as desired
            f.write('{} {} {}'.format(word_comb[0], word_comb[1], word_comb[2]))

从学习基本的python、数据结构等开始,然后找到
itertools
模块,特别是
itertools。排列
OP希望相同但位置不同的单词应被视为另一个变体。,所以
排列
是正确的选择,而不是
组合
谢谢您的回复。你能帮我修改代码,把每个组合保存到不同的文本文件吗?文件名可以随机生成…@4k3or3et,我更新了我的答案,如果这对你有用,我将非常感谢你的投票/验证。我刚刚尝试了你的方法。我得到:文件“test.py”,第6行,在itertools中的i,e.permutations(l,3):ValueError:太多的值无法解包(预期2)很酷的东西。如何创建一个文件名,使其在每次迭代中增加+1?现在它会生成一个随机字符串文件名。此外,它还添加了一个解决方案,用于以不断增加的数字命名文件