I Want to get multiple arrays, For example :
a(number of arrays)
Suppose let's say a=3
arr1[1,4,8,9....n]
arr2[2,4,9.....n]
arr3[2,6...n]
The values should be user inputs.
The issue here is the array will be repeated and the last value entered will be stored only..
Code so far:
def main():
a=input("Enter the number of array's: ")
k=1
for _ in range(a):
array = list(map(int, input("Enter %s'st array"%k).split()))
print(array)
-
2What have you tried so far? Please post your code.James– James2019年09月27日 13:06:51 +00:00Commented Sep 27, 2019 at 13:06
-
Please edit your post, as opposed to adding code in the commentsJames– James2019年09月27日 13:16:05 +00:00Commented Sep 27, 2019 at 13:16
-
You can solve the issue in your code using a list of lists instead.accdias– accdias2019年09月27日 13:23:28 +00:00Commented Sep 27, 2019 at 13:23
2 Answers 2
The issue here is this part:
for _ in range(a):
array = list(map(int, input("Enter %s'st array"%k).split()))
Every time you assign to array you overwrite the previous value that you stored. In order to keep all the arrays you create, you'll have to keep them in a separate list. Try something like this:
arrays = list()
for i in range(int(a)):
arrays.append(list(map(int, input("Enter %s'st array" % (i + 1)).split())))
for array in arrays:
print(array)
EDIT: You also weren't incrementing k, which means "Enter 1'st array" would come up for every prompt. Since you are in a loop anyway, you can use the loop variable (I have added this in as i) as the number in this prompt. You have to add 1 so that it starts at 1 and goes up, rather than starting at 0. Thanks for spotting that @accdias.
Also, you need to pass an integer to range(), and when you get input from the command line it will come in as a string. So, just call int() on it before passing it to range. I have edited the code above to reflect this.
4 Comments
%k on input should be % a + 1 instead, right?def main():
a=int(input("Enter the number of array's: "))
size=int(input("Each array size: "))
arrays=dict()
for i in range(1, a+1):
# arrays['array_'+str(i)]=[ j for j in range(1, size) lamda x: ]
arrays['array_'+str(i)] = list(map(lambda j: int(input('value: ')), [ j for j in range(size)] ))
print(arrays)