-3

I want to assign a value to each tuple in a list. Let's say my list looks like this:

[(68, 125), (113, 69), (65, 86), (108, 149), (152, 53), (78, 90), (54, 160), (20, 137), (107, 90), (48, 12)]

I need to assign each of these tuples into a dict where it should look like:

1 [(48, 12)]
2 [(68, 125), (54, 160), (20, 137)]
3 [(113, 69), (152, 53)]
4 [(108, 149)] 

This is what I have so far:

new_dict = {"1":[], "2":[], "3":[], "4":[]}
for (x,y) in numbers:
    if (x,y) == (48, 12):
        new_dict += {"1:"[(x,y)]}
    elif (x,y) == (68, 125) or (x,y) == (54, 160) or (x,y) == (20, 137):
        new_dict += {"2:"[(x,y)]}
    elif (x,y) == (113, 69) or (x,y) == (152, 53):
        new_dict += {"3:"[(x,y)]}
    elif (x,y) == (108, 149):
        new_dict += {"4:"[(x,y)]}
    return new_dict

Help will be much appreciated. Thank you.

R. Mercy
  • 509
  • 1
  • 5
  • 21
  • 4
    Why are the tuples collected like this? What is the logic? –  Jun 02 '16 at 11:43
  • It is part of some homework which I have to do. I just need to know how to do this part here. I can upload the rest of this if you want but it is not relevant. – R. Mercy Jun 02 '16 at 11:49

2 Answers2

1

There are some problems in your code.

  • iterate over the tuples, you don't have to split them
  • compare using in, it's shorter than using or
  • use append to add to an existing list

Try this:

new_dict = {"1":[], "2":[], "3":[], "4":[]}
for t in numbers:
    if t == (48, 12):
        new_dict["1"].append(t)
    elif t in [(68, 125), (54, 160), (20, 137)]:
        new_dict["2"].append(t)
    elif t in [(113, 69), (152, 53)]:
        new_dict["3"].append(t)
    elif t == (108, 149):
        new_dict["4"].append(t)
print(new_dict)

Output:

{'1': [(48, 12)], '2': [(68, 125), (54, 160), (20, 137)], '4': [(108, 149)], '3': [(113, 69), (152, 53)]}
1

If you know the group of each element (as you mentioned), you can proceed with:

from itertools import groupby

L  = [(68, 125), (113, 69), (65, 86), (108, 149), (152, 53), (78, 90), (54, 160), (20, 137), (107, 90), (48, 12)]
ix = [1, 2, 2, 2, 3, 3, 4]

{i:map(lambda x: x[0], g) for i, g in groupby(zip(L,ix), key=lambda x: x[1])}

Out[216]:
{1: [(68, 125)],
 2: [(113, 69), (65, 86), (108, 149)],
 3: [(152, 53), (78, 90)],
 4: [(54, 160)]}
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
  • again, who downvote without reading question/answers? seems common in python forum ... – Colonel Beauvel Jun 02 '16 at 12:06
  • 2
    User is a noobie, throwing map, zip, and groupby at her isn't going to help. In fact, many people (myself included) absolutely hate map and zip since they are complex, so they obfuscate code, and the Python interpreter can iterate just as fast via explicit for loops. So, I yank map and zip whenever I see them in a codebase I'm working on if at all possible - the noobs on my team will scratch heads, panic, or misinterpret what they're doing. – Kevin J. Rice Jun 02 '16 at 12:09
  • 1
    or do not employ noobs :) It's also by twicking these functions that people progress a little bit in programming ... – Colonel Beauvel Jun 02 '16 at 12:10
  • Both are valid arguments. How senior does a Python developer have to be to understand such basic functional programming? –  Jun 02 '16 at 12:58