views:

222

answers:

6

I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like

[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]

I know dictionary.items() is a list of tuples, but I can't seem to figure out how to make a nice format that is also compatible with the SetValue() method of wxPython.

I've tried iterating through the list and tuples. If I use a print statement, the output is fine. But when I replace the print statement with SetValue(), it only seems to get the last value of each tuple, rather than both items in the tuple.

I've also tried creating a string and passing that string to SetValue() but, again, I can only get one item in the tuple or the other, not both.

Any suggestions?


Edit: Yes, I am passing the results of the dictionary.items() to a text field in a wxPython application. Rather than having the results like above, I'm simply looking for something like:

BC 45
CHM 25
CMP 30

Nothing special, just simply pulling each value from each tuple and making a visual list.

I have tried making a string format and passing that to SetValue() but it gets hung up on the two values in the tuple. It will either double print each string and add the integers together or it simply returns the integer, depending on how I format it.

A: 

Maybe the pretty print module will help:

>>> import pprint
>>> pprint.pformat({ "my key": "my value"})
"{'my key': 'my value'}"
>>>
derfred
A: 
text_for_display = '\n'.join(item + u' ' + unicode(value) for item, value in my_dictionary.items())
A: 

use % formatting (known in C as sprintf), e.g:

"%10s - %d" % dict.items()[0]

Number of % conversion specifications in the format string should match tuple length, in the dict.items() case, 2. The result of the string formatting operator is a string, so that using it as an argument to SetValue() is no problem. To translate the whole dict to a string:

'\n'.join(("%10s - %d" % t) for t in dict.items())

The format conversion types are specified in the doc.

gimel
A: 

That data seems much better displayed as a Table/Grid.

Ali A
+4  A: 

There is no built-in dictionary method that would return your desired result.

You can, however, achieve your goal by creating a helper function that will format the dictionary, e.g.:

def getNiceDictRepr(aDict):
    return '\n'.join('%s %s' % t for t in aDict.iteritems())

This will produce your exact desired output:

>>> myDict = dict([(u'BC',45), (u'CHM',25), (u'CPM',30)])
>>> print getNiceDictRepr(myDict)
BC 45
CHM 25
CPM 30

Then, in your application code, you can use it by passing it to SetValue:

self.textCtrl.SetValue(getNiceDictRepr(myDict))
DzinX
A: 

I figured out a "better" way of formatting the output. As usual, I was trying to nuke it out when a more elegant method will do.

for key, value in sorted(self.dict.items()):
    self.current_list.WriteText(key + " " + str(self.dict[key]) + "\n")

This way also sorts the dictionary alphabetically, which is a big help when identifying items that have already been selected or used.

crystalattice