字典用法的简单问题 [英] simple question on dictionary usage

查看:86
本文介绍了字典用法的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我提前道歉,我是python的新手


说,我有一个字典,如下所示:


record = {''BAT'':'14 .4'',''USD'':'''24'',''DIF'':'''45'' ,''OAT'':''16'',

''FF'':''3.9'',''C3'':''343'',''E4'' :''1157'',''C1'':''339'',

''E6'':''1182'',''RPM'':''996'' ,''C6'':''311'',''C5'':''300'',

''C4'':''349'',''CLD'' :''0'',''E5'':''1148'',''C2'':''329'',

''MAP'':''15'' ,''OIL'':''167'',''HP'':''19'',''E1'':'''1137'',

''MARK''' :'''',''E3'':''1163'',''TIME'':''15:43:54'',

'E2'':'''1169''}


从这本词典中我想创建另一个词典标记

''ret''')所有以字母E开头的键。在

其他词中它应该是这样的:


egt = {''E6'':''1182'',''E1'':' '1137'',''E4'':''1157'',''E5'':'''1148'',

''E2'':'''1169'',' 'E3'':'''1163''}


这应该很简单,但不知怎的,我用谷歌搜索我已经找不到了b $ b提示。


提前致谢

-

Frank Stutzman



My apologies in advance, I''m new to python

Say, I have a dictionary that looks like this:

record={''BAT'': ''14.4'', ''USD'': ''24'', ''DIF'': ''45'', ''OAT'': ''16'',
''FF'': ''3.9'', ''C3'': ''343'', ''E4'': ''1157'', ''C1'': ''339'',
''E6'': ''1182'', ''RPM'': ''996'', ''C6'': ''311'', ''C5'': ''300'',
''C4'': ''349'', ''CLD'': ''0'', ''E5'': ''1148'', ''C2'': ''329'',
''MAP'': ''15'', ''OIL'': ''167'', ''HP'': ''19'', ''E1'': ''1137'',
''MARK'': '''', ''E3'': ''1163'', ''TIME'': ''15:43:54'',
''E2'': ''1169''}

From this dictionary I would like to create another dictionary calld
''egt'') that has all of the keys that start with the letter ''E''. In
otherwords it should look like this:

egt = {''E6'': ''1182'',''E1'': ''1137'',''E4'': ''1157'',''E5'': ''1148'',
''E2'': ''1169'',''E3'': ''1163''}

This should be pretty easy, but somehow with all my googling I''ve
not found a hint.

Thanks in advance
--
Frank Stutzman

推荐答案

10月26日晚上11点29分,Frank Stutzman< stutz ... @ skywagon.kjsl.com>

写道:
On Oct 26, 11:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.com>
wrote:

我提前道歉,我是python的新手


说,我有一个字典,看起来像这样:

record = {''BAT'':''14 .4'',''USD'':''24'',''DIF'':'''45'',''OAT'': ''16'',

''FF'':''3.9'', 'C3'':''343'',''E4'':''1157'',''C1'':'''339'',

''E6'':' '1182'',''RPM'':''996'',''C6'':''311'',''C5'':''300'',

' 'C4'':''349'',''CLD'':''0'',''E5'':''1148'',''C2'':''329'',
''MAP'':''15'',''OIL'':''167'',''HP'':''19'',''E1'':''1137 '',

''MARK'':'''',''E3'':''1163'',''TIME'':''15:43:54'',

''E2'':'''1169''}


从这本词典中我想创建另一个词典世界

''egt'')所有键都以字母E开头。在

其他词中它应该是这样的:


egt = {''E6'':''1182'',''E1'':' '1137'',''E4'':''1157'',''E5'':'''1148'',

''E2'':'''1169'',' 'E3'':'''1163''}


这应该很简单,但不知怎的,我用谷歌搜索我已经找不到了b $ b提示。


提前致谢


-

Frank Stutzman
My apologies in advance, I''m new to python

Say, I have a dictionary that looks like this:

record={''BAT'': ''14.4'', ''USD'': ''24'', ''DIF'': ''45'', ''OAT'': ''16'',
''FF'': ''3.9'', ''C3'': ''343'', ''E4'': ''1157'', ''C1'': ''339'',
''E6'': ''1182'', ''RPM'': ''996'', ''C6'': ''311'', ''C5'': ''300'',
''C4'': ''349'', ''CLD'': ''0'', ''E5'': ''1148'', ''C2'': ''329'',
''MAP'': ''15'', ''OIL'': ''167'', ''HP'': ''19'', ''E1'': ''1137'',
''MARK'': '''', ''E3'': ''1163'', ''TIME'': ''15:43:54'',
''E2'': ''1169''}

From this dictionary I would like to create another dictionary calld
''egt'') that has all of the keys that start with the letter ''E''. In
otherwords it should look like this:

egt = {''E6'': ''1182'',''E1'': ''1137'',''E4'': ''1157'',''E5'': ''1148'',
''E2'': ''1169'',''E3'': ''1163''}

This should be pretty easy, but somehow with all my googling I''ve
not found a hint.

Thanks in advance

--
Frank Stutzman



我认为这应该可以解决问题。可能还有比这更简洁的东西,但我现在想不到它。


egt = {}

用于记录密钥:

如果key.startswith(''E''):

egt [key] = record [key]


打印egt

{''E5'':''1148'',''E4'':''1157'',''E6'' :''1182'',''E1'':''1137'',''E3'':''1163'',

''E2'':'''1169'' }

-Edward Kozlowski

I think this should do the trick. There''s probably something more
concise than this, but I can''t think of it at the moment.

egt = {}
for key in record:
if key.startswith(''E''):
egt[key] = record[key]

print egt
{''E5'': ''1148'', ''E4'': ''1157'', ''E6'': ''1182'', ''E1'': ''1137'', ''E3'': ''1163'',
''E2'': ''1169''}
-Edward Kozlowski


10月26日晚上9:29,Frank Stutzman< stutz ... @ skywagon .kjsl.comwrote:
On Oct 26, 9:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.comwrote:

我提前道歉,我是蟒蛇新手


说,我有一个字典看起来像这样:


record = {''BAT'':''14 .4'',''USD'':'''24'',''DIF'' :''45'',''OAT'':'''16'',

''FF'': ''3.9'',''C3'':''343'',''E4'':''1157'',''C1'':'''339'',

''E6'':''1182'',''RPM'':''996'',''C6'':''311'',''C5'':''300'',

''C4'':''349'',''CLD'':''0'',''E5'':'''1148'',''C2'':'' 329'',

''MAP'':''15'',''OIL'':''167'',''HP'':''19'','' E1'':'''1137'',

''MARK'':'''',''E3'':'''1163'',''TIME'':''15 :43:54'',

''E2'':'''1169''}


从这本词典我想创建另一个字典calld

''egt'')所有键都以字母E开头。在

其他词中它应该是这样的:


egt = {''E6'':''1182'',''E1'':' '1137'',''E4'':''1157'',''E5'':'''1148'',

''E2'':'''1169'',' 'E3'':'''1163''}


这应该很简单,但不知怎的,我用谷歌搜索我已经找不到了b $ b暗示。
My apologies in advance, I''m new to python

Say, I have a dictionary that looks like this:

record={''BAT'': ''14.4'', ''USD'': ''24'', ''DIF'': ''45'', ''OAT'': ''16'',
''FF'': ''3.9'', ''C3'': ''343'', ''E4'': ''1157'', ''C1'': ''339'',
''E6'': ''1182'', ''RPM'': ''996'', ''C6'': ''311'', ''C5'': ''300'',
''C4'': ''349'', ''CLD'': ''0'', ''E5'': ''1148'', ''C2'': ''329'',
''MAP'': ''15'', ''OIL'': ''167'', ''HP'': ''19'', ''E1'': ''1137'',
''MARK'': '''', ''E3'': ''1163'', ''TIME'': ''15:43:54'',
''E2'': ''1169''}

From this dictionary I would like to create another dictionary calld
''egt'') that has all of the keys that start with the letter ''E''. In
otherwords it should look like this:

egt = {''E6'': ''1182'',''E1'': ''1137'',''E4'': ''1157'',''E5'': ''1148'',
''E2'': ''1169'',''E3'': ''1163''}

This should be pretty easy, but somehow with all my googling I''ve
not found a hint.



一种可能的解决方案(如果您不熟悉,请阅读列表 - 理解

):

One possible solution (read list-comprehension if you not familiar
with it):


>> record = {''BAT'':''14 .4'',''USD'':''24 '',''DIF'':''45'',''OAT'':''16'',
>>record={''BAT'': ''14.4'', ''USD'': ''24'', ''DIF'': ''45'', ''OAT'': ''16'',



....''FF'':''3.9'',''C3'':''343'',''E4'':'''1157'',''C1'':''339 '',

....'''E6'':'''1182'',''RPM'':''996'',''C6'':''311'' ,''C5'':''300'',

....''C4'':''349'',''CLD'':''0'',' 'E5'':''1148'',''C2'':''329'',

....''MAP'':'''15'',''OIL '':'''167'',''惠普'':''19'',''E1'':''1137'',

....''MARK'':''''''''E3'': ''1163'',''时间':''15:43:54'',

....''E2'':''1169''}

.... ''FF'': ''3.9'', ''C3'': ''343'', ''E4'': ''1157'', ''C1'': ''339'',
.... ''E6'': ''1182'', ''RPM'': ''996'', ''C6'': ''311'', ''C5'': ''300'',
.... ''C4'': ''349'', ''CLD'': ''0'', ''E5'': ''1148'', ''C2'': ''329'',
.... ''MAP'': ''15'', ''OIL'': ''167'', ''HP'': ''19'', ''E1'': ''1137'',
.... ''MARK'': '''', ''E3'': ''1163'', ''TIME'': ''15:43:54'',
.... ''E2'': ''1169''}


>> egt = dict([(k,record [k])for k in record if k.startswith(''E'')])
egt
>>egt = dict([(k, record[k]) for k in record if k.startswith(''E'')])
egt



{''E5'':'' 1148'',''E4'':'''1157'',''E6'':''1182'',''E1'':'''1137'',''E3'':''1163' ',

''E2'':''1169''}

Karthik

{''E5'': ''1148'', ''E4'': ''1157'', ''E6'': ''1182'', ''E1'': ''1137'', ''E3'': ''1163'',
''E2'': ''1169''}

Karthik


>

提前致谢


-

Frank Stutzman
>
Thanks in advance

--
Frank Stutzman



代表Edward Kozlowski
On Behalf Of Edward Kozlowski

我认为这应该可以解决问题。可能有点比这更简洁的东西了,但我现在想不到它。


egt = {}

用于记录密钥:

如果key.startswith(''E''):

egt [key] = record [key]
I think this should do the trick. There''s probably something
more concise than this, but I can''t think of it at the moment.

egt = {}
for key in record:
if key.startswith(''E''):
egt[key] = record[key]



不是更简洁,但另一种方式:


egt = dict((key,record [key])

用于记录密钥

如果key.startswith(''E''))


问候,

Ryan Ginstrom

Not much more concise, but another way:

egt = dict((key, record[key])
for key in record
if key.startswith(''E''))

Regards,
Ryan Ginstrom


这篇关于字典用法的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆