从Python中的字符串列表中获取连接字符串

时间:2012-10-09 07:23:04

标签: python

我有一个列表列表和一个像这样的分隔符字符串:

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',
]

结果中的顺序无关紧要。

我该怎么做?

5 个答案:

答案 0 :(得分:16)

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

itertools.product返回一个迭代器,它生成所提供的iterables的笛卡尔积。我们需要map str对结果元组进行处理,因为有些值是整数。最后,我们可以加入字符串化的元组并将整个内容抛出一个列表解析(如果处理大型数据集,则生成器表达式,并且只需要迭代)。

答案 1 :(得分: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生成器。我有这些在我的控制台中查看结果,但这里并不需要它们。

答案 2 :(得分: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']

答案 3 :(得分: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]]

答案 4 :(得分: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']
````````````````````````````