简体   繁体   English

Python TypeError:“ bool”对象不可迭代

[英]Python TypeError: 'bool' object is not iterable

I'm running this code in an attempt to assign key value pairs using a dict. 我正在运行此代码,以尝试使用dict分配键值对。 to store the data (key = xvalue, yvalue) which is in two separate arrays. 存储在两个单独的数组中的数据(键= xvalue,yvalue)。 ie

1  def main():
2  path = 'some/path/'
3
4
5   d = {}
6   xcord = [1.2,2.4,2.9,3.0,4.1]
7   ycord = [1.0,2.0,3.0,4.0,5.0]
8   a=0
9   b=0
10  while b < 136 and a <= 21 :
11      for x in xcord and y in ycord :
12  -->    d{b}.append(xcord[x],ycord[y])
13         b=b+1
14         if a == 21:
15          a=0
16         else:
17          a=a+1
18  print(d)
19
20  if __name__ == "__main__":
21     main()

But when I run this I get a TypeError: 但是当我运行它时,我得到一个TypeError:

File "some/path/", line 21, in <module>
    main()
  File "some/path", line 12, in main
    for x in xcord and y in ycord :
TypeError: 'bool' object is not iterable

I'm looking to append data from the xcord and ycord arrays to the dictionary and I'm clearly not doing this correctly. 我想将数据从xcord和ycord数组追加到字典中,但是显然我没有正确地做到这一点。
I was thinking I could reference the dict for future calculations like this for example: 我当时想我可以参考字典,以便将来进行如下计算:

print(d{0})
# with a result
{1.2 , 1.0}
#   or  say I want to calculate slop between two points
sqrt((d{0, [1],[]} - d{2, [1],[]})sqrd + (d{0, [],[1]} - d{2, [], [1]})sqrd)
# with a result 
3.2

Please critique me on the Pythonic ways, I am new to Python. 请以Python的方式批评我,我是Python的新手。 and any help is appreciated. 和任何帮助表示赞赏。 I know the math portion is not correct as I was just show some syntax to help explain 我知道数学部分不正确,因为我只是显示一些语法来帮助解释

Replace: 更换:

for x in xcord and y in ycord :

With: 带有:

for x,y in zip(xcord,ycord):

And lot more mistakes, so your code should be like this: 还有更多错误,因此您的代码应如下所示:

def main():
    path = 'some/path/'
    d = {}
    xcord = [1.2,2.4,2.9,3.0,4.1]
    ycord = [1.0,2.0,3.0,4.0,5.0]
    a=0
    b=0
    while b < 136 and a <= 21 :
        for x,y in zip(xcord,ycord):
           if b in d:
              d[b].append(x,y)
           else:
              d[b]=[x,y]
           b=b+1
           if a == 21:
              a=0
           else:
              a=a+1
    print(d)

if __name__ == "__main__":
   main()

Thanks to everyone for the support in my early python coding days. 感谢大家在我早期的python编码时代的支持。
The solution I found to work with what exactly I was expecting for the output was: 我发现可以与我期望的输出完全兼容的解决方案是:

d = {}
b = 0
xcord = [1.2,2.4,2.9,3.0,4.1]
ycord = [1.0,2.0,3.0,4.0,5.0]

    for x,y in zip(xcord,ycord):
        if b in d:
            d[b].append(x,y)
        else:
            d[b] = [x,y]
        b=b+1

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

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