【问题标题】:How to get the first element of an array Python, from HTML如何从 HTML 中获取数组 Python 的第一个元素
【发布时间】:2011-07-10 08:55:21
【问题描述】:

我有一个数组叫list,例如

list = [0,1,2,3]

当我这样做时在 HTML 中

<body>
{{ list }}
</body>

输出将是[0,1,2]

我想要的是

{{ list[0] }}

但是这个语句给了我错误,我怎么能打印第一个元素

【问题讨论】:

  • {{ list[0] }} 既不是 HTML 也不是 Python。你还在用什么?
  • @msytNadeem [0,1,2] 不是数组,而是列表; array('l', [1, 2, 3, 4, 5]) 是一个数组。 在罗马,像罗马人那样做。
  • @eyquem 我认为列表只不过是一个复杂的数组!但为什么这不起作用?这是模板的问题吗?
  • @tamizhgeek 我所说的不是日常使用中模糊的数组,而是 Python 的类型,尤其是这个类型:(docs.python.org/library/array.html#module-array) Python 的数组不能是表示列表很复杂,因为“数组是序列类型,其行为与列表非常相似,只是其中存储的对象类型受到限制。”

标签: python html django arrays printing


【解决方案1】:

如果你使用 Django,试试这个:{{ list.0 }} 或切片:{{ list|slice:":1" }}

文档:http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slice

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 2011-05-04
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2022-06-14
    • 2010-12-27
    相关资源
    最近更新 更多