gpt4 book ai didi

python - 如何在不同数组的 For In 中访问数组的特定单元格?

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:22 27 4
gpt4 key购买 nike

我只想添加数字的数字。我设法将一个数字分成数字,但我不知道如何才能做到这一点?我想同时访问两个数组。

它只是给了我 IndexError:列表索引超出范围

t=[]
t=[int(i) for i in str(a)]
d=[]
d=[int(i) for i in str(b)]
c=0
for i in t:
c=c+1
t[c]=(t[c]+d[c])%10

如果 t=[1, 2] 且 d=[3, 4] 那么我想要 t=[4, 6]

最佳答案

您需要 zip() 来帮助您同时迭代两个列表:

t = [x + y for x, y in zip(t, d)]

更正的、不推荐的代码是:

# ...
for i in t:
t[c] = (t[c] + d[c]) % 10
c += 1

关于python - 如何在不同数组的 For In 中访问数组的特定单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55925260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com