【问题标题】:How to append values to a list of lists using other list of lists and doing mathematical operations如何使用其他列表列表并将值附加到列表列表并进行数学运算
【发布时间】:2017-01-02 00:33:49
【问题描述】:

我有几个列表:

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]&lt;=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]]

抱歉帖子的大小。

【问题讨论】:

  • 请修复if (nc[3][x] == 1): prolist = []附近的缩进
  • 问题是什么?
  • @h_e_u_r_e_k_a 我想用 6 创建 pref_indic 如果,其中一个将是我想要给出上述结果的那个。 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]]
  • 然后写下另外 5 个 if。
  • @martineau 其他 5 个真的很复杂,但是如果我弄清楚如何只构建 type=1 的一个,我将轻松创建其他的。所以让 pref_indic 只有我上面写的列表作为开始,当我这样做时,我将创建其他 5 个 ifs

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


【解决方案1】:

这是你的代码,修改后刚好可以满足你的要求(我猜,不确定):

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 链接两个列表以迭代并直接检索所需的值,而不是使用索引
    • 每个case使用function处理(只实现了第一种处理,但已经有更多功能了)
    • 对变量使用“显式”名称
  2. 函数中提取nc[3] == 1情况的处理代码,提高可读性
  3. 尽可能禁止通过索引访问数据

在我看来,它更具可读性和可理解性(希望你会同意)。


使用一些 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:
            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

【讨论】:

  • 这将是下一步!我想首先创建一个带有 6 个 if 的 for 循环,然后将其转换为 6 个不同的函数! :) 真的很感谢你!是的,这正是我想做的!
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多