我只想从两个列表中减去元素 [英] I want to simply subtract elements from two lists

查看:89
本文介绍了我只想从两个列表中减去元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个打开的列表.第一权重(fWeight)和第二权重(sWeight).我想从sWeight减去fWeight.我收到此错误:

I have two open lists. First Weight (fWeight) and Second Weight (sWeight). I want to subtract the fWeight from sWeight. I am getting this error:

unsupported operand type(s) for -: 'list' and 'list'.

有一个简单的解决方案吗?

Is there a a simple solution for this?

names_array = list()
firstWeight_Array=list()
students = 2
for i in range(students):
    name = str(raw_input("Please enter a  name:"))
    names_array.append(str(name))
    fWeight = int(raw_input("Please enter the first weight:"))
    firstWeight_Array.append(int(fWeight))

SecondWeight_Array=list()
for i in range(students):
        sWeight = int(raw_input("Please enter the Second weight:"))
        SecondWeight_Array.append(int(sWeight))

print(firstWeight_Array,SecondWeight_Array)
print firstWeight_Array - SecondWeight_Array

推荐答案

未为list定义减法运算符,因为它在一般情况下没有意义.但是,您可以简单地使用[]运算符获取单个项目,然后在新的list中计算差异:

The substraction operator is not defined for list, as it makes no sense in a general way. However, you can simply get the single items using the [] operator, and calculate the difference in a new list:

newArray = list();
for i in xrange(students):
    newArray.append(firstWeight_Array[i] - secondWeight_Array[i]);
print newArray;

这篇关于我只想从两个列表中减去元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆