如何使用for循环初始化包含多组元素的列表?

时间:2015-05-25 07:16:06

标签: python list for-loop

我创建了一个列表如下:

possible_team = [(t, h, dps1, dps2, dps3) 
                for t in all_T 
                for h in all_H
                for dps1 in all_DPS
                for dps2 in all_DPS
                for dps3 in all_DPS]

但如果列表中有更多元素,例如2t,6h和17dps,我怎样才能避免编写所有元素名称?如:

possible_team = [(t1, t2, h1, h2, ... h5, dps1, dps2, dps3 ... dps17) 
                for t1 in all_T
                for t2 in all_T
                ...
                for dps1 in all_DPS
                for dps2 in all_DPS
                ...
                for dps17 in all_DPS]

我曾尝试将密钥写为(t)* 2,(h)* 5和(dps)* 17:

possible_team = [([t] * 2, [h] * 5, [dps] * 17) 
            for t in all_T 
            for h in all_H
            for dps in all_DPS]

但它们会返回重复值,并且每个都在列表中。

是否有任何简单的方法可以将列表作为[t1,t2,h1,h2,... h5,dps1,dps2,... dps17],每个元素都在一个循环中?

1 个答案:

答案 0 :(得分:1)

怎么样?
possible_team = all_T + all_H + all_DPS

? (不确定我是否理解你的问题。)