【发布时间】:2021-03-02 14:32:47
【问题描述】:
所以我有 4 个列表,其中 2 个在函数内部创建,2 个在函数外部创建。
list_3 = []
list_4 = []
def create_list_3_4():
a = 4
b = 5
list_3.append(a)
list_3.append(b)
c = 8
d = 9
list_4.append(c)
list_4.append(d)
return list_3, list_4
list_1 = [10.5, 8.5]
list_2 = [12.6, 11.5]
value_1 = [x[0] for x in list_1] - [x[0] for x in list_2]
value_2 = [x[0] for x in create_list_3_4()] - [[x[0] for x in create_list_3_4()]]
value_3 = value_2 / value_1
new_list = []
new_list.append(value_3)
print(new_list)
对于list_2 中的每个项目,我想减去list_1 中的每个项目。所以12.6 - 10.5 和11.5 - 8.5 等.. 对于函数内的列表,我也想做同样的事情。所以8 - 4 和9 - 5 等等...
最终我想将12.6 - 10.5 和8 - 4 的答案分开并将其放入
new_list.
这是我当前的输出:
value_1 = [x[0] for x in list_1] - [x[0] for x in list_2]
TypeError: 'float' object is not subscriptable
这是我想要的输出:
[1.904, 1.333]
计算的额外信息:
1.904 = (8 - 4 = 4 / 12.6 - 10.5 = 2.1)
1.333 = (9 - 5 = 4 / 11.5 - 8.5 = 3)
【问题讨论】:
标签: python list integer calculation