【问题标题】:python indexing complex listpython索引复杂列表
【发布时间】:2017-04-19 20:23:47
【问题描述】:

我在去年夏天使用 K&R 第 2 版学习了 C。从 1989 年开始写 C 的书。然后我决定学习 CS50,现在正在第一次学习 python3。

我还决定在http://www.python-course.eu/python3_sequential_data_types.php 在线学习有关python3 的教程。我在理解深度嵌套列表的 python 索引时遇到了一些麻烦。

我在发帖前搜索了一段时间,但没有看到答案。我在网上找到的例子是这样的:

>>> t = [[100, 'str_1'], [200, 'str_2'], [300, 'str_3']]

我明白。索引与 C 2d char 数组相同。

让我困惑的是:

place= ["High up", ["further down", ["and down", ["deep down", "the answer", 42]]]]

>>> place[0]
'High up'
>>> place[1]
['further down', ['and down', ['deep down', 'the answer', 42]]]
>>> place[1][1]
['and down', ['deep down', 'the answer', 42]]
>>> place[1][1][1]
['deep down', 'the answer', 42]
>>> place[1][1][1][0]
'deep down'
>>> place[1][1][1][0][3]
'p'

我以为我明白了,只要继续看一个,就可以进入下一个列表,但后来我找到了。

>>> complex_list = [["a",["b",["c","x"]]],42]
complex_list[0]   #note: index 0 is the entire left, while above it's not.*
['a', ['b', ['c', 'x']]]
>>> complex_list[0][1]
['b', ['c', 'x']]
>>> complex_list[0][1][1][0]
'c'

这两个列表在我看来几乎相同,除了 complex_list 在左侧有两个大括号。 有人可以向我解释一下规则吗,我不明白为什么 place[0] 只是列表中的第一项,而 complex_list[0] 是除了数字 42 之外的整个列表?额外的大括号如何改变索引?

【问题讨论】:

  • 我认为complex_list左边的两个大括号是为了容纳第一个“内部列表”["a",["b",["c","x"]]]和单独的项目42,也许有时你放空格更容易看到在这样的列表项之间:[ ["a",["b",["c","x"]]], 42 ],这就像在说 [ [<inner list>], 42],只要大括号匹配(每个左大括号 [ 对应的右大括号 ])。
  • 它不像C 数组那样是几何的。它实际上是指向对象的指针列表。所以每个[] 通过另一个指针取消引用。所以下一个[] 需要在前一个取消引用的上下文中进行评估。

标签: list python-3.x indexing


【解决方案1】:

您可以将其视为列表中的每个列表都是一个单独的项目。在您访问之前,列表中的任何内容都是无关紧要的。

你的例子:

place = ["high up", [...]]

place[0]"high up",因为place 中的第一项就是那个字符串。

complex_list = [[...], 42]

complex_list[0] 是除 42 之外的整个列表,因为该列表是第一项。

【讨论】:

    【解决方案2】:

    希望这可以帮助您更好地理解嵌套:

    a = ['this element is in outer list', #index No. [0]
         ['this is inner list', #index No. [1] to get whole list and index No. [1][0] to get 1st element
              ['this is inner list within inner list', #index No. [1][1] will get you list and No. [1][1][0] will get 1st element
                   ['this is inner list within inner list that is within inner list']]], #index No. [1][1][1]
         'this element is in outer list', #index No. [2]
         ['this is inner list'], #index No. [3] if you want that list and index No. [3][0] to get string
         'this element is in outer list'] #index No. [4]
    
    print a[0] #this element is in outer list
    print a[1] #['this is inner list', ['this is inner list within inner list', ['this is inner list within inner list that is within inner list']]]
    print a[1][0] #this is inner list
    print a[1][1] #['this is inner list within inner list', ['this is inner list within inner list that is within inner list']]
    print a[1][1][0] #this is inner list within inner list
    print a[1][1][1] #['this is inner list within inner list that is within inner list']
    print a[2] #this element is in outer list
    print a[3] #['this is inner list']
    print a[3][0] #this is inner list
    print a[4] #this element is in outer list
    

    此外,您可以像访问strings 一样访问lists,因此如果您有:

    b = 'Example'
    print b[0] #this will print E
    print b[1] #this will print x
    

    【讨论】:

      【解决方案3】:

      我希望这对你有帮助。 complex_list 开头的两个大括号意味着无论 complex_list 的长度是多少,它的第一项 complex_list[0] 都是另一个列表,而您的第一项 place[0] 只是一个字符串“High up”。

      现在,您应该知道字符串是一组字符,也可以使用索引访问,因此您还可以访问 item place[0] 中的不同元素,如下所示:

      print(place[0][0]) # first element in string of characters 'High up'
      # H 
      print(place[0][1]) # second element in string of characters 'High up'
      # i
      print(place[0][5]) # sixth element in string of characters 'High up'
      # u
      

      此处打印的所有元素都是列表位置第一项中找到的字符串中的字符。

      在 complex_list 的情况下,第一项 complex_list[0] 是一个列表,因此可以通过索引进一步访问,而第二项 complex_list[1] 是一个整数,无法进一步索引

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 2012-08-30
        • 2014-07-29
        • 1970-01-01
        • 2013-12-18
        • 2013-02-14
        • 2019-07-05
        • 2018-11-13
        相关资源
        最近更新 更多