如何使用其他列表列表并执行数学运算将值附加到列表列表

时间:2016-08-25 14:10:28

标签: python list if-statement for-loop append

我有几个列表清单:

crt = [[80, 90, 6, 5.4, 8, 5], [65, 58, 2, 9.7, 1, 1], [83, 60, 4, 7.2, 4, 7],
       [40, 80, 10, 7.5, 7, 10], [52, 72, 6, 2, 3, 8], [94, 96, 7, 3.6, 5, 6]]

nc = [[1, 2, 3, 4, 5, 6], [1, 1, 1, 1, 1, 1], [-1, 1, -1, -1, -1, 1],
      [2, 3, 5, 6, 1, 6], [10, 0, 0.5, 1, 0, 0], [0, 30, 5, 3, 0, 0],
      [0, 0, 0, 0, 0, 5]]

DivMatrix = [[[0, -15, 3, -40, -28, 14], [15, 0, 18, -25, -13, 29],
              [-3, -18, 0, -43, -31, 11], [40, 25, 43, 0, 12, 54],
              [28, 13, 31, -12, 0, 42], [-14, -29, -11, -54, -42, 0]],
             [[0, 32, 30, 10, 18, -6], [-32, 0, -2, -22, -14, -38],
              [-30, 2, 0, -20, -12, -36], [-10, 22, 20, 0, 8, -16],
              [-18, 14, 12, -8, 0, -24], [6, 38, 36, 16, 24, 0]],
              [[0, -4, -2, 4, 0, 1], [4, 0, 2, 8, 4, 5],
              [2, -2, 0, 6, 2, 3], [-4, -8, -6, 0, -4, -3],
              [0, -4, -2, 4, 0, 1], [-1, -5, -3, 3, -1, 0]],
             [[-0.0, 4.299999999999999, 1.7999999999999998,
               2.0999999999999996, -3.4000000000000004, -1.8000000000000003],
              [-4.299999999999999, -0.0, -2.499999999999999,
               -2.1999999999999993, -7.699999999999999, -6.1],
              [-1.7999999999999998, 2.499999999999999, -0.0,
               0.2999999999999998, -5.2, -3.6], [-2.0999999999999996,
               2.1999999999999993, -0.2999999999999998, -0.0, -5.5, -3.9],
              [3.4000000000000004, 7.699999999999999, 5.2, 5.5, 0, 1.6],
              [1.8000000000000003, 6.1, 3.6, 3.9, -1.6, -0.0]],
             [[0, -7, -4, -1, -5, -3], [7, 0, 3, 6, 2, 4],
              [4, -3, 0, 3, -1, 1], [1, -6, -3, 0, -4, -2],
              [5, -2, 1, 4, 0, 2], [3, -4, -1, 2, -2, 0]],
             [[0, 4, -2, -5, -3, -1], [-4, 0, -6, -9, -7, -5],
              [2, 6, 0, -3, -1, 1], [5, 9, 3, 0, 2, 4], [3, 7, 1, -2, 0, 2],
              [1, 5, -1, -4, -2, 0]]]

我想创建一个与DivMatrix大小相同的列表列表。创建必须以for循环开始,然后使用6个不同的if语句,每个语句代表一个带nc[3]的类型方程。我想构建第一个if语句,然后我将构建其他更复杂的语句。到目前为止我有这个算法,但它是错误的。

pref_indic = []
for x in range(len(crt)):
    if (nc[3][x] == 1):
        prolist = []
        for y in range(len(DivMatrix[x])):
            prolist2 = []
            for z in range(len(DivMatrix[x])):
                if (DivMatrix[y][z]<=0):
                    prolist2.append(0)
                else:
                    prolist2.append(1)
            prolist.append(prolist2)
            pref_indic.append(prolist)
    else:
       print "wrong function type"

print pref_indic

如果nc[3][x] = 1表示类型是类型1,那么如果DivMatrix[x]<=0中的值然后追加0,则追加1。

因此,对于等于1的nc[3][4],我将附加以下列表:

pref_indic[4] = [[0, 0, 0, 0, 0, 0], [1, 0, 1, 1, 1, 1],
                 [1, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0],
                 [1, 0, 1, 1, 0, 1], [1, 0, 0, 1, 0, 0]]

很抱歉帖子的大小。

2 个答案:

答案 0 :(得分:0)

这会回答你的问题吗?

pref_indic = []
for x in range(len(crt)):
    if nc[3][x] == 1:
        prolist = []
        for y in range(len(DivMatrix[x])):
            prolist2 = []
            for z in range(len(DivMatrix[x][y])):
                if DivMatrix[x][y][z] <= 0:
                    prolist2.append(0)
                else:
                    prolist2.append(1)
            prolist.append(prolist2)
            pref_indic.append(prolist)
    elif nc[3][x] == 2:
        # add code here
    elif nc[3][x] == 3:
        # add code here
    elif nc[3][x] == 4:
        # add code here
    # ...etc
    else:
       print "wrong function type"

print pref_indic

答案 1 :(得分:0)

这是你的代码,修改得足以做你想做的事(我猜,不确定):

var inlinePlaybackSupported = true;
var elem = document.querySelector('video');

elem.addEventListener('canplay', function () {

    //if in fullscreen here, then inline playback is not supported;
    if (elem.webkitDisplayingFullscreen) {
        inlinePlaybackSupported = false;
    }
});

现在这里的内容更具可读性(但有限,因为我不知道我操作的是哪种数据):

pref_indic = []
for x in range(len(crt)):
    if (nc[3][x] == 1):
        prolist = []
        for y in range(len(DivMatrix[x])):
            prolist2 = []
            for z in range(len(DivMatrix[x][y])):
                if (DivMatrix[x][y][z]<=0):
                   prolist2.append(0)
                else:
                   prolist2.append(1)
            prolist.append(prolist2)
        pref_indic.append(prolist)
    else:
       pref_indic.append("wrong function type")

print pref_indic

它完全相同但是:

  1. 主循环简化为:
    • 使用zip链接两个列表以迭代并直接检索所需值而不是使用索引
    • 使用function处理每个案例(仅实施第一种流程,但已存在更多功能)
    • 为变量使用“显式”名称
  2. 在函数中提取nc [3] == 1的情况下的处理代码,提高了可读性
  3. 尽可能放弃按索引访问数据
  4. 在我看来,它更具可读性和可理解性(希望你会同意)。

    使用一些Python技巧可以使process_eq_type_1更简洁:

    def process_eq_type_1(div_matrix_element):
        prolist = []
        for element_of_element in div_matrix_element:
            prolist2 = []
            for element_of_element_of_element in element_of_element:
                if element_of_element_of_element <= 0:
                    prolist2.append(0)
                else:
                    prolist2.append(1)
            prolist.append(prolist2)
        return prolist
    
    
    def process_eq_type_2(div_matrix_element):
        return "eq type 2 not implemented"
    
    
    def process_eq_type_3(div_matrix_element):
        return "eq type 3 not implemented"
    
    
    pref_indic = []
    for eq_type, unclear_data in zip(nc[3], DivMatrix):
        if (eq_type == 1):
            pref_indic.append(process_eq_type_1(unclear_data))
        elif (eq_type == 2):
            pref_indic.append(process_eq_type_2(unclear_data))
        elif (eq_type == 3):
            pref_indic.append(process_eq_type_3(unclear_data))
        else:
            pref_indic.append("wrong function type")
    
    
    print pref_indic
    

    或使用list comprehension

    def process_eq_type_1(div_matrix_element):
        prolist = []
        for element_of_element in div_matrix_element:
            prolist2 = []
            for element_of_element_of_element in element_of_element:
                prolist2.append(int(element_of_element_of_element > 0))
            prolist.append(prolist2)
        return prolist
    

    最终建议:您可能需要阅读the Python tutorial