📜  Python - 将元组列表分组到字典

📅  最后修改于: 2022-05-13 01:55:14.042000             🧑  作者: Mango

Python - 将元组列表分组到字典

给定一个元组列表,任务是编写一个Python程序来将字典放入这个元组。

Example:

Input: [(1, 2, 3, 4, 5, 6), (1, 4), 
        (3, 5, 3, 4, 5, 6), (5, 7),
        (5, 6), (4, 4)]
Output:{1: [2, 3, 4, 5, 6, 4], 3: (5, 3, 4, 5, 6), 5: [7, 6], 4: [4]}

示例 1:

如图所示创建一个元组列表。然后这里的任务是将元组列表分组到字典中。为此,创建一个空字典,如图所示。然后遍历元组列表以将它们分组到字典中。

Python3
# create a list of tuples.
test = [(1, 2), (1, 4), (3, 5), (5, 7)]
 
# create a empty dictionary name new_dict
new_dict = {}
 
# a for loop to iterate the list of tuples
for i in test:
   
    # checking whether the dictionary
    # has the key already.
    # If it returns No, a new Key is created.
    # If the key is not present the first
    # value of the tuple is made as the key
    if new_dict.get(i[0], 'No') == 'No':
       
      # THe remaining element is made
      # as values of the key
        new_dict[i[0]] = i[1:]
    else:
      # If the key is already present assign
      # the remaining element as values to
      # the corresponding key
        new_dict[i[0]] = new_dict.get(i[0]) + i[1:]
print(new_dict)


Python3
# create a list of tuples
test = [(1, 2, 3, 4, 5, 6), (1, 4),
        (3, 5, 3, 4, 5, 6), (5, 7),
        (5, 6), (4, 4)]
 
# create a empty dictionary name new_dict
new_dict = {}
 
# a for loop to iterate the list of tuples
for i in test:
   
    # checking whether the dictionary
    # has the key already.
    # If it returns No, a new Key is created.
    # If the key is not present the first
    # value of the tuple is made as the key
    if new_dict.get(i[0], 'No') == 'No':
       
          # THe remaining element is
        # made as values of the key
        new_dict[i[0]] = i[1:]
    else:
       
          # If the key is already present assign
        # the remaining element as values to
        # the corresponding key
          # add the values to the tuples and them
        # convert them to list using list() function
        new_dict[i[0]] = list(new_dict.get(i[0]) + i[1:])
 
# if the length of value is 1,
# it would be still as tuple,
# so convert them as list using list() function
for k, v in new_dict.items():
    if len(v) == 1:
        new_dict[k] = list(v)
print(new_dict)


输出
{1: (2, 4), 3: (5,), 5: (7,)}

正如您在输出中看到的那样,元组列表被分组到一个字典中,其中的值仍然是元素的元组。

示例 2:

如果您希望字典的值是一个列表,那么,创建一个新的元组列表。任务增益是将元组列表分组到字典中。为此,让我们创建一个空字典,如图所示。在这里,字典值必须是一个列表。值中具有单个元素的键值对仍将作为元组,因此将它们转换为列表。

有关详细说明,请查看代码以及下面的注释。

Python3

# create a list of tuples
test = [(1, 2, 3, 4, 5, 6), (1, 4),
        (3, 5, 3, 4, 5, 6), (5, 7),
        (5, 6), (4, 4)]
 
# create a empty dictionary name new_dict
new_dict = {}
 
# a for loop to iterate the list of tuples
for i in test:
   
    # checking whether the dictionary
    # has the key already.
    # If it returns No, a new Key is created.
    # If the key is not present the first
    # value of the tuple is made as the key
    if new_dict.get(i[0], 'No') == 'No':
       
          # THe remaining element is
        # made as values of the key
        new_dict[i[0]] = i[1:]
    else:
       
          # If the key is already present assign
        # the remaining element as values to
        # the corresponding key
          # add the values to the tuples and them
        # convert them to list using list() function
        new_dict[i[0]] = list(new_dict.get(i[0]) + i[1:])
 
# if the length of value is 1,
# it would be still as tuple,
# so convert them as list using list() function
for k, v in new_dict.items():
    if len(v) == 1:
        new_dict[k] = list(v)
print(new_dict)
输出
{1: [2, 3, 4, 5, 6, 4], 3: (5, 3, 4, 5, 6), 5: [7, 6], 4: [4]}