[Tutor] dictionary of lists

Alan Gauld alan.gauld at btinternet.com
Wed Jun 3 22:15:26 CEST 2015


On 03/06/15 17:39, Chris Stinemetz wrote:
> I am trying to create a dictionary of lists as I read a file. I
> envision it looking like: {key: [float_type],[string_type]}


Thats not a dictionary of lists. You maybe mean:

{key: [[float_type],[string_type]]}

Which is a dictionary of lists of lists?

> For the first item in the list I am trying to add the value to the
> existing value where the key matches

Sorry, I'm sure that made sense to you but not to me.
Which value are you adding to which existing value?
Can you give a before/after example?

> Resetting execution engine
> Running C:\Users\cs062x\Desktop\python\projects\PanHandle\PanHandle\PanHandle.py
> The Python REPL process has exited

That's slightly unusual. How are you running this?

> Traceback (most recent call last):
>    File "C:\Users\cs062x\Desktop\python\projects\PanHandle\PanHandle\PanHandle.py",
> line 22, in <module>
>      d[IMEI] += Elapsed_Mins
> TypeError: 'float' object is not iterable



> d = defaultdict(list)
> for fname in os.listdir('.'):
>      with open (fname) as csvfile:
>          spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
>          next(spamreader)
>          for row in spamreader:
>
>              if row[8]:
>                  device = row[36]
>                  Elapsed_Mins = float(row[7])
>                  IMEI = row[8].replace("'", "")

So IMEA is a string and Elapsed_Mins is a float and d is a default dict 
that sets its defaults to lists.

>                  d[IMEI] += Elapsed_Mins ## this is where the error occurs.

So this is trying to add a float to a list.
 >>> L = []
 >>> L += f
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'float' object is not iterable

look familiar?

Now, what I don't know, is what you are trying to do.
Are you trying to append the float to the list?
Or to replace the list with the float?
Or to add the float to the value of the first(or last?)
element in the list - if it exists
(and if it doesn't? Then what?)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list