【发布时间】:2015-07-15 01:46:08
【问题描述】:
抱歉,我无法更好地描述问题名称 - 我真的不知道问题可能是什么,因此我不能非常具体。
在 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]]
现在出了问题:第一个属性循环中的值仍然存在,并且属性子列表得到扩展。这真的很奇怪,因为我认为由于我之前创建的组合列表,我为函数提供了一个“新鲜”的数据集来使用。有谁知道我的错误在哪里?
提前致谢!
【问题讨论】: