如何乘以向量之类的单词列表?

时间:2018-10-06 04:49:44

标签: python list numpy

我有这样的单词列表(这里只列出了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)

但这只会返回一个空集。有没有办法遍历“无限”列表?

2 个答案:

答案 0 :(得分:1)

如果您必须使用列表:

In [86]: list_1 = ['average', 'reasonable'] 
    ...: list_2 = ['fiddle', 'frolic']
In [87]: arr1 = np.array(list_1, object)
In [88]: arr2 = np.array(list_2, object)
In [89]: np.add.outer(arr1, arr2)
Out[89]: 
array([['averagefiddle', 'averagefrolic'],
       ['reasonablefiddle', 'reasonablefrolic']], dtype=object)

通过创建对象数组,而不是字符串dtype,我迫使add ufunc使用Python字符串的+方法。如@Sandeep的答案所示,字符串添加是一个联接。字符串乘法是一个复制。

第三个数组:

In [90]: arr3 = np.array(['etc', 'etc'], object)
In [91]: np.add.outer(np.add.outer(arr1, arr2),arr3)
Out[91]: 
array([[['averagefiddleetc', 'averagefiddleetc'],
        ['averagefrolicetc', 'averagefrolicetc']],

       [['reasonablefiddleetc', 'reasonablefiddleetc'],
        ['reasonablefrolicetc', 'reasonablefrolicetc']]], dtype=object)

我正在通过链接操作来猜测您的意思。

我个人更喜欢@vash的itertools解决方案; numpy对Python的字符串处理没有多大作用。

In [105]: [' '.join(x) for x in itertools.product(arr1,arr2,arr3)]
Out[105]: 
['average fiddle etc',
 'average fiddle etc',
 'average frolic etc',
 'average frolic etc',
 'reasonable fiddle etc',
 'reasonable fiddle etc',
 'reasonable frolic etc',
 'reasonable frolic etc']

答案 1 :(得分:1)

使用 itertools.product 我们可以将它们作为.product获得,然后使用tuples创建' '.join

str