0

this question comes together with another one i posted few hours ago:

Sorting dict items by key, beyond alphanumeric sorting

Keeping the need of having a tuple-like object ordered in that way https://stackoverflow.com/users/2141635/padraic-cunningham answered me very well,

now i need to modify tuple items, for example:

od = sorted(dizN.items(), key=key_func) #creates the sorted list of tuples

od[3][1]+=1 #is my attempt to increase by 1 the 2nd element of the 3rd tuple in the list of tuples; of course it is not possible.

Keep in mind which kind of object is "od":

od
Out[157]: 
[('a0p12', 0),
 ('a1p11', 0),
 ('a2p10', 0),
 ('a3p9', 0),
 ('a4p8', 0),
 ('a5p7', 0),
 ('a6p6', 0),
 ('a7p5', 0),
 ('a8p4', 0),
 ('a9p3', 0),
 ('a10p2', 0),
 ('a11p1', 0),
 ('a12p0', 0)]

I think a good solution for this problem would be to transform this list of tuples to a list of lists of 2 elements; this is the output i'm thinking to:

[['a0p12', 0],
 ['a1p11', 0],
 ['a2p10', 0],
 ['a3p9', 0],
 ['a4p8', 0],
 ['a5p7', 0],
 ['a6p6', 0],
 ['a7p5', 0],
 ['a8p4', 0],
 ['a9p3', 0],
 ['a10p2', 0],
 ['a11p1', 0],
 ['a12p0', 0]]

Does anybody know how to do this? Thanks a lot to anyone will answer!

Community
  • 1
  • 1
mik.ferrucci
  • 121
  • 1
  • 2
  • 13
  • 1
    are you simply trying to convert your tuples into lists in-place? if so... `od = [list(tup) for tup in od]` – Aaron Oct 17 '16 at 17:01
  • Exactly! I imagined it was very easy for programmers.. I have started very few time ago with basic programming.. Thanks very much! – mik.ferrucci Oct 17 '16 at 17:06
  • @Aaron - you should re-write this as an answer :-) – Tony Suffolk 66 Oct 18 '16 at 08:26
  • @TonySuffolk66 meh, OP probably has forgotten about it by now anyway... It wasn't really long enough to write a whole answer on the first time... Still isn't – Aaron Oct 18 '16 at 15:47
  • @Aaron I have not forget about it, as i told you, i am a beginner, and your answer in the comment was precious to me and for my program, even if it is a joke for expert programmers! Bye and thanks again! – mik.ferrucci Oct 19 '16 at 07:52

0 Answers0