需要从多个值创建字典
Need to create a dictionary from multiple values
我正在尝试使用以下代码创建字典:
def func(inp):
return (dict(zip(inp.keys(), values)) for values in product(*inp.values()))
x ={'Key1': ['111', '42343'], 'key2': ['TEST', 'TESTTT123'], 'Key3': ['Cell Phone', 'e-Mail'], 'Key5': ['32142341', 'test@email.com']}
func(x)
但是它给了我一个笛卡尔积
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
但是输出要求是:
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
对如何避免笛卡尔积有什么帮助吗?
只需使用 range
遍历列表的长度,并使用 dictionary comprehension 构建单个字典,每次选择列表的第 i
个元素:
def func(inp):
return ({k: v[i] for k, v in inp.items()} for i in range(len(list(inp.values())[0])))
x = {'Key1': ['111', '42343'], 'key2': ['TEST', 'TESTTT123'], 'Key3': ['Cell Phone', 'e-Mail'], 'Key4': ['32142341', 'test@email.com']}
res = func(x)
for r in res:
print(r)
输出:
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
这使用第一个 key/value 对列表的长度,并假定所有列表的长度相等。
您的解决方案非常接近,但不必要地调用了笛卡尔积函数。您可以改为直接压缩输入字典的值,以便您可以通过使用输入字典的键压缩它们来遍历它们以创建子字典:
def func(inp):
return (dict(zip(inp, values)) for values in zip(*inp.values()))
我正在尝试使用以下代码创建字典:
def func(inp):
return (dict(zip(inp.keys(), values)) for values in product(*inp.values()))
x ={'Key1': ['111', '42343'], 'key2': ['TEST', 'TESTTT123'], 'Key3': ['Cell Phone', 'e-Mail'], 'Key5': ['32142341', 'test@email.com']}
func(x)
但是它给了我一个笛卡尔积
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '111', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TEST', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'Cell Phone', 'Key4': 'test@email.com'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
但是输出要求是:
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
对如何避免笛卡尔积有什么帮助吗?
只需使用 range
遍历列表的长度,并使用 dictionary comprehension 构建单个字典,每次选择列表的第 i
个元素:
def func(inp):
return ({k: v[i] for k, v in inp.items()} for i in range(len(list(inp.values())[0])))
x = {'Key1': ['111', '42343'], 'key2': ['TEST', 'TESTTT123'], 'Key3': ['Cell Phone', 'e-Mail'], 'Key4': ['32142341', 'test@email.com']}
res = func(x)
for r in res:
print(r)
输出:
{'Key1': '111', 'Key2': 'TEST', 'Key3': 'Cell Phone', 'Key4': '32142341'}
{'Key1': '42343', 'Key2': 'TESTTT123', 'Key3': 'e-Mail', 'Key4': 'test@email.com'}
这使用第一个 key/value 对列表的长度,并假定所有列表的长度相等。
您的解决方案非常接近,但不必要地调用了笛卡尔积函数。您可以改为直接压缩输入字典的值,以便您可以通过使用输入字典的键压缩它们来遍历它们以创建子字典:
def func(inp):
return (dict(zip(inp, values)) for values in zip(*inp.values()))