Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的随机大小列表生成_Python_List - Fatal编程技术网

Python中的随机大小列表生成

Python中的随机大小列表生成,python,list,Python,List,我想用Python创建一个随机大小列表 这是我的密码: row_num = int(input('Please enter matrix row number:')) colum_num = int(input('please enter matrix column number:')) a = [[0]*row_num]*colum_num print("Please enter matrix: ") for i in range(colum_num): for j in range(

我想用Python创建一个随机大小列表

这是我的密码:

row_num = int(input('Please enter matrix row number:'))
colum_num = int(input('please enter matrix column number:'))
a = [[0]*row_num]*colum_num
print("Please enter matrix: ")
for i in range(colum_num):
    for j in range(row_num):
        a[i][j]=int(input())

print(a)
例如,我想生成一个2*3矩阵,下面是我的结果:

Please enter matrix row number:2
please enter matrix column number:3
Please enter matrix: 
1
2
3
4
5
6
[[5, 6], [5, 6], [5, 6]]
我希望结果是
[[1,2],[3,4],[5,6]]


为什么结果是最后一个元素?

您必须创建一个包含列表列表的列表

# Create a list containing lists initialised to 0
a = [[0 for x in range(row_num)] for x in range(column_num)]
您的代码实际上生成了一个列长度列表,其元素都包含对同一行长度列表的引用。基于您的代码,您可以在以下代码中看到这一点:

row_num = 2                                                                     
colum_num = 3                                                                   

a = [[0 for x in range(row_num)] for x in range(colum_num)]                     
b = [[0]*row_num]*colum_num                                                     

assert a == b                                                                   

count = 0                                                                       
for i in range(colum_num):                                                      
    for j in range(row_num):                                                    
        a[i][j] = count                                                         
        b[i][j] = count                                                         
        print "a",a                                                             
        print "b",b                                                             
        print                                                                   
        count += 1
产生:

a [[0, 0], [0, 0], [0, 0]]
b [[0, 0], [0, 0], [0, 0]]

a [[0, 1], [0, 0], [0, 0]]
b [[0, 1], [0, 1], [0, 1]]

a [[0, 1], [2, 0], [0, 0]]
b [[2, 1], [2, 1], [2, 1]]

a [[0, 1], [2, 3], [0, 0]]
b [[2, 3], [2, 3], [2, 3]]

a [[0, 1], [2, 3], [4, 0]]
b [[4, 3], [4, 3], [4, 3]]

a [[0, 1], [2, 3], [4, 5]]
b [[4, 5], [4, 5], [4, 5]]
另一种方法可能是使用提供现成数组原语的方法:

import numpy
# Returns a new array of given shape and type, filled with zeros.
a = numpy.zeros((2, 3), dtype=numpy.int)
print a
[[0 0 0]
 [0 0 0]]

代码中的问题是,当您执行类似操作时:

a = [[0] * row_num] * colum_num
row_num = int(input('Please enter matrix row number:'))
column_num = int(input('please enter matrix column number:'))
a = [[int(input()) for x in range(column_num)] for y in range(row_num)]
您可以创建到列表
[[0]*行数]
的列数链接。当您更改其中一个列表(在的
中)时,其他列表也会更改。您可以运行以下代码来了解我的意思

a = [1,2,3]
b = [a] * 3
print(b)
a[1] = 10
print(b)
输出将是
[[1,2,3],[1,2,3],[1,2,3]]
[[1,10,3],[1,10,3],[1,10,3]
,因为b只是指向a的3个链接

有多种方法可以创建新列表而不是副本,例如为完整列表编制索引

b = [a[:]] * 3 
或使用
复制
模块

import copy
b = [copy.copy(a)] * 3
或者使用生成器表达式创建多个相似的列表(充满
0
),但它们是不同的对象,如sgbirch建议的

a = [[0 for x in range(column_num)] for y in range(row_num)]

顺便说一句,没有必要创建一个满是
0
的列表,您可以这样做:

a = [[0] * row_num] * colum_num
row_num = int(input('Please enter matrix row number:'))
column_num = int(input('please enter matrix column number:'))
a = [[int(input()) for x in range(column_num)] for y in range(row_num)]
对于用户来说,逐行输入矩阵可能更简单

a = [[int(x) for x in input('Input row № {} ({} numbers): '.format(y + 1, column_num)).split()] for y in range(row_num)]

已经给出了完整的答案,但关键是try:a=[[0]*3]*3#n打印(a)#n a[1][1]=“相同”#n打印(a)#n@Joop,由于我在答覆中提出的理由,同样的问题仍会存在。试着在空闲状态下运行它。我并没有假装给出答案,只是简单地说明了问题,就像你们在答案中所做的那个样