0

I've been teaching myself python and so far it's hasn't been too bad. I reckon the easiest way to learn is to just start coding so I've come up with relatively simple tasks to help me. And now I'm stuck and it's getting frustrating not being able to figure out what I'm doing wrong

{0: [[1, '.', 0, '-', 8]], 
1: [['.', 3, '.', 2, 0, 0, 1, '-']], 
2: [], 
3: [['.', '.']], 
4: [], 
5: [[2, 0, 1, 2, '-', 0, 1, '-', 1, 9]], 
6: [[1, '.', 0, 0, 9, 5]], etc...

So I have a dictionary where each key has a value(which are exchange rates for various currency pairs). I've been trying to turn the values into a single string so for instance:

0: [[1, '.', 0, '-', 8]] would become 0: '1.0-8'
6: [[1, '.', 0, 0, 9, 5]] would be 6 : '1.0095'
etc...

After numerous hours of trying various methods and googling, I've come to the conclusion that I have no idea whatsoever on how to accomplish this. I've tried doing a replace, and various complicated loops that turn the dictionary into a list of lists with the key[value] as a list and then trying to iterate over it using a join function etc...and it all has accomplished absolutely nothing!

It seems like it should be simple to do, but I give up, I have no idea how to do this, so hopefully someone here can give me a hand.

Thanks a lot!

georg
  • 211,518
  • 52
  • 313
  • 390
Jason White
  • 666
  • 4
  • 10
  • 23

4 Answers4

1

One-liner...

>>> {i:(''.join(str(x) for x in j[0]) if j else '') for i,j in d.items()}
{0: '1.0-8', 1: '.3.2001-', 2: '', 3: '..', 4: '', 5: '2012-01-19', 6: '1.0095'}

"writing it in a noob friendly way", as asked:

d_new = {}
for i,j in d.items():
    if j:
        d_new[i] = ''.join(str(x) for x in j[0])
    else:
        d_new[i] = ''
JBernardo
  • 32,262
  • 10
  • 90
  • 115
0
d = {0: [[1, '.', 0, '-', 8]], 6: [[1, '.', 0, 0, 9, 5]]}
for k,v in d.iteritems():
    print str(k)+':'+''.join(str(el) for el in v[0])
Denis
  • 7,127
  • 8
  • 37
  • 58
  • I think the OP wants to create a new dictionary instead of printing the format of one. – jamylak Apr 18 '12 at 07:26
  • This is a concept, it is easily reworked by whatever and I dont think what stackoverflow is world center for homework solution )) – Denis Apr 18 '12 at 07:37
  • Fair enough but this isn't actually homework, the OP is teaching himself. – jamylak Apr 18 '12 at 07:38
0
>>> from itertools import chain
>>> d = {0: [[1, '.', 0, '-', 8]], 1: [['.', 3, '.', 2, 0, 0, 1, '-']], 2: [], 
3: [['.', '.']], 4: [], 5: [[2, 0, 1, 2, '-', 0, 1, '-', 1, 9]], 6: [[1, '.', 0, 0, 9, 5]]}
>>> {k:''.join(str(el) for el in chain.from_iterable(v)) for k,v in d.items()}
{0: '1.0-8', 1: '.3.2001-', 2: '', 3: '..', 4: '', 5: '2012-01-19', 6: '1.0095'}
jamylak
  • 128,818
  • 30
  • 231
  • 230
0

Another approach. Here chain takes care in unwrapping a list and handling empty list.

>>> {k:"".join(map(str,chain(*v))) for k,v in spam.items()}
{0: '1.0-8', 1: '.3.2001-', 2: '', 3: '..', 4: '', 5: '2012-01-19', 6: '1.0095'}

Few of intuitive approaches

To unwrap a list like [[1, '.', 0, '-', 8]] using itertools.chain, use

>>> list(chain(*[[1, '.', 0, '-', 8]]))
[1, '.', 0, '-', 8]

To convert a list of int to str with map, use (Please note, it can also work with iterables)

>>> map(str,[1, '.', 0, '-', 8])
['1', '.', '0', '-', '8']

To join a list of strings use. (Please note, it can also work with iterables)

"".join(['1', '.', '0', '-', '8'])

Dictionary Comprehension

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Also my approach although I used `chain.from_iterable(v)` instead of `chain(*v)` – jamylak Apr 18 '12 at 07:31
  • @jamylak: And also didn't used list comprehensions – Abhijit Apr 18 '12 at 07:33
  • Ah yes, I've been told they are much more readable than `map`. – jamylak Apr 18 '12 at 07:34
  • @jamylak: Have a look into the second answer in this [SO Question](http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map) – Abhijit Apr 18 '12 at 10:58
  • I've read something like that before but i guess its also a matter of personal preference and i just thought it looks better without so many nested brackets. – jamylak Apr 18 '12 at 11:07