python - 无法解压不可迭代的对象/遍历python中的对象数组
问题描述
我想遍历我拥有的 URL 对象数组中的所有对象
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for i, u in urls:
print(i)
print(u)
但是,当我运行此脚本时,它会返回以下错误:
TypeError: cannot unpack non-iterable Url object
我该如何解决?
解决方案
试试这个:
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for i in urls:
print(i)
通过 url 进行迭代。要获得结果和网址(我认为您正在尝试这样做),请执行以下操作:
class Url(object):
pass
a = Url()
a.url = 'http://www.heroku.com'
a.result = 0
b = Url()
b.url = 'http://www.google.com'
b.result = 0
c = Url()
c.url = 'http://www.wordpress.com'
c.result = 0
urls = [a, b, c]
for c,i in enumerate(urls):
print("index is ",c)
print(i.result)
print(i.url)
推荐阅读
- css - 是否有可能在 SASS 中使用 CSS 变量?
- php - 如何使新创建的类别自动设置为 wordpress 中的主要导航菜单?
- java - 使用 Spring Boot 在 HTML 页面中显示表格
- html - 底部的 Flexbox 图像间隙
- python - 虚拟机器人 random.choices 和 os.startfile()
- python - 我的pyserial有什么问题?我已经使用 pip 安装了 py serial
- python - mysql 连接器。'chunk = self.sock.recv(4 - packet_len)' 的无限等待
- javascript - 是否有针对 XSD 1.1 版本验证 XML 的 JavaScript 库?
- c - 从plugin_path返回局部变量我如何改变它
- typescript - @types/react-table:如何为我的包的用户包含配置的类型?