[英]2D array in python using list of lists
I am trying to make a function to input and one to output a 2D array . 我试图使输入和输出2D数组的功能。 After some research (While working with 1D array) I found out that there is nothing such as arrays in python. 经过一番研究(使用1D数组时),我发现python中没有诸如数组之类的东西。 However, I could achieve my goal using lists. 但是,我可以使用列表实现我的目标。
The following code worked for 1D array using list: 以下代码适用于使用列表的一维数组:
def array_input(num):
for Index in range(0, num):
ind = int(input("Please enter element {0} : ".format(Index)))
array_list.append(ind)
def array_output():
for Index in range(0, len(array_list)):
print("Element {0} is {1} ".format(Index, array_list[int(Index)]))
"""print(array_list)"""
array_list = []
a = int(input("Please enter the length of the array"))
array_input(a)
array_output()
input("Pres any key to continue")
The following is what I wrote for 2D arrays using list of lists: output is working, however input is not . 以下是我使用列表列表为2D数组编写的内容:输出有效,但输入无效。 Can anyone help me with figuring out how i can add to the lists of lists new elements (kind of like a 2D matrix)? 谁能帮我弄清楚如何将新元素添加到列表中(有点像2D矩阵)?
def array_input(row, column):
print(array_list)
for R in range(0, row):
for C in range(0, column):
ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
array_list[R][C] = ind
def array_output(row, column):
for R in range(0, row):
for C in range(0, column):
print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
print(array_list)
array_list = [[]]
a = int(input("Please enter the number of rows of the array"))
b = int(input("Please enter the number of columns of the array"))
array_input(a, b)
array_output(a, b)
input("Pres any key to continue")
def array_input(row, column):
for R in range(0, row):
for C in range(0, column):
ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
array_list[R][C] = ind
print(ind)
def array_output(row, column):
for R in range(0, row):
for C in range(0, column):
print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
print(array_list)
a, b = 2, 2;
array_list = [[0 for x in range(a)] for y in range(b)]
array_input(2,2)
array_output(2,2)
This should work for you. 这应该为您工作。 You can of course switch the assignment of a
and b
to the users input. 当然,您可以将a
和b
的分配切换到用户输入。 The important part is the assignment of the array_list variable. 重要的部分是array_list变量的分配。 Hope this ansers your question. 希望这能回答您的问题。
This will allow you the most flexibility over what you can do with your array 这将使您在阵列处理方面拥有最大的灵活性
def array_input(row, column):
for R in range(0, row):
for C in range(0, column):
ind = int(input("Please enter element ({0},{1}) : ".format(R, C)))
array_list[R][C] = ind
print(ind)
def array_output(row, column):
for R in range(0, row):
for C in range(0, column):
print("Element ({0},{1}) is {2} ".format(R, C, array_list[int(R)][int(C)]))
print(array_list)
def create_array(row,column):
array_list=[]
for R in range(0,row):
array_list.append([])
for C in range(0,column):
array_list[R].append(0)
return array_list
a = int(input("Please enter the number of rows of the array "))
b = int(input("Please enter the number of columns of the array "))
array_list= create_array(a,b)
array_input(a,b)
array_output(a,b)
Happy coding! 编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.