【问题标题】:Store result from loop in nested list将循环结果存储在嵌套列表中
【发布时间】:2020-09-11 19:18:34
【问题描述】:

我有这个插入排序算法,它产生jikeyA。所有这些我想存储在一个单独的列表中。但是当我尝试存储 A 时,嵌套列表会返回这个。

store_j = []
store_i = []
store_key = []
store_A = []
def insertion_sort(A):
    for j in range(1, len(A)):
        key = A[j]
        i = j - 1
        store_j.append(j)
        store_i.append(i)
        store_key.append(key)
        store_A.append(A)
        while i >= 0 and A[i] > key:
            A[i + 1] = A[i]
            i = i - 1
            A[i + 1] = key
    print(store_i)
    print(store_j)
    print(store_key)
    print(store_A)

a = [5, 1, 3, 4, 2]
insertion_sort(a)

[0, 1, 2, 3]
[1, 2, 3, 4]
[1, 3, 4, 2]
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]

我希望最后一个 store_A 像这样输出

[[5, 1, 3, 4, 2][1, 5, 3, 4, 2][1, 3, 5, 4, 2][1, 3, 4, 5, 2][1, 2, 3, 4, 5]]

所以我最终可以用存储的值制作一个表格

类似的东西

j | i | key|      A        |
1 | 0 | 1  |[5, 1, 3, 4, 2]|
2 | 1 | 3  |[1, 5, 3, 4, 2]|
3 | 2 | 4  |[1, 3, 5, 4, 2]|
4 | 3 | 2  |[1, 3, 4, 5, 2]|
4 | 0 | 2  |[1, 2, 3, 4, 5]|

【问题讨论】:

标签: python python-3.x arraylist datatable nested-lists


【解决方案1】:

如果我理解你的问题,你想要一个列表的所有排列。

import numpy as np
import itertools

permutations = list(itertools.permutations([5, 2, 3, 1]))
print (permutations)    # list of tuples
perm_array = np.array(permutations)
print(perm_array)       # yields 2D array

【讨论】:

    【解决方案2】:

    在存储之前复制数组。否则,您将保存数组 A 的实例,这就像将 A 存储 5 次或 len(A) 次一样。并且 A 将在每次迭代中发生变化,当您打印 store_A 时,您将在那时获得 A 的值。要理解这一点,您可以在store_A.append(A) 之后添加print(store_A)。这会给你

    [[5, 1, 3, 4, 2]]
    [[1, 5, 3, 4, 2], [1, 5, 3, 4, 2]]
    [[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]
    [[1, 3, 4, 5, 2], [1, 3, 4, 5, 2], [1, 3, 4, 5, 2], [1, 3, 4, 5, 2]]
    

    现在,解决方法很简单,只需在添加到 store 之前创建实例 a 的 acope 并在循环后添加一行到 append(A) 以附加最后一个结果。

    store_j = []
    store_i = []
    store_key = []
    store_A = []
    def insertion_sort(A):
        for j in range(1, len(A)):
            key = A[j]
            i = j - 1
            store_j.append(j)
            store_i.append(i)
            store_key.append(key)
            store_A.append(A.copy())
            while i >= 0 and A[i] > key:
                A[i + 1] = A[i]
                i = i - 1
                A[i + 1] = key
    
        store_j.append(j)
        store_i.append(i)
        store_key.append(key)
        store_A.append(A.copy())
        print(store_i)
        print(store_j)
        print(store_key)
        print(store_A)
    
    a = [5, 1, 3, 4, 2]
    insertion_sort(a)
    

    出来会像

    [0, 1, 2, 3, 0]
    [1, 2, 3, 4, 4]
    [1, 3, 4, 2, 2]
    [[5, 1, 3, 4, 2], [1, 5, 3, 4, 2], [1, 3, 5, 4, 2], [1, 3, 4, 5, 2], [1, 2, 3, 4, 5]]
    

    也可以添加以下代码来制作表格

    template='|{0:^5}|{1:^5}|{2:^5}|{3:^18}|'
    print(template.format('i','j','key','A'))
    for i in range (len(store_A)):
        print(template.format(f'{store_j[i]}',f'{store_i[i]}',f'{store_key[i]}',f'{store_A[i]}'))
    

    结果会是这样的

    |  i  |  j  | key |        A         |
    |  1  |  0  |  1  | [5, 1, 3, 4, 2]  |
    |  2  |  1  |  3  | [1, 5, 3, 4, 2]  |
    |  3  |  2  |  4  | [1, 3, 5, 4, 2]  |
    |  4  |  3  |  2  | [1, 3, 4, 5, 2]  |
    |  4  |  0  |  2  | [1, 2, 3, 4, 5]  |
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 2021-04-12
      • 1970-01-01
      • 2013-06-05
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多