具有多个 2D 列表的 Python 列表将相同的值附加到每个值的第一行

问题描述 投票:0回答:2

我有一个这样的清单:

a = [[[0, 1], [1, 1]], [[1, 0], [1, 1]]]

这意味着我在列表中有两个 2D 列表:

a[0] = [0, 1], [1, 1]           

a[1] = [1, 0], [1, 1]

我想同时将相同的值附加到两个二维列表的第一行和第二行(假设为 [0, 1]);结果是这样的:

a[0] = [0, 1, 0], [1, 1, 1]           


a[1] = [1, 0, 0], [1, 1, 1]

我不想这样做,因为我想减少循环所花费的时间。

我一直是这样做的:

        elif (rown == 0) and (columnn > 0):
            s_routine = time.time()
            p_temp = p[:]
            p = []
            for matrix in p_temp:
                ca = matrix[0][columnn]
                cb = matrix[1][columnn]
                if columnv == 0:
                    zl = zfc[str(ca)+str(cb)]
                    for z in zl:
                        a = matrix[0] + [z[0][1]]
                        b = matrix[1] + [z[1][1]]
                        c = [a, b]
                        p.append(c)
                elif columnv == 1:
                    ol = zfc[str(ca)+str(cb)]
                    for o in ol:
                        a = matrix[0] + [o[0][1]]
                        b = matrix[1] + [o[1][1]]
                        c = [a, b]
                        p.append(c)
            e_routine = time.time() - s_routine

但是矩阵增长太多,因此循环占用了很多时间。

list python-2.7 matrix 2d
2个回答
0
投票

我认为你可以更容易地做到这一点,例如:

a = [[[0, 1], [1, 1]], [[1, 0], [1, 1]]]

for row in a:
    for subl, v in zip(row, [0, 1]):
        subl.append(v)

print(a)

打印:

[[[0, 1, 0], [1, 1, 1]], [[1, 0, 0], [1, 1, 1]]]

0
投票

我正在使用这段代码,如果我有一个低于 10 列的 bool 矩阵,效果会很好,但之后时间会增加很多,我认为问题是我的列表增长很多,每个循环需要 4 秒:

将 numpy 导入为 np 导入迭代工具 导入时间

定义解(g): 开始 = 时间.time() 零列表 = [[[0, 1], [1, 1]], [[1, 0], [1, 1]], [[1, 1, ], [1, 0]], [[1, 1, ], [0, 1]]] one_list = [[[0, 0], [1, 1]], [[0, 1], [1, 0]], [[0, 1], [0, 1]], [[1, 0] ], [1, 0]], [[1, 0], [0, 1]], [[1, 1], [0, 0]], [[1, 1], [1, 1]] , [[0, 0], [0, 0]], [[0, 0], [1, 0]], [[0, 0], [0, 1]], [[1, 0], [ 0, 0]], [[0, 1], [0, 0]]]

zfc = {'00': [], '01': [[[0, 1], [1, 1]]], '10': [[[1, 1], [0, 1]]], '11': [[[1, 0], [1, 1]], [[1, 1], [1, 0]]]}
ofc = {'00': [[[0, 0], [0, 0]], [[0, 0], [0, 1]], [[0, 1], [0, 1]], [[0, 1], [0, 0]]], '01': [[[0, 0], [1, 1]], [[0, 1], [1, 0]], [[0, 0], [1, 0]]],
       '10': [[[1, 1], [0, 0]], [[1, 0], [0, 1]], [[1, 0], [0, 0]]], '11': [[[1, 1], [1, 1]], [[1, 0], [1, 0]]]}

zc = {'011': [[[1, 0], [1, 1]]], '101': [[[0, 1], [1, 1]]], '110': [[[1, 1], [0, 1]]], '111': [[[1, 1], [1, 0]]], '000':[], '001':[], '010':[], '100':[]}
oc = {'000': [[[0, 0], [0, 1]], [[0, 0], [0, 0]]], '001': [[[0, 0], [1, 1]], [[0, 0], [1, 0]]], '010': [[[1, 0], [0, 1]], [[1, 0], [0, 0]]], '011': [[[1, 0], [1, 0]]],
      '100': [[[0, 1], [0, 1]], [[0, 1], [0, 0]]], '101': [[[0, 1], [1, 0]]], '110': [[[1, 1], [0, 0]]], '111': [[[1, 1], [1, 1]]]}

current = np.array(g).astype(int)
current = np.where((current == 0) | (current == 1), current ^ 1, current)

p = []
for rown, rowv in enumerate(current):
    for columnn, columnv in enumerate(rowv):
        if (rown == 0) and (columnn == 0):
            if columnv == 0:
                p = zero_list[:]
            else:
                p = ones_list[:]
        elif (rown == 0) and (columnn > 0):
            s_routine = time.time()
            p_temp = p[:]
            p = []
            for matrix in p_temp:
                ca = matrix[0][columnn]
                cb = matrix[1][columnn]
                if columnv == 0:
                    zl = zfc[str(ca)+str(cb)]
                    for z in zl:
                        a = matrix[0] + [z[0][1]]
                        b = matrix[1] + [z[1][1]]
                        c = [a, b]
                        p.append(c)
                elif columnv == 1:
                    ol = zfc[str(ca)+str(cb)]
                    for o in ol:
                        a = matrix[0] + [o[0][1]]
                        b = matrix[1] + [o[1][1]]
                        c = [a, b]
                        p.append(c)
            e_routine = time.time() - s_routine
            print(e_routine)
        elif (rown > 0) and (columnn == 0):
            p_temp = p[:]
            p = []
            for matrix in p_temp:
                cl = matrix[rown][0]
                cr = matrix[rown][1]
                if columnv == 0:
                    zl = z_c_fcolumn(cl, cr)
                    for z in zl:
                        a = z[1][0]
                        b = z[1][1]
                        c = [a, b]
                        d = matrix + [c]
                        p.append(d)
                elif columnv == 1:
                    ol = o_c_fcolumn(cl, cr)
                    for o in ol:
                        a = o[1][0]
                        b = o[1][1]
                        c = [a, b]
                        d = matrix + [c]
                        p.append(d)
        elif (rown > 0) and (columnn > 0):
            p_temp = p[:]
            p = []
            for matrix in p_temp:
                ur = matrix[rown][columnn+1]
                ul = matrix[rown][columnn]
                dl = matrix[rown+1][columnn]
                if columnv == 0:
                    zl = zc[str(ur)+str(ul)+str(dl)]
                    for z in zl:
                        a = matrix[rown + 1] + [z[1][1]]
                        b = matrix[:-1] + [a]
                        p.append(b)
                elif columnv == 1:
                    ol = oc[str(ur)+str(ul)+str(dl)]
                    for o in ol:
                        a = matrix[rown + 1] + [o[1][1]]
                        b = matrix[:-1] + [a]
                        p.append(b)

end = time.time()
print(end - start)
return len(p)

g = [[真,真,假,真,假,真,假,真,真,假,真,假,真,假,真,假,真,真,假,真,真,假,假,假,假,真,真,真,假,真,假,真,假,真,假,真,真,假]] 打印(溶液(g))

© www.soinside.com 2019 - 2024. All rights reserved.