[英]Advanced List Comprehension: how can I fit multiple for loops in one list comprehension?
I have been trying to fit the following list of words from a text file into one list comprehension:我一直在尝试将文本文件中的以下单词列表放入一个列表理解中:
file = open("Lincoln.txt", "r").read().split()
world_list = []
for v in file:
word_list.append(v.translate(str.maketrans("", "",string.punctuation)).lower())
for i in word_list:
if i != '':
world_list.append(i)
This is succesful, but I'm not sure how to include the second part of the for loop in that same list comprehension:这是成功的,但我不确定如何在同一个列表理解中包含 for 循环的第二部分:
word_list = [v.translate(str.maketrans("", "",string.punctuation)).lower() for i,v in enumerate(word_list)]
Without the second part, I still get empty strings from my word extraction:没有第二部分,我仍然从我的单词提取中得到空字符串:
已编辑
[i for i in (v.translate(str.maketrans("", "",string.punctuation)).lower() for v in word_list) if i != '']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.