English 中文(简体)
使用带有以值作为列表的 python 字典, 并创建一个包含初始密钥和列表值的字典新列表
原标题:Take a python dictionary with values as lists and create a new list of dictionarys each with initial key and list values
如果我有一个包含列表值的字典:用户 = {第一个名字 {第一个名字 {Jim, John, Jane }, 最后一个名字 : [Boothe, Smith, Doe ], 用户 : [100, 101, 102 ] } 我如何用初始字典将初始词典列表中的每个索引分开来创建新的词典列表? 我的意思是,根据上面的初始字典,我想创建以下的词典: 有效载 = [{第一个名字 : Jim, 最后一个名字 : Boothe, 用户 : 100} John, 最后一个名字 : 100 用户 简 : 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户: 最后一个用户:
问题回答
The below 1-liner should work. users = { first-name : [ Jim , John , Jane ], last-name : [ Boothe , Smith , Doe ], user : [ 100 , 101 , 102 ]} expected_payload = [{ first-name : Jim , last-name : Boothe , user : 100 }, { first-name : John , last-name : Smith , user : 101 }, { first-name : Jane , last-name : Doe , user : 102 } ] payload = [dict(zip(users.keys(), xs)) for xs in zip(*users.values())] assert payload == expected_payload
Assuming that all lists are always equal in length: result = [{key: value[i] for key, value in users.items()} for i in range(len(users["first-name"]))]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签