12

我有一个列表列表和一个分隔符字符串,如下所示:

lists = [
    ['a', 'b'],
    [1, 2],
    ['i', 'ii'],
]
separator = '-'

结果,我希望将字符串列表与子列表中的字符串中的分隔符字符串相结合:

result = [
    'a-1-i', 
    'a-1-ii', 
    'a-2-i', 
    'a-2-ii',
    'b-1-i', 
    'b-1-ii', 
    'b-2-i', 
    'b-2-ii',
]

结果中的顺序无关紧要。

我怎样才能做到这一点?

4

5 回答 5

17
from itertools import product
result = [separator.join(map(str,x)) for x in product(*lists)]

itertools.product返回一个迭代器,该迭代器产生提供的可迭代对象的笛卡尔积。我们需要map str遍历结果元组,因为其中一些值是整数。最后,我们可以加入字符串化的元组并将整个内容放入列表推导式(或生成器表达式,如果处理大型数据集,您只需要它进行迭代)。

于 2012-10-09T07:29:55.220 回答
3
>>> from itertools import product
>>> result = list(product(*lists))
>>> result = [separator.join(map(str, r)) for r in result]
>>> result
['a-1-i', 'a-1-ii', 'a-2-i', 'a-2-ii', 'b-1-i', 'b-1-ii', 'b-2-i', 'b-2-ii']

正如@jpm 指出的那样,您实际上并不需要强制list转换为product生成器。我有这些可以在我的控制台中查看结果,但这里并不真正需要它们。

于 2012-10-09T07:31:29.790 回答
3

您可以使用内置函数执行此操作:

>>> map(separator.join, reduce(lambda c,n: [a+[str(b)] for b in n for a in c], lists, [[]]))
['a-1-i', 'b-1-i', 'a-2-i', 'b-2-i', 'a-1-ii', 'b-1-ii', 'a-2-ii', 'b-2-ii']
于 2012-10-09T11:46:29.167 回答
1
["%s%c%s%c%s" % (a, separator, b, separator, c) for a in lists[0] for b in lists[1] for c in lists[2]]
于 2012-10-09T07:32:22.287 回答
0
from _ast import List
from itertools import product
import itertools


list1 = [
    ['1', '2', '3'], 
    ['4', '5', '6'], 
    ['7', '8', '9']
]
result = list(map(''.join, product(*list1)))
for s in result:
    print(repr(s))
    
print('............')   
    
for s in map(''.join, product(*list1)):
    print(repr(s))  

print('............')
res = [*map(''.join,product(*list1))]
print(res)
print('............')


ra = list(map(''.join, product(*list1)))
print(ra)

print('............')
for element in itertools.product(*list1):
    print(element)

#If there is a separator 
separator = '-'

#Then,

res2 = [ separator.join(map(str,r))  for r in product(*list1) ]
print(res2)
``````````````````````````

The Output :

```````````````````

['147', '148', '149', '157', '158', '159', '167', '168', '169', '247', '248', '249', '257', '258', '259', '267', '268', '269', '347', '348', '349', '357', '358', '359', '367', '368', '369']
............
['147', '148', '149', '157', '158', '159', '167', '168', '169', '247', '248', '249', '257', '258', '259', '267', '268', '269', '347', '348', '349', '357', '358', '359', '367', '368', '369']
............
('1', '4', '7')
('1', '4', '8')
('1', '4', '9')
('1', '5', '7')
('1', '5', '8')
('1', '5', '9')
('1', '6', '7')
('1', '6', '8')
('1', '6', '9')
('2', '4', '7')
('2', '4', '8')
('2', '4', '9')
('2', '5', '7')
('2', '5', '8')
('2', '5', '9')
('2', '6', '7')
('2', '6', '8')
('2', '6', '9')
('3', '4', '7')
('3', '4', '8')
('3', '4', '9')
('3', '5', '7')
('3', '5', '8')
('3', '5', '9')
('3', '6', '7')
('3', '6', '8')
('3', '6', '9')
['1-4-7', '1-4-8', '1-4-9', '1-5-7', '1-5-8', '1-5-9', '1-6-7', '1-6-8', '1-6-9', '2-4-7', '2-4-8', '2-4-9', '2-5-7', '2-5-8', '2-5-9', '2-6-7', '2-6-8', '2-6-9', '3-4-7', '3-4-8', '3-4-9', '3-5-7', '3-5-8', '3-5-9', '3-6-7', '3-6-8', '3-6-9']
````````````````````````````
于 2021-01-18T08:34:46.240 回答