函数,它接受3个列表参数并返回所有组合

2024-04-24 23:41:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个Python函数的帮助,它接受3个参数,这些参数是列表,并且可以返回所有的组合。在

例如,如果我运行:

shirts = ['white', 'blue']
ties = ['purple', 'yellow']
suits = ['grey', 'blue']
combinations = dress_me(shirts, ties, suits)
for combo in combinations:
    print combo

它会打印如下内容:

^{pr2}$

Tags: 函数列表参数bluegreymewhitepurple
3条回答
def dress_me(l1, l2, l3):
    res = []
    for i in l1:
        for j in l2:
            for k in l3:
                res.append((i, j, k))
    return res

shirts = ['white', 'blue']
ties = ['purple', 'yellow']
suits = ['grey', 'blue']

if __name__ == '__main__':  
    combinations = dress_me(shirts, ties, suits)
    for combo in combinations:
        print(combo)
def dress_me(l1, l2, l3):
    return [(i, j, k) for i in l1 for j in l2 for k in l3]

救人。在

import itertools

def dress_me(*choices):
  return itertools.product(*choices)

相关问题 更多 >