Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过将一个dict列表的值作为键添加到另一个dict,将两个dict列表合并到一个dict_Python_Dictionary - Fatal编程技术网

Python 通过将一个dict列表的值作为键添加到另一个dict,将两个dict列表合并到一个dict

Python 通过将一个dict列表的值作为键添加到另一个dict,将两个dict列表合并到一个dict,python,dictionary,Python,Dictionary,我有两个dict列表: lst1= [{'st_name': 'ram', 'st_email_id': 'ram@abc.com'}, {'st_name': 'Raj', 'st_email_id': 'raj@abc.com'}, {'st_name': 'jatin', 'st_email_id': 'jatin@abc.com'},{'st_name': 'tom', 'st_email_id': 'tom@abc.com'}] lst2 = [{'team_name': 'Team

我有两个dict列表:

lst1= [{'st_name': 'ram', 'st_email_id': 'ram@abc.com'}, {'st_name': 'Raj', 'st_email_id': 'raj@abc.com'}, {'st_name': 'jatin', 'st_email_id': 'jatin@abc.com'},{'st_name': 'tom', 'st_email_id': 'tom@abc.com'}]

lst2 = [{'team_name': 'Team1'}, {'team_name': 'Team2'}]
想要这样的东西:

{
"team1":
[
    {
        'st_name': 'ram', 
        'st_email_id': 'ram@abc.com',
     
    },
    {
        'st_name': 'Raj',
        'st_email_id': 'raj@abc.com'
    }
],
"team2":
[
    {
        'st_name': 'jatin',
        'st_email_id': 'jatin@abc.com'
    },
    {
        'st_name': 'tom',
        'st_email_id': 'tom@abc.com'
    }
]
}  

(不考虑如何知道谁属于哪个团队)

您不能在python中组合/合并2个字典列表,但可以创建一个新字典,其中
lst2
content作为键,
lst1
content作为值

注意:我的结果与您要求的结果不一样,因为您需要听写器中实际需要的值

话虽如此,我的代码非常简单:

lst1 = #dict_list with the values
lst2 = #dict_list with the keys

dict_final = {}
for key in lst2:
        dict_final[key['team_name']] = lst1 #<- lst1 should be replaced by your needs

print(dict_final)

您如何知道谁属于哪个团队?另外,
Team1
如何成为
Team1
{team['team_name'].lower():团队花名册,zip中的花名册(lst2,(lst1[:2],lst2[2:])
在创建api{team['team_name']时,我正在从表映射团队信息。lower():团队花名册,zip中的花名册(lst2,(lst1[:2],lst2:])这样我们就只有一队了
{
    'Team1':
    [
        {
            'st_name': 'ram',
            'st_email_id': 'ram@abc.com'
        }, 
        {
            'st_name': 'Raj',
            'st_email_id': 'raj@abc.com'
        }, 
        {
            'st_name': 'jatin', 
            'st_email_id': 'jatin@abc.com'
        }, 
        {
            'st_name': 'tom', 
            'st_email_id': 'tom@abc.com'
        }
    ],
    'Team2': 
    [
        {
            'st_name': 'ram', 
            'st_email_id': 'ram@abc.com'
        },
        {
            'st_name': 'Raj',
            'st_email_id': 'raj@abc.com'
        }, 
        {
            'st_name': 'jatin', 
            'st_email_id': 'jatin@abc.com'
        },
        {
            'st_name': 'tom', 
            'st_email_id': 'tom@abc.com'
        }
    ]
}