从字典中提取多个项目的语法 [英] Syntax for extracting multiple items from a dictionary
问题描述
row = {" fname" :Frank,lname; :琼斯,城市和城市。 :Hoboken,state和Hoboken。 :
" Alaska"}
cols =(" city"," state")
有没有要求只包含键的对象的最佳实践方法
在cols中命名为行?换句话说,要得到这个:
{" city" :Hoboken,state和Hoboken。 :Alaska}
谢谢,
鲨鱼
shark写道:row = {" fname" :Frank,lname; :琼斯,城市和城市。 :Hoboken,state和Hoboken。 :
Alaska}
cols =(" city",state)
是否有一种最佳实践方式来请求只包含钥匙
以cols的名字命名?换句话说,要得到这个:
{" city" :Hoboken,state和Hoboken。 :阿拉斯加}
为什么需要这样做?你不能对第二个
字典做什么,你不能做第一个,所以离开
额外的项目有什么问题到位?
在文章< Tu ******************** @ rcn.net>," ;鲨" < no@spam.none>
写道:
row = {" fname" :Frank,lname; :琼斯,城市和城市。 :Hoboken,state和Hoboken。 :
Alaska}
cols =(" city",state)
是否有一种最佳实践方式来请求只包含钥匙
以cols的名字命名?换句话说,要得到这个:
{" city" :Hoboken,state和Hoboken。 :阿拉斯加}
谢谢,
鲨鱼
出于好奇,你为什么想要?要做到这一点?你是否想要节省内存来存储很多这些东西?如果是这样,我怀疑你会更好地(每个对象几个字节)来存储
值的元组,即(Hoboken, 阿拉斯加),并根据需要打开它们。
但是,为了回答你的问题,我不知道有什么标准方法可以做到
你想要什么。写起来很容易(生产版本可能会想要在for循环中捕获KeyError'):
def getDictionarySlice( row,cols):
slice = {}
用于cols中的键:
slice [key] = row [key]
返回切片
这不会起作用,但如果确实如此会很酷:
def getOmnicientDictionarySlice(row,cols):
for key not cols:
del row [key]
嗯。也许那里有个愚人节PEP :-)
实际上,你可以这样做:
def deleteUnwantedKeysInPlace(row,cols):
for row in row.keys():
如果密钥不在cols中:
del row [key]
shark schrieb:row = {" fname" :Frank,lname; :琼斯,城市和城市。 :Hoboken,state和Hoboken。 :
Alaska}
cols =(" city",state)
是否有一种最佳实践方式来请求只包含钥匙
以cols的名字命名?换句话说,要得到这个:
{" city" :Hoboken,state和Hoboken。 :Alaska}
未经测试:
dict((键,值)for(key,value)in row.iteritems()if cols中的键)
在Py2.4中工作
row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
"Alaska"}
cols = ("city", "state")
Is there a best-practices way to ask for an object containing only the keys
named in cols out of row? In other words, to get this:
{"city" : "Hoboken", "state" : "Alaska"}
Thanks,
shark
shark wrote:row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
"Alaska"}
cols = ("city", "state")
Is there a best-practices way to ask for an object containing only the keys
named in cols out of row? In other words, to get this:
{"city" : "Hoboken", "state" : "Alaska"}
Why would you need to do that? There''s nothing you can do to the second
dictionary that you can''t do to the first, so what''s wrong with leaving
the extra items in place?
In article <Tu********************@rcn.net>, "shark" <no@spam.none>
wrote:
row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
"Alaska"}
cols = ("city", "state")
Is there a best-practices way to ask for an object containing only the keys
named in cols out of row? In other words, to get this:
{"city" : "Hoboken", "state" : "Alaska"}
Thanks,
shark
Just out of curiosity, why would you want to do that? Are you trying to
save on memory to store a lot of these things? If so, I suspect you''d
do even better (a few bytes per object) to store tuples of just the
values, i.e. ("Hoboken", "Alaska"), and unpack them as you need them.
But, to answer your question, I don''t know of any standard way to do
what you want. It''s easy enough to write (a production version would
probably want to catch KeyError''s inside the for loop):
def getDictionarySlice (row, cols):
slice = {}
for key in cols:
slice[key] = row[key]
return slice
This won''t work, but it would be kind of cool if it did:
def getOmnicientDictionarySlice (row, cols):
for key not in cols:
del row[key]
Hmmm. Maybe there''s an April Fools PEP in there somewhere :-)
Actually, you could do:
def deleteUnwantedKeysInPlace (row, cols):
for key in row.keys():
if key not in cols:
del row[key]
shark schrieb:row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
"Alaska"}
cols = ("city", "state")
Is there a best-practices way to ask for an object containing only the keys
named in cols out of row? In other words, to get this:
{"city" : "Hoboken", "state" : "Alaska"}
Untested:
dict( (key,value) for (key,value) in row.iteritems() if key in cols )
Works in Py2.4
Stefan
这篇关于从字典中提取多个项目的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!