简体   繁体   English

您如何制作价值对并重复它们?

[英]How do you make value pairs and repeat them?

def cartesian_product(table1, table2):

    '''(table, table) -> table

    Return a table that is created by joining the tables table1 and table2. The
    cartesian products of the two tables is a new table where each row in the first 
    table is paired with every row in the second table. Each table is supposed to 
    be a dictionary.

    Example:

    dict1 = {'a': ['b', 'c']}
    dict2 = {'d': ['e', 'f']}
    cartesian_product({'a': ['b', 'c']}, {'d': ['e', 'f']})
    {'a': ['b', 'b', 'c', 'c'], 'd': ['e', 'f', 'e', 'f']}

    '''

    table1 = list(table1.values())
    table2 = list(table2.values())

    for object1 in table1:
       x = object1

       for object2 in table2:
          y = object2

          return x,y

This is what I have so far and I know the output is: 到目前为止,这是我所知道的,输出是:

(['b', 'c'], ['e', 'f'])

I want it to return: 我希望它返回:

{'a': ['b', 'b', 'c', 'c'], 'd': ['e', 'f', 'e', 'f']}

I can try to return the dictionary myself -- but in the list -- how do you make pairs and repeat them? 我可以尝试自己返回字典-但在列表中-您如何配对并重复配对?

Use itertools.product() to produce pairs, which you can then append to your output: 使用itertools.product()生成对,然后可以将其附加到输出中:

from itertools import product

def cartesian_product(table1, table2):
    (table1_name, table1_columns), = table1.items()
    (table2_name, table2_columns), = table2.items()
    output = {table1_name: [], table2_name: []}
    for cola, colb in product(table1_columns, table2_columns):
        output[table1_name].append(cola)
        output[table2_name].append(colb)
    return output

or you can nest the loops: 或者您可以嵌套循环:

def cartesian_product(table1, table2):
    (table1_name, table1_columns), = table1.items()
    (table2_name, table2_columns), = table2.items()
    output = {table1_name: [], table2_name: []}
    for cola in table1_columns:
        for colb in table2_columns:
            output[table1_name].append(cola)
            output[table2_name].append(colb)
    return output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅提取数组名称和括号内的内容并使它们成为键值对? - How do I extract only the array names and the content inside the parentheses and make them a key value pairs? 你如何从两列对中制作方阵? - How do you make a square matrix from two columns of pairs? 你如何使字符串在python中重复 - How do you make a string repeat itself in python 如何在pygame中制作图像并使它们移动? - How do you make images in pygame and make them move? 如何创建多个矩形并使它们独立移动 - how do you create multiple rectangles and make them move independently 我如何比较2个json文件并获取仅2个键/值对的差异并使用python打印它们 - how do I compare 2 json files and fetch the difference of only 2 key/value pairs and print them using python 如何将行划分为字典并为它们分配键值对,以及 append 每个条目的 ID? - How do I divide lines into dictionaries and assign key-value pairs to them, and append an ID for each entry? 你如何让生成器释放一个值? - How do you make a generator release a value? 如何从以键为输入,值为输出的函数中返回键值对? - How do you return key-value pairs from a function where the key is the input and the value is the output? 如何将元素列表分组到对中? - How do you group a list of elements in to pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM