为什么在此处使用嵌套循环时 python 打印出奇怪的结果?

时间:2021-01-12 10:21:17

标签: python arrays loops nested-loops

我得到了一个字符串数组,我想将每个单独的字符串拆分为单独的字符并将它们存储在单独的二维数组中。我写了以下代码:-

# The given array
grid = ['1112', '1912', '1892', '1234']

# Creating a new 2D array
mat = [[None]*len(grid)]*len(grid)
for i in range(0,len(grid)):
    for j in range(0,len(grid)):
        mat[i][j] = grid[i][j]
print(mat)

但是这样做会在我的二维数组 mat 中产生奇怪的值。二维数组 mat 的每一行都被打印为 ['1','2','3','4'] 但这应该只是最后一行的值。 >

如果有人能回答我的问题,我将不胜感激。

3 个答案:

答案 0 :(得分:3)

mat = [[None]*len(grid)]*len(grid)

此语句正在创建一个关系二维数组(矩阵),因此无论何时更新一行,它都会更新所有其他行。

您应该使用它来创建一个空的二维数组:

mat = [[None for i in len(grid)] for j in range(len(grid))]

正如@marc 所评论的,您还可以在此处传递一个列表理解为宽度 = 高度

mat = [[None]*len(grid) for _ in range(len(grid))]

答案 1 :(得分:1)

对列表的乘法在内存中产生相同的引用,代码固定如下:

# The given array
grid = ['1112', '1912', '1892', '1234']

# Creating a new 2D array
# mat = [[None]*len(grid)]*len(grid)
mat = []
for i in range(len(grid)):
    t = []
    for j in range(len(grid)):
        t.append(None)
    mat.append(t)

for i in range(0,len(grid)):
    for j in range(0,len(grid)):
        mat[i][j] = grid[i][j]
print(mat)

答案 2 :(得分:1)

我必须同意在这个问题中很容易混淆。

假设 [2021-01-12 11:38:58,095] {dagbag.py:440} INFO - Filling up the DagBag from /dev/null [2021-01-12 11:38:59,049] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.dataprep.GoogleDataprepHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,050] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,050] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLDatabaseHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,050] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.compute_ssh.ComputeEngineSSHHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,050] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.bigquery.BigQueryHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,051] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.common.hooks.base_google.GoogleBaseHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.common' [2021-01-12 11:38:59,117] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.dataprep.GoogleDataprepHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,117] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,118] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLDatabaseHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,118] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.compute_ssh.ComputeEngineSSHHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,118] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.cloud.hooks.bigquery.BigQueryHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.cloud' [2021-01-12 11:38:59,118] {providers_manager.py:283} WARNING - Exception when importing 'airflow.providers.google.common.hooks.base_google.GoogleBaseHook' from 'apache-airflow-providers-google' package: No module named 'airflow.providers.google.common' = 2,所以我们得到 len(grid)

这里mat = [[None]*2]*2指向mat[0]的同一个pobject,因为mat[1]给出了两个指向同一个数组的指针。您可以使用 *2 来创建两个不同的对象 - for _ in range(2)

我们可以使用[[None]*len(grid) for _ in range(len(grid))]打印对象的地址来验证:

id