【问题标题】:Unhashable type: 'list' error when running python script in mininet不可散列的类型:在 mininet 中运行 python 脚本时出现“列表”错误
【发布时间】:2017-12-26 17:20:39
【问题描述】:
#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import Link
from mininet.log import setLogLevel, info
from mininet.cli import CLI

class myNetwork(Topo):
 "three users connected to cloud"

 def build(self, **_opts):
  #create switch
  s1 = \
  [ self.addSwitch for s in 's1' ]
  #create hosts
  h1, h2, h3 = \
  [ self.addHosts for h in 'h1', 'h2', 'h3' ]
  #create links
  [for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

def run():  
 topo = myNetwork()
 net = Mininet(topo=topo)
 net.start()
 CLI(net)
 net.stop()

if __name__=='__main__':
setLogLevel('info')
run()

当我尝试在 mininet 中运行我的 python 脚本时,我得到了错误:

不可散列的类型:'list'

我尝试对此进行研究,并且我理解错误的含义,但我不确定为什么在运行我的 python 脚本时会出现此错误。

【问题讨论】:

  • self.addSwitch for s in 's1' 什么???如果你不掌握列表推导,那么就不要使用它们。

标签: python network-programming mininet


【解决方案1】:

您似乎对 List Comprehensions 的工作原理有些困惑。您可能正在尝试这样做

[ self.addSwitch for s in s1 ] 
# notice the lack of quotes around s1, this means we want the VAR not the string

而不是

[ self.addSwitch for s in 's1' ]

另外,您可能忘记从该行中删除 [,因为我认为这不是正确的语法:

[for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

到:

for h,s in [(h1,s1), (h2,s1), (h3,s1)]: self.addLink(h,s)

这里有几个关于列表理解的例子:

l = [1, 2, 3, 4]

l_modified = [i+1 for i in l] # [2, 3, 4, 5]

a = "String"
a_list = [c for c in a] # ["S", "t", "r", "i", "n", "g"]

b = [c.upper() for c in "hello"] # ["H", "E", "L", "L", "O"]

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2022-01-11
    • 2017-03-07
    • 2023-04-09
    相关资源
    最近更新 更多