如何从输入数据创建二维列表? [英] How to create a 2d list from a input data?
本文介绍了如何从输入数据创建二维列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试
n = int(input())
m = int(input())
print(n,m)
for i in range(0,n):
for j in range(0,m):
ar[i][j] = int(input())
for i in range(0,n):
for j in range(0,m):
print (ar[i][j])
但是它显示了一个错误
Traceback (most recent call last):
File "C:/Users/shivansh/Desktop/test.py", line 6, in <module>
ar[i][j] = int(input())
NameError: name 'ar' is not defined
我曾经用C语言做过同样的事情,但是它可以工作.那么,如何在Python中做到这一点?
I used to do the same in C language but it works. So, how to this in Python?
推荐答案
您尚未声明ar
.在Python中,您不必执行单独的声明和初始化.但是,您不能对willn-nilly名称进行操作.
You haven't declared ar
yet. In Python, you don't have to perform separate declaration and initialization; nevertheless, you can't perform operations on names willy-nilly.
从这样的东西开始:
ar = [[0 for j in range(m)] for i in range(n)]
这篇关于如何从输入数据创建二维列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文