0

I want to use the below code (am supposed to not use map(), for an online programming site as gives error on that of : TypeError: 'map' object is not subscriptable ):

arr[i][j] = [int(input("Input score").strip().split()[:l]) for j in range(n) for i in range(t)]

instead of the below working version:

for i in range(t):   
    for j in range(n):
        arr[i][j] = map(int, input("Input score").strip().split())[:l]

but the error is (assume so) based on providing a list instead of individual values, as stated below:

TypeError: int() argument must be a string or a number, not 'list'

Unable to find a solution by any alternate way, say convert rhs (of desired soln.) to a string in first step & then assign to lhs in the second step; as need assign to arr[i][j].

P.S. Need to make the solution use the individual and row-wise values of arr, as say need find row-wise sum of values or even individual values. The below code uses the row-wise arr values to fill up total.

for i in range(t): 
    for j in range(n):
        # Find sum of all scores row-wise 
        sum = 0
        for m in arr[i][j]:
            sum += m
        total[i][j] = sum

2 Answers 2

2

You can map your nested for loops:

for i in range(t):   
    for j in range(n):
        arr[i][j] = map(int, input("Input score").strip().split())[:l]

to a list comprehension like:

arr = [map(int, input("Input score").strip().split())[:l] for i in range(t) for j in range(n)]

And without a map like:

arr = [[int(k) for k in input("Input score").strip().split())[:l]] 
       for i in range(t) for j in range(n)]
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, did not mention that am not supposed to use map().
No, a geeksforgeeks site practice sample question, not even contest.
But need arr[i][j] to have row-wise sum of values. Please tell me how to use arr in a loop for processing row-wise or indl. values. Please see the edit above.
1

We can do a nested list comprehension as follows:

t = int(input("Input number of tests: ")) 
n = int(input("Input number of rows: "))#.strip().split()) 

total = [[sum([int(i) for i in input("Input score: ").split()]) for j in range(n)] for t_index in range(t)]

print(total)

Example of an input-output pair:

Input number of tests: 2
Input number of rows: 3
Input score: 1 2 3
Input score: 2 3 4
Input score: 3 4 5
Input score: 4 5 6
Input score: 5 6 7
Input score: 6 7 8
[[6, 9, 12], [15, 18, 21]]

2 Comments

I changed to : total = [[sum([int(i) for i in input("Input score: ").split()[:l]]) for j in range(n)] for t_index in range(t)]
Kindly join the chatroom for my code at : onlinegdb.com/H1mR3VjvV , for coin change problem c code using memoization. Unable to understand its workings. Please join the chatroom at : chat.stackexchange.com/rooms/89042/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.