0

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)
3
  • 2
    What have you tried so far? Please post your code. Commented Sep 27, 2019 at 13:06
  • Please edit your post, as opposed to adding code in the comments Commented Sep 27, 2019 at 13:16
  • You can solve the issue in your code using a list of lists instead. Commented Sep 27, 2019 at 13:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess that %k on input should be % a + 1 instead, right?
def main(): arrays = list() a = int(input("Enter number of arrays: ")) k = 0 for _ in range(a): arrays.append(list(map(int, input("Enter %s'st array"%k).split()))) for array in arrays: print(array)
for _ in range(a): NameError: name 'a' is not defined
Have edited the code and added another note to the post as I noticed another bug.
0
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)

Hope this will help you

7 Comments

can you please explain arrays=dict()?
dict is a build-in class I have created the empty dict object with reference arrays, instead of doing, arrays = {}
instead of dict can we use list?
{'array_1': [1], 'array_2': [2], 'array_3': [3]} how to get a specific output let . say i want values of 'array_3'???
Yes, you can use list. Then your output will look like, [[1,2,3], [4,5,6]] for that: my_array_list = list() for i in range(1, a+1): my_array_list.append(list(map(lambda j: int(input('value: ')), [ j for j in range(size)] )))
|

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.