【问题标题】:How to print an element of a linked list in Python?如何在 Python 中打印链表的元素?
【发布时间】:2017-03-16 07:23:42
【问题描述】:

比如说我有

node = {}
node['data'] = ['hi', '8']

newNode = {}
newNode['data'] = ['hello', '6']

我想比较node和newNode中的数字6和8

如果我尝试这样做

print(node[1])

因为数字位于列表的第 1 位,所以我收到一条错误消息:

密钥错误:1

【问题讨论】:

  • print(node['data'][1])

标签: python list python-3.x linked-list nodes


【解决方案1】:

您可以将它们比较为:

node["data"][1] == newNode["data"][1]

【讨论】:

    【解决方案2】:

    通过打印node[1],您实际上是在节点字典中搜索名为1 的键。相反,既然您将其命名为“数据”,请使用node['data'][1]node['data'] 指的是['hi', '8']。如果 8 和 6 相同,则以下打印 True of False。

    node = {}
    node['data'] = ['hi', '8']
    # you can also create the dictionary by doing this:
    # node = {'data' :  ['hi', '8']}
    # or
    # node = dict{'data' =  ['hi', '8']}
    
    newNode = {}
    newNode['data'] = ['hello', '6']
    
    # so to compare:
    
    print(node['data'][1]==newNode['data'][1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多