简体   繁体   English

结合三个不同的字典列表集合,在 python 中的键名“firstname”和“lastname”中具有相同的值

[英]Combining three different list collection of dictionary having same value in key name “firstname” and “lastname” in python

I have three different list collection of dictionary as shown all three have same "firstname" and lastname". I need to combine this list in a copy of one without replicating the firstname and lastname, ie for each firstname and lastname a combination of the other three list collection of dictionary:我有三个不同的字典列表集合,如图所示,所有三个都有相同的“名字”和姓氏。我需要将这个列表合并到一个副本中,而不复制名字和姓氏,即对于每个名字和姓氏,另一个的组合字典的三个列表集合:

list one列举一个

[{'First Name': 'Justin',
  'lastName': 'Walker',
  'Age (Years)': '29',
  'Sex': 'Male',
  'Vehicle Make': 'Toyota',
  'Vehicle Model': 'Continental',
  'Vehicle Year': '2012',
  'Vehicle Type': 'Sedan'},
 {'First Name': 'Maria',
  'lastName': 'Jones',
  'Age (Years)': '66',
  'Sex': 'Female',
  'Vehicle Make': 'Mitsubishi',
  'Vehicle Model': 'Yukon XL 2500',
  'Vehicle Year': '2014',
  'Vehicle Type': 'Van/Minivan'},
 {'First Name': 'Samantha',
  'lastName': 'Norman',
  'Age (Years)': '19',
  'Sex': 'Female',
  'Vehicle Make': 'Aston Martin',
  'Vehicle Model': 'Silverado 3500 HD Regular Cab',
  'Vehicle Year': '1995',
  'Vehicle Type': 'SUV'}

list two列举两个

[{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': 71,
  'iban': 'GB43YKET96816855547287',
  'credit_card_number': '2221597849919620',
  'credit_card_security_code': '646',
  'credit_card_start_date': '03/18',
  'credit_card_end_date': '06/26',
  'address_main': '462 Marilyn radial',
  'address_city': 'Lynneton',
  'address_postcode': 'W4 0GW'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': 91,
  'iban': 'GB53QKRK45175204753504',
  'credit_card_number': '4050437758955103343',
  'credit_card_security_code': '827',
  'credit_card_start_date': '11/21',
  'credit_card_end_date': '01/27',
  'address_main': '366 Brenda radial',
  'address_city': 'Ritafurt',
  'address_postcode': 'NE85 1RG'}]

list three列出三个

{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': '64',
  'sex': 'Male',
  'retired': 'False',
  'dependants': '2',
  'marital_status': 'single',
  'salary': '56185',
  'pension': '0',
  'company': 'Hudson PLC',
  'commute_distance': '14.1',
  'address_postcode': 'G2J 0FH'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': '69',
  'sex': 'Female',
  'retired': 'False',
  'dependants': '1',
  'marital_status': 'divorced',
  'salary': '36872',
  'pension': '0',
  'company': 'Wall, Reed and Whitehouse',
  'commute_distance': '10.47',
  'address_postcode': 'TD95 7FL'}

This is what I trying but这就是我正在尝试的,但是

for i in range(0,2):
    dict1 = list_one[i]
    dict2 = list_two[i]
    dict3 = list_three[i]
    combine_file = list_three.copy()
    for k, v in dict1.items():
        if k == "firstname" or "lastname":
            for k1, v1 in combine_file.items():
                if dict1.get(k) == combine_file.v1:                
            

This is what I'm expecting print(combine_file)这就是我所期待的 print(combine_file)

{'firstName': 'Justin',
  'lastName': 'Walker',
  'age': '64',
  'sex': 'Male',
  'retired': 'False',
  'dependants': '2',
  'marital_status': 'single',
  'salary': '56185',
  'pension': '0',
  'company': 'Hudson PLC',
  'commute_distance': '14.1',
  'iban': 'GB43YKET96816855547287',
  'credit_card_number': '2221597849919620',
  'credit_card_security_code': '646',
  'credit_card_start_date': '03/18',
  'credit_card_end_date': '06/26',
  'address_main': '462 Marilyn radial',
  'address_city': 'Lynneton',
  'address_postcode': 'W4 0GW',
  'Vehicle Make': 'Mitsubishi',
  'Vehicle Model': 'Yukon XL 2500',
  'Vehicle Year': '2014',
  'Vehicle Type': 'Van/Minivan'},
 {'firstName': 'Maria',
  'lastName': 'Jones',
  'age': '69',
  'sex': 'Female',
  'retired': 'False',
  'dependants': '1',
  'marital_status': 'divorced',
  'salary': '36872',
  'pension': '0',
  'company': 'Wall, Reed and Whitehouse',
  'commute_distance': '10.47',
  'iban': 'GB53QKRK45175204753504',
  'credit_card_number': '4050437758955103343',
  'credit_card_security_code': '827',
  'credit_card_start_date': '11/21',
  'credit_card_end_date': '01/27',
  'address_main': '366 Brenda radial',
  'address_city': 'Ritafurt',
  'address_postcode': 'NE85 1RG',
  'Vehicle Make': 'Aston Martin',
  'Vehicle Model': 'Silverado 3500 HD Regular Cab',
  'Vehicle Year': '1995',
  'Vehicle Type': 'SUV'}

Python's dict.update() functionality might be what you are looking for. Python 的dict.update()功能可能正是您正在寻找的。

For example:例如:

dict1 = { 'a' : 0,
          'b' : 1,
          'c' : 2}

dict2 = { 'c' : 0,
          'd' : 1,
          'e' : 2}

dict2.update(dict1)

dict2 is now: dict2现在是:

{'a' : 0, 'b': 1, 'c': 2, 'd' 1, 'e': 2}

Notice how 'c' was overwritten with the updated value from dict1 .请注意'c'是如何被dict1的更新值覆盖的。

You can't update together dictionaries from different people, but if you run through your lists beforehand you could compile sets of dictionaries where each set belongs to one person.你不能一起更新来自不同人的词典,但如果你事先遍历你的列表,你可以编译一组词典,其中每组属于一个人。

You can create a new dictionary, called people , and then iterate through your lists of dictionaries and extract the person's name from those dictionaries and turn it into a key in the new "people" dictionary.您可以创建一个名为people的新字典,然后遍历您的字典列表并从这些字典中提取人名并将其转换为新"people"字典中的键。

If that person's name is not in people yet, you can add that dictionary, so that people[name] points to that dictionary.如果那个人的名字还没有在people中,你可以添加那个字典,这样 people[name] 就指向那个字典。

If people[name] does exist, then you can use the people[name].update() function on the new dictionary to add the new values.如果people[name]确实存在,那么您可以在新字典上使用people[name].update()函数来添加新值。

After this process, you will have a dictionary whose keys are the names of people and the values point to a dictionary containing those people's attributes.在此过程之后,您将拥有一个字典,其键是人名,值指向包含这些人的属性的字典。

Create a new dictionary keyed on a composite of either 'firstname_lastname' or 'First Name_lastname' then you can do this:创建一个以 'firstname_lastname' 或 'First Name_lastname' 组合为关键字的新字典,然后你可以这样做:

master = {}

for _list in list_1, list_2, list_3:
    for d in _list:
        if not (firstname := d.get('firstName')):
            firstname = d['First Name']
        name_key = f'{firstname}_{d["lastName"]}'
        for k, v in d.items():
            master.setdefault(name_key, {})[k] = v

print(list(master.values()))

暂无
暂无

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

相关问题 在列表中将姓氏,名字切换为姓氏 - Switch Lastname, Firstname to Firstname Lastname inside List 在python中使用相同的键但不同的值更新字典/列表 - Update dictionary/list with same key but different value in python 在python中组合三个不同的列表 - Combining three different list in python 如何将LastName,FirstName格式的字符串名称解析为“Firstname”,“LastName”格式的列表? - How to parse a name that is a string in LastName, FirstName format into a list in 'Firstname', 'LastName' format? 改善CSV名称解析的Python解决方案:姓氏,姓氏到姓氏 - Improving My Python Solution for CSV Name Parsing: lastname, firstname to firstname lastname 将字典附加到具有不同值相同键的列表 - Appending dictionary to list with different value same key Python并翻转姓氏,名字的顺序 - Python and flipping the order of lastname, firstname 在 python pandas dataframe 中将具有“姓氏,名字”的列数据更改为“名字姓氏” - changing column data with “lastname, firstname” to “firstname lastname” in python pandas dataframe 为什么在将字典列表与相同的键合并时合并键错误 - Why key error is combining while combining List of dictionary with same keys 如何创建字典外的列表,其名称与字典键相同,并且大小与该键的值相同? - How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM