使用Python中的循环构建列表列表

时间:2018-01-24 15:56:21

标签: python list loops

我正在尝试使用plotly api构建与python中的跟踪可见性相关联的长布尔元素列表。

我的意见是:

list_true = list( np.ones(3, dtype=bool) )
list_false = list( np.zeros(3, dtype=bool) )
blocks = [1,2,3]

我应该如何定义构建列表列表的循环,以便list_true块的位置根据索​​引blocks[i]的值而变化?输出应该是:

combination1 = list_true + list_false + list_false
combination2 = list_false + list_true + list_false
combination3 = list_false + list_false + list_true

即:

combination1 = [
True, True, True,
False, False, False,
False, False, False,
]
combination2 = [
False, False, False,
True, True, True,
False, False, False,
]
combination3 = [
False, False, False,
False, False, False,
True, True, True,
]

5 个答案:

答案 0 :(得分:1)

import numpy as np

list_true = list(np.ones(3, dtype=bool))
list_false = list(np.zeros(3, dtype=bool))
blocks = [1,2,3]

combination = []
for i in blocks:
    sub_combination = [list_false for k in range(3)]
    sub_combination[i-1] = list_true
    combination.append(sub_combination [0] + sub_combination [1] + sub_combination [2])

print('combination1')
print(combination[0])

print('combination2')
print(combination[1])

print('combination3')
print(combination[2])

答案 1 :(得分:0)

如果我理解正确并且组合应该是列表列表(矩阵基本)你可以这样做(为了更优雅地解决它我没有分离组合变量但是列出了组合):

import numpy as np

list_true = list(np.ones(3, dtype=bool))
list_false = list(np.zeros(3, dtype=bool))
blocks = [1,2,3]

combination = []
for i in range(len(blocks)):
    combination.append([list_true, list_true, list_true])
    combination[i][i] = list_false

print(combination)

答案 2 :(得分:0)

您可以定义通用函数以在N的位置插入列表并将其应用于您的真实和错误列表。好处是你不仅限于你的例子中的3个列表。您可以拥有任意数量的列表(更改用户定义的总数n并将其用作函数put_true的参数)。

您还可以为该功能提供您想要的任何列表。你可以决定你感兴趣的是哪一个(选择L_insert,你的例子中的真实列表)以及你将用来填充空白的那些(选择L2,你的例子中的假列表)。

import numpy as np
list_true = list(np.ones(3, dtype=bool) )
list_false = list(np.zeros(3, dtype=bool) )
blocks = [1,2,3]

# generic function to insert list L_insert among L2s
# which list to be inserted, to which position within how many lists
# can all be defined by user
def insert_list(L_insert, L2):
    def insert_at_n(n_insert, n_total):
        if n_total == 0:
            return []
        elif n_insert == 1:
            return L_insert + insert_at_n(n_insert-1, n_total-1) 
        else:
            return L2 + insert_at_n(n_insert-1, n_total-1)
    return insert_at_n

put_true=insert_list(list_true, list_false)
total = 3 # if you want more false list to be appended, simply change this number
put_true_at_n = lambda n: put_true(n, total)

combination1 = put_true_at_n(1)
combination2 = put_true_at_n(2)
combination3 = put_true_at_n(3)
...
combinationn = put_true_at_n(n)

# an additional benefit is that you can simply apply it to a list of integers
# in order to automatize your work    
print([put_true_at_n(n) for n in blocks])

答案 3 :(得分:0)

我找到了我的问题的解决方案,它随着blocks列表的长度(按预期)而变化,并将组合(嵌套)列表展平为所需的结果。感谢@Odame Prince提示。

import numpy as np

list_true = list(np.ones(3, dtype=bool))
list_false = list(np.zeros(3, dtype=bool))
blocks = ['A','B','C','D']

for i in range(len(blocks)):
    combo_list = [list_false for k in range(len(blocks))]
    combo_list[i] = list_true    
    flat_list = [item for sublist in combo_list for item in sublist]
    print('\nCombination{}:  {}'.format(i, flat_list) )

返回:

Combination0:  [True, True, True, False, False, False, False, False, False, False, False, False]

Combination1:  [False, False, False, True, True, True, False, False, False, False, False, False]

Combination2:  [False, False, False, False, False, False, True, True, True, False, False, False]

Combination3:  [False, False, False, False, False, False, False, False, False, True, True, True]

答案 4 :(得分:0)

您还可以将此较短版本与insert

一起使用
list_true = np.ones(3, dtype=bool)
list_false = np.zeros(3, dtype=bool)

blocks = [1, 2, 3]

initial = [np.repeat(list_false, 2) for i in range(3)]
final = [np.insert(initial[b-1], (b-1)*3, list_true) for b in blocks]