+ 3
How to take 3 variables in a for loop?
I have written a code. for x,y,z in map(int, input().split()): print(x,y,z) I want to use it as list comprehension with a formula. But the code doesn't work. Even i make it into tuple or list the code doesn't work. It gives out an error "cannot unpack non-iterable int object"
16 ответов
+ 5
It's tuple unpacking, the basic form is:
x, y = (2, 3)
print(x, y)
So, all you need something that returns a list of tuples to unpack from iteration variables. In this case, you can use zip() function.
n,p = int(input()),list()
for _ in range(n):
numlist = list((map(int, input().split())))
a,b,c, = numlist[:1],numlist[1:-1],numlist[2:]
k = [x*y+z for x,y,z in zip(a,b,c)]
p.extend(sum(map(int, str(x))) for x in k)
print(*p)
+ 3
Anshuman Routray ,
to get your code run as a comprehension with also doing the calculation, we need to know, that a comprehension needs to get an *iterable* as input. this is not different from running a regular for loop. instead of using list(...) to accomplish this, we should use just [...] which is also a comprehension. so we have a nested comprehension.
the line in your code where the checksum is calculated, needs a bit more reworking.
so the code that is doing the input and all the calculation is only 2 lines:
n,p = int(input()),list()
for _ in range(n):
k = [x*y+z for x,y,z in [map(int, input().split())]]
# calculating the value k, nested comprehension
p.append(sum(list(map(int, str(*k)))))
# calculating the checksum of k
print(*p)
+ 2
Anshuman Routray ,
the way the comprehension is currently done is not working. the reason is, that input().split() does not supply 3 numbers in one go, but it supplies the 3 numbers one by one. we should separate the input from the calculation.
here is a rough try, that may guide you:
counter = int(input()) # do not create and initialize 2 variables in one line
check_sum = []
for i in range(counter):
inp = [int(num) for num in input().split()] # input 3 numbers separated by space
buf = str(inp[0] * inp[1] + inp[2]) # doing the required calculation
check_sum.append(sum([int(dig) for dig in buf])) # calculating the checksum
print(*check_sum) # output result
+ 2
Lothar got it 🙇
+ 2
Simba thanks for the code
+ 2
Prince Kumar ,
if you are asking how this is calculated, see the description from the op somewhere above.
+ 1
n,p = int(input()),list()
for _ in range(n):
k = [x*y+z for x,y,z in list(map(int, input().split()))]
p.append(sum(list(map(int, str(x).replace('',' ').split()))))
print(*p)
How to make this code work?
+ 1
An illustration of input and expected output may help others to better understand your intention, and suggest you accordingly.
+ 1
Input data are in the following format:
first line contains N - the number of values to process;
and then N lines will follow describing the values for which sum of digits should be calculated by 3 integers A B C;
for each case you need to multiply A by B and add C (i.e. A * B + C) - then calculate sum of digits of the result.
+ 1
input data:
3
11 9 1
14 90 232
111 15 111
answer:
1 16 21
+ 1
Lothar ,
I know how to make a working code to this problem But I want to learn more about the loop thing when it comes to multiple variables.
Could you please explain me how to use multiple variable in a for loop?
+ 1
Prince Kumar
Input data are in the following format:
first line contains N - the number of values to process;
and then N lines will follow describing the values for which sum of digits should be calculated by 3 integers A B C;
for each case you need to multiply A by B and add C (i.e. A * B + C) - then calculate sum of digits of the result.
0
Thanks I searched for explaining I searched for it on google and understood how list comprehension works.
But I have seen people using multiple variables in a for loop how does that work could you explain me?
Here is an example,
for i, j in [(0, 1), ('a', 'b')]:
... print('i:', i, 'j:', j)
output:
i: 0 j: 1
i: a j: b
How does this work? and is it different form list comprehension?
0
def downloadData(n,i,d): for name, id, data in zip(n,i,d): URL = "http://www.website.com/data/{}".format(name) #downloads the file from the website. The last part of the URL is the name r = requests.get(URL) with open("data/{}_{}_{}.csv".format(name, id, data), "wb") as code: #create the file in the format name_id_dataType code.write(r.content)
0
Anshuman Routray sorry but what is the result you would obtain?
0
How to print take a num it is a strong num Or not a strong