【发布时间】:2018-10-06 04:49:43
【问题描述】:
我有这样的单词列表(这里只列出了 2 个):
list_1 = ['average', 'reasonable']
list_2 = ['fiddle', 'frolic']
list_n = ['etc', 'etc']
我希望将这两个列表相乘以获得这个答案:
obj[l1] * obj[l2] = ['average fiddle', 'average frolic', 'reasonable fiddle', 'reasnable frolic']
obj[l1] * obj[l2] *...* obj[n]
我写了这段代码:
import numpy as np
obj = {}
obj['l1'] = np.array(list_1)
obj['l2'] = np.array(list_2)
print(obj['l1']*obj['l2'])
但这只会给我一个错误:
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U10') dtype('<U10') dtype('<U10')
我该怎么办?
编辑: 尝试使用以下用户建议的 itertools:
word_list = ['fair play']
output = {'fair': ['average', 'reasonable'], 'play': ['fiddle', 'frolic']}
result = []
for words in word_list:
for word in word_tokenize(words):
list_1 = output_set[word]
result = [(x, y) for x, y in product(list_1, result)]
result = list(map(' '.join, result))
print(result)
但这只会返回一个空集。有没有办法遍历“无限”列表?
【问题讨论】:
-
不是numpy数组乘法元素吗?