我还是新编码,所以我为基本问题道歉。如何添加两个单独列表的元素?
listOne = [0, 1 , 7, 8]
listTwo = [3, 4, 5, 6]
listThree = []
for i in listOne:
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
print(listThree)
我希望输出为[3, 5, 12, 14]
,但我不断收到语法错误
TypeError: List indices must be integers, not strings.
我认为这可能与我可能是一个字符串有关,所以我把int(i)
作为for循环之后的第一行,但这没有帮助。
答案 0 :(得分:4)
您可以使用zip()
功能。将+
更改为您想要的任何操作。
listOne = [0, 1 , 7, 8]
listTwo = [3, 4, 5, 6]
l = [x + y for x, y in zip(listOne, listTwo)]
print(l)
[3, 5, 12, 14]
答案 1 :(得分:2)
您可以将map
与add
模块中的operator
功能结合使用:
>>> from operator import add
>>> listOne = [0, 1 , 7, 8]
>>> listTwo = [3, 4, 5, 6]
>>> map(add, listOne, listTwo)
[3, 5, 12, 14]
答案 2 :(得分:1)
print np.array(list1) + np.array(list2)
这就是全部。
答案 3 :(得分:1)
我不知道你为什么会收到你发布的错误。其余的代码在哪里? listThree在哪里初始化?
您所犯的错误是您正在迭代列表元素,但之后您就像在迭代索引一样。例如,没有listOne [8]。你似乎想要做的是:
listThree = []
for i in range(len(listOne)):
listThree.append(listOne[i] + listTwo[i])
print(listThree)
虽然更好的方法是避免for-append all all并使用列表理解。
listThree = [ listOne[i]+listTwo[i] for i in range(len(listOne)) ]
print(listThree)
答案 4 :(得分:1)
你犯了两件事
看起来很难,但很简单
listOne = [0, 1, 7, 8]
listTwo = [3, 4, 5, 6]
for i in listOne:
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
print(listThree)
看看你的“i”变量,假设这个值: 0,1,7,8
那是因为你使用“i in listOne”,所以,“i”它假设了listOne的值。
正确的方法是:
listOne = [0, 1, 7, 8]
listTwo = [3, 4, 5, 6]
for i in range(len(listOne)):
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
print(listThree)
现在“i”将假设这个值:“0,1,2,3”,因为现在你在你的for中使用了listOne的长度。
但还有另外一件事。这有点困难,但你需要知道。
当你在一个代码块中声明一个变量时(在if,while,for ...中),当程序离开那个块时就会死掉。例如:
x = 30
if x is 30:
stringInsideIf = "Hello World"
print(stringInsideIf)
在这个例子中,你会收到和Error,因为“stringInsideIf”它没有在if的块之外声明。
所以,你需要在for的块之外声明“listThree”,作为一个新列表,就像这样
listThree = []
我认为它会解决问题
不要害怕提出基本问题,没有人作为程序员出生
答案 5 :(得分:0)
你需要在for循环之外创建listThree。
listOne = [0,1,7,8]
listTwo = [3,4,5,6]
listThree = []
for list in listOne:
protocol SuperProtocol {
}
protocol SubProtocol: SuperProtocol {
}
class MyObject: SubProtocol {
}
let value1: SubProtocol = MyObject()
let value2: SuperProtocol = value1 // No error here. Upcasting works.
let array1: [SubProtocol] = [MyObject()]
let array2: [SuperProtocol] = array1 // Error here "Cannot convert value of type '[SubProtocol]' to specified type '[SuperProtocol]'"
打印(listThree)
多数民众赞成。编码很容易学习向计算机解释你自己如何向自己解释。快乐编码: - )
答案 6 :(得分:0)
与其他人一致认为有更好的方法(列表理解,内置库等),但这里是如何最低限度地修复代码:
for i in range(len(listOne)):
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
关键是你试图遍历列表索引,而不是它的值(这是你得到的"对于我在listOne:")。 / p>
len(listOne)给出列表的长度(这里:4)
范围(4) - > (0,1,2,3)
答案 7 :(得分:0)
对于listOne中的i->这里,我代表列表的元素而不是索引
listOne = [0, 1 , 7, 8]
listTwo = [3, 4, 5, 6]
listThree = []
for i in range(len(listOne)):
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
print(listThree)
上面的代码可以正常工作。
答案 8 :(得分:-1)
我不确定你为什么会遇到那个特别的错误。实际上你应该让索引超出范围异常。这就是我将代码粘贴到我的解释器中时得到的结果。原因就在于此。 当你说
for i in listOne:
listAdd = (listOne[i] + listTwo[i])
listThree.append(listAdd)
我实际上等于每次迭代时列表中项的值,所以0然后是1,然后是7,然后是8.所以当你尝试使用i作为索引访问listOne和listTwo的元素时,它当我的数字大于列表中最后一项的位置时,将失败。
有很多方法可以达到你想要的效果。你可以这样做
for i in range(4):
listAdd.append(listOne[i] + listTwo[i])
您也可以这样做
for x,y in zip(listOne, listTwo)
listAdd.append(x+y)
这些是一些最环状的方法。 leb和Donald展示了上面的列表理解风格,Dan展示了地图。