嵌套循环以搜索CSV中的列表内容



我在这个问题上已经有一段时间了(假设我的订单/逻辑关闭(

我有一个csv文件(words.csv(,它有一个数千个单词的列表我有一个凯撒密码,它给了我一个句子所有可能排列的列表,并将其存储在另一个变量(解码格式(中,我将其剥离为单词(纯单词(

*我想做的是:在words.csv文件中搜索我的单词,如果它们存在,请将其打印为

*我目前得到的是:这些单词正在搜索和匹配,但它们是根据words.csv文件按字母顺序排列的这是有问题的,因为最终消息的顺序是错误的。。。

例如,我得到的消息输出为"apple hello tattoo yellow"而不是"hello tattoo apple yellow"

有什么想法吗?

代码:

words.csv文件类似于:

按字母顺序排序的

apple,
hello,
tattoo,
yellow,

purewords列表类似于:

根据用户输入随机订购

[hello,tattoo,apple,yellow,ifmmp,ubuupp,bqqmf,zfmmpx,jgnnq,vcvvqq,crrng,agnnqy]
def seekanddecrypt():
results=[]
pureword=[words for segments in pureword for words in segments.split()]
print(pureword)
with open('words.csv') as words:
reader = csv.reader(words)
#updated per Thomas answer below           
for row in reader:
for x in pureword:
if x in row:
print('Found the word: {}'.format(row))
results.append(row)
print("Your message contains the words: ", results)

#working attempt
for row in reader:
if row[0] in pureword:
print('Found the word: {}'.format(row))
results.append(row)
print("Your message contains the words: ",results)

顺序不正确。你正在阅读你的文本好几次,每次都只检查你的"纯单词";列表

def seekanddecrypt():
results=[]
pureword=[words for segments in pureword for words in segments.split()]
print(pureword)
with open('words.csv') as words:
reader = csv.reader(words)
#my other attempt for review            
for row in reader:
for x in pureword:
if x in row:
print('Found the word: {}'.format(row))
results.append(row)
print("Your message contains the words: ", results)

最新更新