我有3个文本文件 List1.txt List2.txt List3.txt
他们每个人有3,000个单词 每个词都换一行
我需要从每个单词中选择一个随机单词,然后将它们串在一起。 一旦尝试了该随机解决方案,就无需再次使用该字符串。
我会输入示例代码,但是我不知道从哪里开始。
感谢所有人
答案 0 :(得分:0)
读取文件,存储单词,然后使用numpy.random.choice从所述构造的单词列表/单词集中进行选择
import numpy as np
# your files
file_paths = ["List1.txt", "List2.txt", "List3.txt"]
# list to store all the words you have
words = [] # should be a set for unique words
for path in file_path:
# open each fine
with open(path, 'r') as f:
# for each line (containing a word):
# strip the word (meaning removing trailing whitespaces and line returns)
# and add this list of words to the existing one
words += [w.strip() for w in f.readlines() if w]
# if using sets: words |= set(w.strip() for w in f.readlines() if w)
# if you want to chose from unique words, consider using: words = set(words)
# this means all words have equal probability of being chosen
# otherwise repetition skews said choice
# chose N random words
N = 100
random_words = np.random.choice(words, size=N, replace=False)