从列表中查找项目的长度

时间:2010-10-10 08:32:52

标签: python arrays

我在python中有两个列表

list1=['12aa','2a','c2']

list2=['2ac','c2a','1ac']

首先 - 查找list1中每两项的组合。

第二步 - 从list2中找到每两项的组合。

第三 - 查找list1和list2

中每两项的组合

第四 - 计算每个组合的总长度

感谢Python中的建议和帮助。

谢谢

1 个答案:

答案 0 :(得分:3)

import itertools as it

list1=['12aa','2a','c2']
list2=['2ac','c2a','1ac']

# First- Finding combinations of each two item from list1.
first = list(it.combinations(list1, 2))

# Second- Finding combinations of each two item from list2.
second = list(it.combinations(list2, 2))

# Third- Finding combinations of each two items from list1 and list2
third = list(it.product(list1, list2))

# Fourth- Calculating each combinations total length
for combination in first: # first, second, third
    print combination, len(''.join(combination))
相关问题