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

2024-05-15 18:39:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个列表:

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语句,每个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]]

抱歉,邮件太大了


Tags: in列表forlenifrange语句nc
2条回答

这能回答你的问题吗

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

这是您的代码,修改得刚好可以做您想做的事情(我猜,不确定):

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

下面是一些更具可读性的内容(但由于我不知道我在处理什么样的数据而受到限制):

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

它的功能完全相同,但是:

  1. 主回路简化为:
    • 使用zip链接两个列表以迭代和直接检索所需的值,而不是使用索引
    • 对每种情况使用function进行处理(只实现了第一种处理,但已经存在更多的函数)
    • 对变量使用“显式”名称
  2. 在函数中提取nc[3]==1的情况下的处理代码,提高了可读性
  3. 尽可能不使用索引访问数据

在我看来,它的可读性和可理解性要高得多(希望你会同意)


使用一些Python技巧,您可以使流程\u eq \u type \u 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:
            prolist2.append(int(element_of_element_of_element > 0))
        prolist.append(prolist2)
    return prolist

或使用list comprehension

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

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

相关问题 更多 >