繁体   English   中英

我想将“list_a”添加到“list_b”

[英]i want to add "list_a" to "list_b"

temp_list = [2,2,9]
var_a = 2

list_a = []
print("first temp_list : ",temp_list)
print()

while temp_list[-1] >= var_a:
    before_end_value = temp_list[-1]
    temp_list[-1] = var_a
    temp_list.append(before_end_value - var_a)
    list_a.append(temp_list)
    print("temp_list", temp_list)
    print("list_a : ", list_a)
    print()

- - -输出 - - -

first temp_list :  [2, 2, 9]

temp_list [2, 2, 2, 7]
list_a :  [[2, 2, 2, 7]]

temp_list [2, 2, 2, 2, 5]
list_a :  [[2, 2, 2, 2, 5], [2, 2, 2, 2, 5]]

temp_list [2, 2, 2, 2, 2, 3]
list_a :  [[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2, 3]]

temp_list [2, 2, 2, 2, 2, 2, 1]
list_a :  [[2, 2, 2, 2, 2, 2, 1], [2, 2, 2, 2, 2, 2, 1], [2, 2, 2, 2, 2, 2, 1], [2, 2, 2, 2, 2, 2, 1]]

我想制作 list_a [[2,2,2,7],[2,2,2,2,5],[2,2,2,2,2,3],[2,2,2,2 ,2,2,1]] (为了解决这个问题,我使用了 list_a.append(temp_list) 但 list_a 有错误的值)

你能建议这个问题吗?

感谢您的最新编辑,这是解决方案:

temp_list = [2,2,9]
var_a = 2

list_a = []
print("first temp_list : ",temp_list)
print()

while temp_list[-1] >= var_a:
    before_end_value = temp_list[-1]
    temp_list[-1] = var_a
    temp_list.append(before_end_value - temp_list[-1])
    list_a.append(temp_list.copy())
    print("temp_list", temp_list)
    print("list_a : ", list_a)
    print()

只需将list_a.append(temp_list)替换为list_a.append(temp_list.copy())

Output:

first temp_list :  [2, 2, 9]

temp_list [2, 2, 2, 7]
list_a :  [[2, 2, 2, 7]]

temp_list [2, 2, 2, 2, 5]
list_a :  [[2, 2, 2, 7], [2, 2, 2, 2, 5]]

temp_list [2, 2, 2, 2, 2, 3]
list_a :  [[2, 2, 2, 7], [2, 2, 2, 2, 5], [2, 2, 2, 2, 2, 3]]

temp_list [2, 2, 2, 2, 2, 2, 1]
list_a :  [[2, 2, 2, 7], [2, 2, 2, 2, 5], [2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2, 2, 1]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM