【问题标题】:How to find out the last item in a list of string?如何找出字符串列表中的最后一项?
【发布时间】:2016-07-04 09:49:11
【问题描述】:

在 Genie 中使用 字符串列表 非常简单。我想知道是否可以在 python 中使用类似于 [-1] 的内容找到最后添加的项目。

以 Genie 的教程为例:

[indent=4]

init

    /* test lists */
    var l = new list of string

    l.add ("Genie")
    l.add ("Rocks")
    l.add ("The")
    l.add ("World")

    for s in l
        print s

    print " "

    print l[-1]

目标

我的期望是 l[-1] 位将指向“世界”项目。但是它在执行时给了我错误:

ERROR:arraylist.c:954:gee_array_list_real_get: assertion failed: (index >= 0)
/tmp/geany_run_script_X6D4JY.sh: line 7: 13815 Abortado                (imagem do núcleo gravada)/tmp/./test

问题

gee 数组显然只适用于正索引,有没有其他方法可以获取数组中最后添加的项?

【问题讨论】:

    标签: genie


    【解决方案1】:

    Gee list 有一个last 方法:

    print( l.last() )
    

    使用 size 属性可以找到 Gee 列表的长度:

    print( l[ l.size -1 ] )
    

    Gee 列表也可以只使用最后一个元素进行切片:

    for s in l[l.size-1:l.size]
        print( s )
    

    【讨论】:

      【解决方案2】:

      在python中

      last_index = len(a) - 1
      
      print a[last_index]
      

      在瓦拉或精灵中,

      last_index = a.length - 1
      
      print a[last_index]
      

      【讨论】:

      • Gee.Array 列表似乎没有 len 或 length 方法...我在 valadoc 也找不到 len 函数...
      • @Luís, An array is created by using "array of type name" followed by the fixed size of the array e.g. var a = new array of int[10] to create an array of 10 integers. The length of such an array can be obtained by the length member variable e.g. var count = a.length. Source : wiki.gnome.org/Projects/Genie` 如果有错误请告诉我..
      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2020-09-08
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多