从列表中查找项目的长度 [英] Finding length of items from a list

查看:57
本文介绍了从列表中查找项目的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有两个列表

I have two list in a python

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

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

首先从list1中查找每两项的组合.

First- Finding combinations of each two item from list1.

第二个-从list2中查找每两项的组合.

Second- Finding combinations of each two item from list2.

第三项-从list1和list2中查找每两项的组合

Third- Finding combinations of each two items from list1 and list2

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

Fourth- Calculating each combinations total length

赞赏Python中的建议和帮助.

Advice and help in Python is appreciated.

谢谢

推荐答案

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))

这篇关于从列表中查找项目的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆