将列表转换为dict的问题 [英] problem of converting a list to dict

查看:122
本文介绍了将列表转换为dict的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨好朋友


我有一个这样的清单


mylist = ['''',''tom = boss'' ,''mike = manager'',''paul = employee'',''意思是无意义'']


我想删除第一项和最后一项他们是不成文的,

并将其兑换成字典:

{''tom'':''boss'',''mike'':''manager' ',''paul'':''员工''}

我试过这个但是没办法:


mydict = {}
$ m $ b for m in mylist [1:-1]:

a = i.split(''='')#这会将每个mylist项目分成一个2项

列表

mydict [a [0]] = a [1]


我得到了这个:

文件" srch",第19行,< module>

grab(" a / tags1")

文件" srch",第15行,在抓取

mydict [mylist [0]] = mylist [1]

IndexError:列表索引超出范围

任何人都可以请给我一个亮点?


谢谢

解决方案

2008年1月9日星期三10:56 :36 -0800,Louis.Soninhu写道:


嗨好朋友


我有这样的清单


mylist = ['''',''tom = boss'',''mike = manager'',''paul = employee'',''表示无意义'']


我想删除第一个和最后一个项目,因为它们是不可能的,

并将其转换为dict:

{''tom'':''老板'',''迈克'':''经理'',''保罗'':''员工''}


我尝试了这个,但它没有用:


mydict = {}
$ m $ b for i in mylist [1:-1]:

a = i.split(''='')#这将把每个mylist项目分成两个项目

列表

mydict [a [0] ] = a [1]


我得到了这个:

文件" srch",第19行,< module>
抓取(" a / tags1")

文件" srch",第15行,抓住

mydict [mylist [0]] = mylist [1]

IndexError:列表索引超出范围


任何人都可以对此有所了解?



您使用的真实列表中至少有一个字符串,其中没有''=''。上面给出的

列表并没有引起异常:


在[102]中:mylist = ['''',''tom = boss '',''mike = manager'',''paul = employee'',''意思是无意义'']


在[103]中:mydict = {}


在[104]中:对于我在mylist [1:-1]:

.....:a = i.split(''='' )

.....:mydict [a [0]] = a [1]

.....:


在[105]:mydict

Out [105]:{''mike'':''manager'',''paul'':''employee'',''tom '':''老板''}


Ciao,

马克''BlackJack''Rintsch


Lo***********@gmail.com 写道:


我有一个这样的清单


mylist = ['''',''tom = boss'', ''mike = manager'',''paul = employee'',''意思是无聊'''


我会喜欢e删除第一个和最后一个项目,因为它们是不可能的,

并将其转换为dict:

{''tom'':''boss'', ''mike'':''经理'',''paul'':''员工''}


我试过这个,但它没有用:


mydict = {}
$ m $ b for m in mylist [1:-1]:

a = i.split(''='' )

mydict [a [0]] = a [1]


我得到了这个:

文件" srch" ;,第19行,在< module>

grab(" a / tags1")

文件" srch",第15行,抓住

mydict [mylist [0]] = mylist [1]

IndexError:列表索引超出范围


任何人都可以让我知道这个?



适用于我,使用您提供的mylist示例。


查看机器上发生了什么,尝试打印a,在

拆分之后,但在你用它来填充字典之前。


< / F>


这很奇怪...


我在这里给出的列表与真实列表几乎相同,除了

长度。


谢谢Marc,我会去检查其他地方的错误


Hi pals

I have a list like this

mylist=['''',''tom=boss'',''mike=manager'',''paul=employee'',''mean ingless'']

I''d like to remove the first and the last item as they are irrevalent,
and convert it to the dict:
{''tom'':''boss'',''mike'':''manager'',''paul'':''employee''}

I tried this but it didn''t work:

mydict={}
for i in mylist[1:-1]:
a=i.split(''='') # this will disect each item of mylist into a 2-item
list
mydict[a[0]]=a[1]

and I got this:
File "srch", line 19, in <module>
grab("a/tags1")
File "srch", line 15, in grab
mydict[mylist[0]]=mylist[1]
IndexError: list index out of range

Anyone could shed me a light on this?

thanks

解决方案

On Wed, 09 Jan 2008 10:56:36 -0800, Louis.Soninhu wrote:

Hi pals

I have a list like this

mylist=['''',''tom=boss'',''mike=manager'',''paul=employee'',''mean ingless'']

I''d like to remove the first and the last item as they are irrevalent,
and convert it to the dict:
{''tom'':''boss'',''mike'':''manager'',''paul'':''employee''}

I tried this but it didn''t work:

mydict={}
for i in mylist[1:-1]:
a=i.split(''='') # this will disect each item of mylist into a 2-item
list
mydict[a[0]]=a[1]

and I got this:
File "srch", line 19, in <module>
grab("a/tags1")
File "srch", line 15, in grab
mydict[mylist[0]]=mylist[1]
IndexError: list index out of range

Anyone could shed me a light on this?

The real list you used had at least one string without a ''='' in it. The
list given above doesn''t raise that exception:

In [102]: mylist=['''',''tom=boss'',''mike=manager'',''paul=employee'',''mean ingless'']

In [103]: mydict={}

In [104]: for i in mylist[1:-1]:
.....: a=i.split(''='')
.....: mydict[a[0]]=a[1]
.....:

In [105]: mydict
Out[105]: {''mike'': ''manager'', ''paul'': ''employee'', ''tom'': ''boss''}

Ciao,
Marc ''BlackJack'' Rintsch


Lo***********@gmail.com wrote:

I have a list like this

mylist=['''',''tom=boss'',''mike=manager'',''paul=employee'',''mean ingless'']

I''d like to remove the first and the last item as they are irrevalent,
and convert it to the dict:
{''tom'':''boss'',''mike'':''manager'',''paul'':''employee''}

I tried this but it didn''t work:

mydict={}
for i in mylist[1:-1]:
a=i.split(''='')
mydict[a[0]]=a[1]

and I got this:
File "srch", line 19, in <module>
grab("a/tags1")
File "srch", line 15, in grab
mydict[mylist[0]]=mylist[1]
IndexError: list index out of range

Anyone could shed me a light on this?

works for me, with the mylist example you provided.

to see what''s going on on your machine, try printing "a" after the
split, but before you use it to populate the dictionary.

</F>


that''s very strange...

the list I give here is almost same as the real list, except for the
length.

Thanks Marc, I''ll go check what''s wrong elsewhere


这篇关于将列表转换为dict的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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