【问题标题】:Python Array Question(Programming)Python 数组题(编程)
【发布时间】:2011-08-04 14:24:45
【问题描述】:

[] 是什么意思? 另外如何在python中将变量识别为空数组? 谢谢!

perl: xcoords = ()

如何翻译?

【问题讨论】:

  • 这不是 Perl。 perl 版本是:@xcoords = ();
  • 对不起,我忘了添加数组符号,我该如何翻译呢
  • 您是指[] 还是()?在 perl 中,他们在那里做非常不同的事情。请复制并粘贴您想了解的实际代码,否则您的问题将变得毫无意义。
  • perl 中的@Xcoords = () 如何翻译成python

标签: python arrays perl syntax


【解决方案1】:

[] - 在 Python 中是一个空列表,与调用 list() 相同,例如[] == 列表() 要检查该列表是否为空,您可以使用 len(l) 或:

listV = [] # an empty list
if listV:
    # do something if list is not empty
else:
    # do something if list is really empty

要阅读有关列表的更多信息,您可以使用the following link

【讨论】:

  • 所有空容器评估为False
  • @tbilisidavid 或者您可以使用len(emptyList) 来确定列表是否为空,以防您只想检查。
【解决方案2】:

列表类似于 C++ 数组,但有一些区别。不同之处之一是它们甚至可以携带不同类型的列表。检查列表是否为空

lists = []
len(lists)
lists[0]= "More of me"
len(lists)

更多查看Python official教程及以上文档

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2014-08-26
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多