H
H
heyovak232020-02-25 12:44:00
Python
heyovak23, 2020-02-25 12:44:00

How to loop through tuples?

Good afternoon!

In responses to SQL queries, I receive a tuple in the 1st response and tuples in the second.
For example (10, 20, 30)and [(5, 10, 15), (11, 12, 14)](may not be 2, 122 sets in the second answer) respectively.
I can't figure out how to make a loop to divide the response values ​​(of the first tuple) into the corresponding values ​​of the second response.
Here in this form - everything works, but it's a little (absolutely) not that.

a = (10, 20, 30)
w = (5, 10, 15)
ee = zip(a,w)
for a,w in ee:
  print(a*w)


Maybe mathematical operations can be done immediately in sql queries?
Now they look like this: and Thank you! UPD. I would like to see the answer like this: 50, 200, 450 110, 240, 420
c.execute('SELECT sid, kol, bot FROM stocks')
c2.execute('SELECT sid, kol, bot FROM stul')


Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2020-02-25
@heyovak23

a = (10, 20, 30)
b = [(5, 10, 15), (11, 12, 14)]
c = list()

for i, v in enumerate(b):
    c.insert(i, [])
    for x, y in zip(a, v):
        c[i].append(x * y)

print(c)

outp:
It?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question