[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

problem of converting a list to dict

Louis.Soninhu

1/9/2008 6:57:00 PM

Hi pals

I have a list like this

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

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
16 Answers

Fredrik Lundh

1/9/2008 7:06:00 PM

0

Louis.Soninhu@gmail.com wrote:

> I have a list like this
>
> mylist=['','tom=boss','mike=manager','paul=employee','meaningless']
>
> 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>

Marc 'BlackJack' Rintsch

1/9/2008 7:08:00 PM

0

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','meaningless']
>
> 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','meaningless']

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

Louis.Soninhu

1/9/2008 7:17:00 PM

0

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

Louis.Soninhu

1/9/2008 7:25:00 PM

0

On Jan 9, 3:05 pm, Fredrik Lundh <fred...@pythonware.com> wrote:
> Louis.Soni...@gmail.com wrote:
> > I have a list like this
>
> > mylist=['','tom=boss','mike=manager','paul=employee','meaningless']
>
> > 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>- Hide quoted text -
>
> - Show quoted text -

'print a' works

Fredrik Lundh

1/9/2008 7:39:00 PM

0

Louis.Soninhu@gmail.com wrote:

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

so what does it tell you?

</F>

John Machin

1/9/2008 7:46:00 PM

0

On Jan 10, 5:56 am, Louis.Soni...@gmail.com wrote:
> Hi pals
>
> I have a list like this
>
> mylist=['','tom=boss','mike=manager','paul=employee','meaningless']
>
> 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

No it doesn't; it dissects i into a 2-item list if i is a string
containing exactly one '='.

DON'T rely on "knowing" that the first and last entries are the only
irrelevant ones. Do some checking. Conditions to check for:
(1) len(a) == 2
(2) a[0] is empty or not what you expect (a person's name)
(3) a[1] is empty or not what you expect (a job title)
(consider what happens with 'tom = boss' ... a[0] = 'tom ', a[1] = '
boss')
(4) duplicate keys [...., 'tom=boss', 'tom=clerk', ...]


Reedick, Andrew

1/9/2008 7:53:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Fredrik Lundh
> Sent: Wednesday, January 09, 2008 2:39 PM
> To: python-list@python.org
> Subject: Re: problem of converting a list to dict
>
> Louis.Soninhu@gmail.com wrote:
>
> >> to see what's going on on your machine, try printing "a" after the
> >> split, but before you use it to populate the dictionary.
> >
> > 'print a' works
>
> so what does it tell you?
>

A bigger hint:
a=i.split('=')
print "'%s' splits into " % (i), a
assert len(a) == 2
mydict[a[0]]=a[1]


Louis.Soninhu

1/9/2008 7:57:00 PM

0

oops, it seems there are other 'meaningless' item, which actually
caused the problem

Thanks for helps

John Machin

1/9/2008 8:02:00 PM

0

On Jan 10, 6:52 am, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=att....@python.org [mailto:python-
> > list-bounces+jr9445=att....@python.org] On Behalf Of Fredrik Lundh
> > Sent: Wednesday, January 09, 2008 2:39 PM
> > To: python-l...@python.org
> > Subject: Re: problem of converting a list to dict
>
> > Louis.Soni...@gmail.com wrote:
>
> > >> to see what's going on on your machine, try printing "a" after the
> > >> split, but before you use it to populate the dictionary.
>
> > > 'print a' works
>
> > so what does it tell you?
>
> A bigger hint:
> a=i.split('=')
> print "'%s' splits into " % (i), a

consider:
(1) using %r instead of '%s'
(2) omitting the redundant space after 'into'
(3) losing the redundant () around i


> assert len(a) == 2
> mydict[a[0]]=a[1]

Tim Chase

1/9/2008 8:12:00 PM

0

> mylist=['','tom=boss','mike=manager','paul=employee','meaningless']
>
> 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

This can be rewritten a little more safely like

mydict = dict(pair.split('=',1)
for pair in mylist
if '=' in pair)

Some of John Machin's caveats still apply:
(2) a[0] is empty or not what you expect (a person's name)
(3) a[1] is empty or not what you expect (a job title)
(consider what happens with 'tom = boss' ... a[0] = 'tom ', a[1] = '
boss')
(4) duplicate keys [...., 'tom=boss', 'tom=clerk', ...]

to which I'd add

(5) what happens if you have more than one equals-sign in your
item? ("bob=robert=manager" or "bob=manager=big-cheese")


#2 and #3 can be ameliorated a bit by

import string
mydict = dict(
map(string.strip,pair.split('=',1))
for pair in mylist
if '=' in pair)

which at least whacks whitespace off either end of your keys and
values. #4 and #5 require a clearer definition of the problem.

-tkc