【问题标题】:What is the difference between {} and [] in python?python中的{}和[]有什么区别?
【发布时间】:2011-03-08 09:58:37
【问题描述】:

python中columnNames = {}columnNames = []有什么区别?

如何迭代每一个?使用{% for value in columnNames %}for idx_o, val_o in enumerate(columnNames):

【问题讨论】:

  • 您有机会浏览本教程吗?
  • 你应该添加django标签,因为第一个循环语法是Django模板语音。
  • 对不起,我是 Python 新手,只听说过数组和列表,没听说过字典。
  • 我不认为这个问题是 django 特定的,我认为 OP 遇到了一些显示模板语法的 django 文档,这导致了误解。
  • 对不起那个 {% Django tags

标签: python syntax


【解决方案1】:
  • columnNames = {} 定义一个空的 dict
  • columnNames = [] 定义一个空的 list

这些是根本不同的类型。 dictassociative arraylist 是带有整数索引的 standard array

我建议您查阅参考资料,以更加熟悉这两种非常重要的 Python 容器类型。

【讨论】:

    【解决方案2】:

    除了大卫的回答之外,您通常如何迭代它们:

    # iterating over the items of a list
    for item in someList:
        print( item )
    
    # iterating over the keys of a dict
    for key in someDict:
        print( key, someDict[key] )
    
    # iterating over the key/value pairs of a dict
    for ( key, value ) in someDict.items():
        print( key, value )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-04
      • 2022-01-21
      • 2018-06-06
      • 2011-05-04
      • 2016-01-10
      • 2016-07-03
      • 2019-07-11
      • 2021-01-02
      相关资源
      最近更新 更多