简体   繁体   中英

How to take input of an array in python using the array module?

So, I am new to python. Previously I used to code in C and there I used arrays extensively, but here in python we don't have the option of using arrays directly without importing the "array" module. I recently learnt about lists but I want to implement arrays in python and not lists. Here in my code I am trying to take input the dimensions of an array(2d) and then its elements and then print it. Later I am planning to take input another array and multiply it with the previous one. But every time I run this code, I am getting an error saying "Array index out of bound". I am well aware of this error but here I cant figure out what is wrong. Please help me out.

Also this is my first question on Stack overflow so please forgive me if my question framing is wrong.

from array import *

print("First array: ")

a = int(input("No. of rows: "))
b = int(input("No. of columns: "))

print("Second array")

x = int(input("No. of rows: "))
y = int(input("No. of columns: "))

if(b == x):    #Checking if multiplication is possible or not

    array1 = array('i', [])
    array2 = array('i', [])

    #1st array
    for i in range(0,a):
        for j in range(0,b):
            n1 = int(input("Enter values for first array: "))
            array1[i][j].append(n1)

     print(array1)

     #2nd array  
     for i in range(0,x):
            for j in range(0,y):
                n2 = int(input("Enter values for first array: "))
                array2[i][j].append(n2)

     print(array2)

You can use numpy arrays for such kind of tasks ( matrix multiplication and other useful things also included):

import numpy as np
import sys

print("First array: ")

a1 = int(input("No. of rows: "))
b1 = int(input("No. of columns: "))

print("Second array")

a2 = int(input("No. of rows: "))
b2 = int(input("No. of columns: "))

if b1 != a2:
    print("Wrong array size!")
    sys.exit(-1)

array1 = np.zeros((a1,b1))
array2 = np.zeros((a2,b2))

print("Enter first array:")
for x in range(0,a1):
    for y in range(0,b1):
        array1[x,y] = float(input("Enter %d %d: " % (x,y)))

print(array1)

print("Enter second array:")    
for x in range(0,a2):
    for y in range(0,b2):
        array2[x,y] = float(input("Enter %d %d: " % (x,y)))

print(array2)

The python array module is designed to represent 1-d arrays, ie lists. It doesn't support 2D or other higher dimensions because the module restricts the data type of the array elements. When you say array1 = array('i', []) , that means "create a list that only accepts integer values, and start with an empty list".

If you don't want to use numpy or other matrix libraries, you can do something like this (see comments):

print("First array: ")
a = int(input("No. of rows: "))
b = int(input("No. of columns: "))

print("Second array")
x = int(input("No. of rows: "))
y = int(input("No. of columns: "))

if (b == x):
    array1 = []  ## empty 1-D list
    array2 = []
    for i in range(0,a):
        array1.append([])  ## add a row
        for j in range(0,b):
            n1 = int(input("Enter values for first array: "))
            array1[i].append(n1)  ## add a column value to row
    print(array1)
    for i in range(0,x):
        array2.append([])
        for j in range(0,y):
            n2 = int(input("Enter values for first array: "))
            array2[i].append(n2)
    print(array2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM