0

Premise that i'm still looking for a solution. By extracting the values from a Sql, i create a dictionary in the following way. As you can see the key is the set of row[0],row[1],row[2], row[3], row[4], while the value is only row[5].

PROBLEM: The problem is that I can access only the key and value (which is for example [0, 1, 1, 5]). I would like to access individual row[0],row[1],row[2], row[3], row[4] and row[5] separately.

How can I create a dictionary where for example:

  • the key is New York-Dallas (so row[0])
  • the values are NHL (then row[1]), 8.1 (then row[2]), 9 (then row[3]), 15.00 (then row[4]) and finally [0, 1, 1, 5] (then row[5])

So something like {('New York-Dallas': 'NHL', 8.1, 9, '15:00', [0, 1, 1, 5]

P.S: I'm still looking for answers. I have been given a helpful answer, but I don't want to convert the dictionary leaving the bad code, but I want to edit the dictionary code directly

x={}

for row in next.fetchall():
        x.setdefault((row[0],row[1],row[2], row[3], row[4]), []).append(row[5])

Output
#{('New York-Dallas', 'NHL', 8.1, 9, '15:00'): [0, 1, 1, 5], 
#('Vegas-Chicago', 'NHL', 8.1, 9, '18:00'): [1, 2, 3, 4]}
  • `x[row[0]] = (row[i] for i in range(1, 6))`? – Plagon Jan 08 '23 at 01:16
  • Right, or just `x[row[0]] = row[1:]`. – Tim Roberts Jan 08 '23 at 01:19
  • @Plagon Now i get error: {'New York-Dallas: . at 0x7f8b95cd1d90>, 'Vegas-Chicago': . at 0x7f8b95cd1e00> –  Jan 08 '23 at 01:20
  • Why don't you just do your queries from SQL as you need them? – Tim Roberts Jan 08 '23 at 01:20
  • @TimRoberts Because I have to put everything into a dictionary. But maybe I don't understand what you are saying. However your code works, it prints the dictionary correctly, but there is a problem. The last value which should be (for example) [0, 1, 1, 5], I just get 0. I would like to print them all –  Jan 08 '23 at 01:25
  • @Plagon If I replace the brackets and write this [row[i] for i in range(1, [6)], it works fine. But there is one problem which is the same one received with Tim Roberts' code. The last value which should be (for example) [0, 1, 1, 5], I just get 0. I would like to print them all –  Jan 08 '23 at 01:32
  • Why is it [0,1,1,5]? I don't know of an SQL query that would produce that. Show us your SQL query, and maybe your table layout. – Tim Roberts Jan 08 '23 at 02:21

1 Answers1

0

If I understood your question well, you want to transform the x to have keys only from the first value of the tuples:

x={}

for row in next_.fetchall():  # <-- don't use `next`, that's built-in function
    x.setdefault((row[0],row[1],row[2], row[3], row[4]), []).append(row[5])

# transform the x:
x = {a: [*rest, lst] for (a, *rest), lst in x.items()}
print(x)
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • I get error: UnboundLocalError: local variable 'x' referenced before assignment –  Jan 08 '23 at 01:37
  • @Takao貴男 You don't use `x` as a name for the dictionary? – Andrej Kesely Jan 08 '23 at 01:40
  • Yes, i use x as a name for dictionary –  Jan 08 '23 at 01:41
  • @Takao貴男 The code I posted should work. First declare `x = {}` (inside the function), then do your loop that you have in your question and then make the transformation `out = {a: [b, c, d, e, lst] for (a, b, c, d, e), lst in x.items()}` – Andrej Kesely Jan 08 '23 at 01:42
  • I have x outside the function, but X is in the single main file. It worked so far and has always worked without giving me any problems. However if I move x into the function, I get ValueError: too many values to unpack (expected 4). X however I need it out of the function (in the main file) where it has always been and hasn't caused any problems –  Jan 08 '23 at 01:45
  • @Takao貴男 Then at the beginning of your function you should put `global x` (but it's hard to say without seeing your complete structure). For your second problem, I've updated the answer. – Andrej Kesely Jan 08 '23 at 01:49
  • Why has x = {} always worked out of the function so far and now it doesn't? Even if I use the other two users' answers in the comments, x works even if it doesn't. What is the difference between before and now? –  Jan 08 '23 at 01:53
  • @Takao貴男 You've declared `x = {}` outside the function. It has worked so far because you didn't assign to `x` yet (with `x = ...`). You've only used it's methods `x.setdefault()`, `x[key] = ` etc. You can read more for example here: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Andrej Kesely Jan 08 '23 at 01:56
  • Ok, I just changed the name and it worked fine. Now it's out of the function :) A curiosity: how can I print singularly the various row[0],row[1],row[2], row[3], row[4]? I wanted to change the code in order to access every element of the dictionary –  Jan 08 '23 at 01:57
  • @Takao貴男 When you have transformed `x` you can do `for key, value in x.items(): print(key, value[0], value[1], ... etc.)` – Andrej Kesely Jan 08 '23 at 01:59
  • OK perfect. Thank you. One thing is not clear to me: you talk about transformation of x. What did you turn x into? Does it have a technical definition? What could you call that staff you created x = {a: [*rest, lst] for (a, *rest), lst in x.items()}? What is it? What does he do? –  Jan 08 '23 at 02:03
  • @Takao貴男 The transformation I'm talking about is that I transformed the dictionary where keys were tuples with 5 elements to dictionary where keys are just one element and values are lists with 5 elements. – Andrej Kesely Jan 08 '23 at 02:04
  • Looking at the code I was thinking: there is no way to "not transform" x, but to directly modify this code x.setdefault((row[0],row[1],row[2], row[3], row[4]), []).append(row[5])? It seems strange to me to transform a bad code to make it better. Wouldn't it be better to directly intervene on the wrong code and modify it? –  Jan 08 '23 at 02:15
  • @Takao貴男 With `x.setdefault((row[0],row[1],row[2], row[3], row[4]), []).append(row[5])` you group the data by the first 5 elements. Another way to do it would be to do the grouping directly in your SQL query (`GROUP BY` clause). – Andrej Kesely Jan 08 '23 at 02:18
  • Yes, I group the data for the first 5 items. Isn't it possible to group them directly so you have what you do with the transform? It's about creating a simple dictionary with key and values. I'm not sure what the transformation you use is needed –  Jan 08 '23 at 02:20
  • @Takao貴男 As I've already wrote... I don't have all information about structure of your code (and your SQL query, DB schema...). Of course, It probably can be done differently. – Andrej Kesely Jan 08 '23 at 02:24
  • You mentioned that another way to do this would be grouping directly in the SQL query. Could you show me please? I've updated the question showing my SQL. If this solves it, I'll take your application right away. Thank you –  Jan 08 '23 at 02:26
  • @Takao貴男 That's probably topic for a new question here on StackOverflow - not Python but SQL one. – Andrej Kesely Jan 08 '23 at 02:27