根据另一个列表计算嵌套列表中的元素
Count elements in nested lists based on another list
我正在尝试根据不同的单词列表计算嵌套列表中某个单词出现的次数。例如:
one = [['apple','pear','bear'],['apple','drawers','bear','grapes']]
word = ['pear','oranges','pineapple','scones','drawers']
我想计算列表单词中的每个单词在每个名为 one
的嵌套列表中出现的次数。作为我想要的输出:
new_one = [[0,1,0],[0,1,0,0]]
我尝试使用 .count
,但 .count
不使用列表中的元素,而是使用单个字符串或整数。我无法使用 for 循环来索引使用 .count()
的单词元素。对于 Counter 也是如此,它似乎不适用于嵌套列表或 for 循环。
我可以考虑使用字典,但最终我希望 new_one
成为列表的列表,因为我想稍后将 new_one 变成一个矩阵,其中一行是矩阵的一行.
one = [['apple','pear','bear'],['apple','drawers','bear','grapes']]
word = ['pear','oranges','pineapple','scones','drawers']
output = []
# create a dict and populate with keys being unique words and values being its occurances
d = {}
for x in one:
for y in x:
d[y] = word.count(y)
# go through each word in sublist and find the count from the dict
for x in ne:
output.append([d[y] for y in x])
这应该给你:
output = [[[0, 1, 0], [0, 1, 0, 0]]]
这是一个可能的方法:
[[[1 if z == x else 0 for z in y] for y in one] for x in word]
输出:
[[[0, 1, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 1, 0, 0]]]
最简单的方法是使用嵌套列表理解:
[[word.count(w) for w in l] for l in one]
这有点低效,因为它每次都会计算每个单词的出现次数(例如,它将执行 word.count('apple')
两次),但如果您的列表不是很长,那将不是问题。
首先我们迭代输出列表,即一个。对于一个列表中的每个列表,我们迭代元素,即 apple pear bear 等。如果这与列表单词匹配,那么我们将附加到临时列表 new_one_temp。在每次外部迭代中,我们附加到 new_one 列表。
one=[['apple','pear','bear'],['apple','drawers','bear','grapes']]
word=['pear','oranges','pineapple','scones','drawers']
new_one=[]
for list_elem in one:
new_one_temp=[]
for word_text in list_elem:
if word_text in word:
new_one_temp.extend([1])
else:
new_one_temp.extend([0])
new_one.append(new_one_temp)
print new_one
输出
new_one = [[0, 1, 0], [0, 1, 0, 0]]
我正在尝试根据不同的单词列表计算嵌套列表中某个单词出现的次数。例如:
one = [['apple','pear','bear'],['apple','drawers','bear','grapes']]
word = ['pear','oranges','pineapple','scones','drawers']
我想计算列表单词中的每个单词在每个名为 one
的嵌套列表中出现的次数。作为我想要的输出:
new_one = [[0,1,0],[0,1,0,0]]
我尝试使用 .count
,但 .count
不使用列表中的元素,而是使用单个字符串或整数。我无法使用 for 循环来索引使用 .count()
的单词元素。对于 Counter 也是如此,它似乎不适用于嵌套列表或 for 循环。
我可以考虑使用字典,但最终我希望 new_one
成为列表的列表,因为我想稍后将 new_one 变成一个矩阵,其中一行是矩阵的一行.
one = [['apple','pear','bear'],['apple','drawers','bear','grapes']]
word = ['pear','oranges','pineapple','scones','drawers']
output = []
# create a dict and populate with keys being unique words and values being its occurances
d = {}
for x in one:
for y in x:
d[y] = word.count(y)
# go through each word in sublist and find the count from the dict
for x in ne:
output.append([d[y] for y in x])
这应该给你:
output = [[[0, 1, 0], [0, 1, 0, 0]]]
这是一个可能的方法:
[[[1 if z == x else 0 for z in y] for y in one] for x in word]
输出:
[[[0, 1, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 0], [0, 1, 0, 0]]]
最简单的方法是使用嵌套列表理解:
[[word.count(w) for w in l] for l in one]
这有点低效,因为它每次都会计算每个单词的出现次数(例如,它将执行 word.count('apple')
两次),但如果您的列表不是很长,那将不是问题。
首先我们迭代输出列表,即一个。对于一个列表中的每个列表,我们迭代元素,即 apple pear bear 等。如果这与列表单词匹配,那么我们将附加到临时列表 new_one_temp。在每次外部迭代中,我们附加到 new_one 列表。
one=[['apple','pear','bear'],['apple','drawers','bear','grapes']]
word=['pear','oranges','pineapple','scones','drawers']
new_one=[]
for list_elem in one:
new_one_temp=[]
for word_text in list_elem:
if word_text in word:
new_one_temp.extend([1])
else:
new_one_temp.extend([0])
new_one.append(new_one_temp)
print new_one
输出
new_one = [[0, 1, 0], [0, 1, 0, 0]]