Skip to main content
 首页 » 编程设计

Python:函数中的列表处理

2024年10月25日23lexus

抱歉,我无法更好地描述问题名称 - 我真的不知道问题是什么,因此我不能非常具体。

在 Python 中,我有以下代码:

# input 
list_environments = ["tree", "bush"] 
list_fruit = ["banana", "kiwi"] 
list_properties = [ 
                   ["_size", [["width"], ["height"], ["depth"]]], 
                   ["_colour", [["rgb_r"], ["rgb_g"], ["rgb_b"]]], 
                   ["_shape", [["is_round"], ["is_square"], ["is_flat"]]] 
                   ] 
 
# create list of all combinations: [environment, fruit, properties set] 
list_combinations = [] 
for env in list_environments: 
    for fruit in list_fruit: 
        for prop in list_properties: 
            list_combinations.append([env, fruit, prop]) 
 
# define function 
def process_entry(inp_environment, inp_fruit, inp_property): 
 
    # print data at start of function 
    print(".") 
    print("Dataset before processing:", inp_environment, inp_fruit, inp_property) 
 
    # - create temporary list for property value pairs - 
    tmp_list = inp_property[1] # takes only the sublist from property list 
 
    # add info for size 
    if inp_property[0] == "_size": 
        tmp_list[0].append(1) # width is 1  
        tmp_list[1].append(2) # height is 2 
        tmp_list[2].append(3) # depth is 3 
    # add info for colour 
    elif inp_property[0] == "_colour": 
        tmp_list[0].append(100) # r-value is 100 
        tmp_list[1].append(101) # g-value is 101 
        tmp_list[2].append(102) # b-value is 102 
    # add info for shape  
    elif inp_property[0] == "_shape": 
        tmp_list[0].append(0) # is not round 
        tmp_list[1].append(0) # is not square 
        tmp_list[2].append(1) # is flat 
 
    print("Dataset now:", inp_environment, inp_fruit, tmp_list)     
 
# call processing function for every entry in combination list 
for entry in list_combinations: 
    process_entry(entry[0], entry[1], entry[2]) 

我现在期望的是,该函数会将(静态)值分配给末尾的 for 循环的每个组合。相反,分配的值与属性列表一起保存,并且输出总是由另一个值集扩展。输出如下所示:

. 
Dataset before processing: tree banana ['_size', [['width'], ['height'],['depth']]] 
Dataset now: tree banana [['width', 1], ['height', 2], ['depth', 3]] 
. 
Dataset before processing: tree banana ['_colour', [['rgb_r'], ['rgb_g'], ['rgb_b']]] 
Dataset now: tree banana [['rgb_r', 100], ['rgb_g', 101], ['rgb_b', 102]] 
. 
Dataset before processing: tree banana ['_shape', [['is_round'], ['is_square'], ['is_flat']]] 
Dataset now: tree banana [['is_round', 0], ['is_square', 0], ['is_flat', 1]] 

-> 到目前为止,第一次循环遍历属性后一切正常。

现在……:

Dataset before processing: tree kiwi ['_size', [['width', 1], ['height', 2], ['depth', 3]]] 
Dataset now: tree kiwi [['width', 1, 1], ['height', 2, 2], ['depth', 3, 3]] 
. 
Dataset before processing: tree kiwi ['_colour', [['rgb_r', 100], ['rgb_g', 101], ['rgb_b', 102]]] 
Dataset now: tree kiwi [['rgb_r', 100, 100], ['rgb_g', 101, 101], ['rgb_b', 102, 102]] 
. 
Dataset before processing: tree kiwi ['_shape', [['is_round', 0], ['is_square', 0], ['is_flat', 1]]] 
Dataset now: tree kiwi [['is_round', 0, 0], ['is_square', 0, 0], ['is_flat', 1, 1]] 

现在出了问题:第一个属性循环的值仍然存在,属性子列表得到扩展。这真的很奇怪,因为我认为由于我之前创建的组合列表,我正在为函数提供一个"new"数据集来处理。有人看到我的错误在哪里吗?

提前致谢!

请您参考如下方法:

问题出在这一行:tmp_list = inp_property[1]。这只是简单地为现有列表赋予一个新名称,然后您可以继续修改它,从而修改原始列表。实际上问题更深层次,因为你随后附加到子列表。要解决此问题,只需导入复制模块并将其更改为 tmp_list = copy.deepcopy(inp_property[1])。