从列表组合生成Python字典

时间:2019-09-29 03:17:26

标签: python pandas list dictionary combinations

我试图解决我的问题,但我做不到。

我有三个Python列表:

atr = ['a','b','c']
m = ['h','i','j']
func = ['x','y','z']

我的问题是基于这三个列表的组合来生成Python字典:

所需的输出:

py_dict = {
    'a': [('x','h'), ('x','i'), ('x','j'), ('y','h'), ('y','i'), ('y','j'),('z','h'), ('z','i'), ('z','j')],
    'b': [('x','h'), ('x','i'), ('x','j'), ('y','h'), ('y','i'), ('y','j'),('z','h'), ('z','i'), ('z','j')],
    'c': [('x','h'), ('x','i'), ('x','j'), ('y','h'), ('y','i'), ('y','j'),('z','h'), ('z','i'), ('z','j')]
}

1 个答案:

答案 0 :(得分:7)

您可以使用df['time'] = pd.to_timedelta('00:' + df['time']) df['total seconds'] = df['time'].dt.total_seconds()

itertools.product

输出:

import itertools
atr = ['a','b','c']
m = ['h','i','j']
func = ['x','y','z']
prod = list(itertools.product(func, m))
result = {i:prod for i in atr}