Reassigning keys in dictionary of lists and then writing out to CSV file?

2015-06-18 Thread Sahlusar
I am currently attempting to work on converting a fairly sizeable JSON object and convert it into a CSV format. However, when I attempt to do so, using a conventional approach (that seems to work with other files). I am presented with a ValueError: too many values to unpack I have tried to

Re: Help building a dictionary of lists

2012-11-13 Thread Thomas Bach
On Mon, Nov 12, 2012 at 11:41:59PM +, Joshua Landau wrote: Dict comprehension: {i:[] for i in [Test 1, Test 2, Test 3]} In Python 2.6 this syntax is not supported. You can achieve the same there via dict((i, []) for i in ['Test 1', 'Test 2', 'Test 3']) Also have a look at

Help building a dictionary of lists

2012-11-12 Thread NJ1706
', 'Test_2' ) # Initialise the dictionary of lists dict1 = { 'Test_1' : [], 'Test_2' : [], } instances = ('1') # Loop through the list of tests for Test in TestList: print print Test: , Test # Append to the list for each instance for instance in instances: print

Re: Help building a dictionary of lists

2012-11-12 Thread Joshua Landau
On 12 November 2012 22:26, NJ1706 nickj1...@googlemail.com wrote: Chaps, I am new to Python have inherited a test harness written in the language that I am trying to extend. The following code shows how dictionaries holding lists of commands are handled in the script... Start of Code_1

Re: Help building a dictionary of lists

2012-11-12 Thread Joshua Landau
', 'Test_2' ] Additionally, your comment here is somewhat pointless. The code says *exactly* what your comment does, so why write it twice? # List of tests Don't need # Initialise the dictionary of lists Don't need # Loop through the list of tests Don't need # Append to the list for each

Appending to dictionary of lists

2011-05-03 Thread Alex van der Spek
I open a csv file and create a DictReader object. Subsequently, reading lines from this file I try to update a dictionary of lists: csvf=open(os.path.join(root,fcsv),'rb') csvr=csv.DictReader(csvf) refd=dict.fromkeys(csvr.fieldnames,[]) for row in csvr: for (k,v) in row.items

Re: Appending to dictionary of lists

2011-05-03 Thread Paul Rubin
Alex van der Spek zd...@xs4all.nl writes: refd=dict.fromkeys(csvr.fieldnames,[]) ... I do not understand why this appends v to every key k each time. You have initialized every element of refd to the same list. Try refd = dict((k,[]) for k in csvr.fieldnames) instead. --

Re: Appending to dictionary of lists

2011-05-03 Thread Dan Stromberg
On Tue, May 3, 2011 at 12:56 PM, Paul Rubin no.email@nospam.invalid wrote: Alex van der Spek zd...@xs4all.nl writes: refd=dict.fromkeys(csvr.fieldnames,[]) ... I do not understand why this appends v to every key k each time. You have initialized every element of refd to the same list.

Re: Appending to dictionary of lists

2011-05-03 Thread Alex van der Spek
Thank you! Would never have found that by myself. Paul Rubin no.email@nospam.invalid wrote in message news:7x7ha75zib@ruckus.brouhaha.com... Alex van der Spek zd...@xs4all.nl writes: refd=dict.fromkeys(csvr.fieldnames,[]) ... I do not understand why this appends v to every key k each

Dictionary of lists strange behaviour

2010-11-09 Thread Ciccio
Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) In [215]: rg Out[215]: {'a': [], 'b': []} In [216]: rg['a'].append('x') In [217]: rg Out[217]: {'a': ['x'], 'b': ['x']} What I meant

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Matteo Landi
On Tue, Nov 9, 2010 at 3:14 PM, Ciccio franap...@gmail.com wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) The argument you pass which is used to fill the values of the

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Jean-Michel Pichavant
Ciccio wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) In [215]: rg Out[215]: {'a': [], 'b': []} In [216]: rg['a'].append('x') In [217]: rg Out[217]: {'a': ['x'], 'b':

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Dave Angel
On 2:59 PM, Ciccio wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) In [215]: rg Out[215]: {'a': [], 'b': []} In [216]: rg['a'].append('x') In [217]: rg Out[217]: {'a':

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Terry Reedy
On 11/9/2010 9:14 AM, Ciccio wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) If you rewrite this as bl = [] rg = dict.fromkeys(g.keys(),bl) is the answer any more

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Ciccio
Il 09/11/2010 16:47, Terry Reedy ha scritto: On 11/9/2010 9:14 AM, Ciccio wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) If you rewrite this as bl = [] rg =

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Ciccio
Thank you all, this was timely and helpful. francesco -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary of lists strange behaviour

2010-11-09 Thread Terry Reedy
On 11/9/2010 12:19 PM, Ciccio wrote: Il 09/11/2010 16:47, Terry Reedy ha scritto: On 11/9/2010 9:14 AM, Ciccio wrote: Hi all, hope you can help me understanding why the following happens: In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} In [214]: rg = dict.fromkeys(g.keys(),[]) If you

Re: Dictionary of lists strange behaviour

2010-11-09 Thread John Posner
On 11/9/2010 1:43 PM, Terry Reedy wrote: ... List *is* useful as an initializer for collecitons.defaultdicts. And it was useful when several members of this forum helped me to develop a prime-number generator. See http://www.mail-archive.com/python-list@python.org/msg288128.html. (I meant

Re: Dictionary with Lists

2009-10-04 Thread Shaun
Okay that makes sense. I was assuming that list.append returned the new list. thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary with Lists

2009-10-04 Thread Mick Krippendorf
John Nagle schrieb: Shaun wrote: I'm trying to create a dictionary with lists as the value for each key. Try using a tuple, instead of a list, for each key. Tuples are immutable, so there's no issue about a key changing while being used in a dictionary. Only if Shaun wanted to use

Re: Dictionary with Lists

2009-10-03 Thread Mick Krippendorf
Hi, Shaun wrote: I'm trying to create a dictionary with lists as the value for each key. I was looking for the most elegant way of doing it... from collections import defaultdict d = defaultdict(list) d[joe].append(something) d[joe].append(another) d[jim].append(slow down, grasshopper

Re: Dictionary with Lists

2009-10-03 Thread Mel
Shaun wrote: testDict = {} ... testDict [1] = testDict.get (1, []).append (Test0) # 1 does not exist, create empty array print testDict testDict [1] = testDict.get (1, []).append (Test1) print testDict [ ... ] However, the first printout gives {1: None} instead of the desired {1:

Re: Dictionary with Lists

2009-10-03 Thread John Nagle
Shaun wrote: Hi, I'm trying to create a dictionary with lists as the value for each key. I was looking for the most elegant way of doing it... Try using a tuple, instead of a list, for each key. Tuples are immutable, so there's no issue about a key changing while being used

Re: Iterating Through Dictionary of Lists

2009-09-14 Thread JB
On Sep 11, 9:42 am, Stefan Behnel stefan...@behnel.de wrote: JBwrote: I have created a small program that generates a project tree from a dictionary. The dictionary is of key/value pairs where each key is a directory, and each value is a list. The list have unique values corresponding to

Iterating Through Dictionary of Lists

2009-09-11 Thread JB
I have created a small program that generates a project tree from a dictionary. The dictionary is of key/value pairs where each key is a directory, and each value is a list. The list have unique values corresponding to the key, which is a directory where each value in the list becomes a

Re: Iterating Through Dictionary of Lists

2009-09-11 Thread Stefan Behnel
JB wrote: I have created a small program that generates a project tree from a dictionary. The dictionary is of key/value pairs where each key is a directory, and each value is a list. The list have unique values corresponding to the key, which is a directory where each value in the list

inverting a dictionary of lists

2007-06-14 Thread bpowah
I searched for an hour and don't see a solution to this (i assume somewhat common) problem. I have a very large dictionary of lists: d = {a:[1,2], b:[2,3], c:[3]} and i want to reverse the associativity of the integers thusly: inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]} my solution expands

Re: inverting a dictionary of lists

2007-06-14 Thread Gabriel Genellina
En Fri, 15 Jun 2007 00:20:33 -0300, [EMAIL PROTECTED] escribió: I searched for an hour and don't see a solution to this (i assume somewhat common) problem. I have a very large dictionary of lists: d = {a:[1,2], b:[2,3], c:[3]} and i want to reverse the associativity of the integers thusly

Re: inverting a dictionary of lists

2007-06-14 Thread Raymond Hettinger
On Jun 14, 8:20 pm, [EMAIL PROTECTED] wrote: I have a very large dictionary of lists: d = {a:[1,2], b:[2,3], c:[3]} and i want to reverse the associativity of the integers thusly: inverse(d) makes {1:[a], 2:[a,b], 3:[b,c]} Try using setdefault: d = {'a':[1,2], 'b':[2,3], 'c':[3]} r

Re: Iterate through a dictionary of lists one line at a time

2007-04-19 Thread Maric Michaud
wswilson a écrit : Here is my code: listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']} I need to output: id name a Joe b Jane c Bob I could do: print 'id', 'name' for id, name in zip(listing['id'], listing['name']): print id, name but that only works if there

Iterate through a dictionary of lists one line at a time

2007-04-18 Thread wswilson
Here is my code: listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']} I need to output: id name a Joe b Jane c Bob I could do: print 'id', 'name' for id, name in zip(listing['id'], listing['name']): print id, name but that only works if there are two entries in the dictionary,

Re: Iterate through a dictionary of lists one line at a time

2007-04-18 Thread Arnaud Delobelle
On Apr 18, 7:39 pm, wswilson [EMAIL PROTECTED] wrote: Here is my code: listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']} I need to output: id name a Joe b Jane c Bob I could do: print 'id', 'name' for id, name in zip(listing['id'], listing['name']): print id, name

Re: Iterate through a dictionary of lists one line at a time

2007-04-18 Thread Steve Holden
wswilson wrote: Here is my code: listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']} I need to output: id name a Joe b Jane c Bob I could do: print 'id', 'name' for id, name in zip(listing['id'], listing['name']): print id, name but that only works if there are

Re: Iterate through a dictionary of lists one line at a time

2007-04-18 Thread wswilson
On Apr 18, 2:54 pm, Arnaud Delobelle [EMAIL PROTECTED] wrote: On Apr 18, 7:39 pm, wswilson [EMAIL PROTECTED] wrote: Here is my code: listing = {'id': ['a', 'b', 'c'], 'name': ['Joe', 'Jane', 'Bob']} I need to output: id name a Joe b Jane c Bob I could do: print 'id',

RE: Iterate through a dictionary of lists one line at a time

2007-04-18 Thread Hamilton, William
-Original Message- From: [EMAIL PROTECTED] [mailto:python- [EMAIL PROTECTED] On Behalf Of wswilson Sent: Wednesday, April 18, 2007 1:39 PM To: python-list@python.org Subject: Iterate through a dictionary of lists one line at a time Here is my code: listing = {'id': ['a', 'b

Inserting a dictionary of lists into '__main__' of an embedded interpreter

2005-05-27 Thread quadric
interpreter, I attempt to insert a dictionary of lists into the module represented by the pointer 'pmod' passed to the DLL from the main application. The dictionary of lists is created with the following call: PyObject * abm_dict = Py_BuildValue( {s:O,s:O,s:O,s:O,s:O,s:O,s:O,s:O,s:O