2017-04-19 179 views
0

我在去年夏天學過C,使用K & R 2nd ed。我從1989年開始寫C語言。於是我決定採用CS50,現在我第一次學習python3。python索引複雜列表

我還決定在python3上在線上http://www.python-course.eu/python3_sequential_data_types.php。我在理解用於深度嵌套列表的pythons索引時遇到了一些麻煩。

我已經花了一段時間搜索發佈之前,但沒有看到答案。我在網上找到的例子是這樣的:

>>> 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對左邊的兩個括號。 有人可以向我解釋規則,我不明白爲什麼地方[0]只是列表中的第一項,而complex_list [0]是整個列表,除了數字42?該額外大括號如何更改索引?

+0

我認爲'complex_list'左邊的兩個大括號將容納第一個「內部列表」[「a」,[「b」,[「c」,「x」]]]'和單獨的項目'42',也許有時候在列表項之間放置空格時會更容易看到:'[[「a」,[「b」,[「c」,「x」]]],42]',這就像是說[[[只要大括號匹配(每個左括號'[''對應的右括號']']。 – davedwards

+0

它不像'C'陣列那樣幾何。它實際上是指向對象的指針列表。所以每個'[]'通過另一個指針去引用。所以下一個'[]'需要在前面的解除引用的上下文中進行評估。 –

回答

1

您可以將其視爲列表中的每個列表是單獨的項目。無論在列表中的任何內容是無關緊要的,直到你訪問它。

對於示例:

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

place[0]"high up"因爲place的第一個項目是字符串。

complex_list = [[...], 42] 

complex_list[0]是除42之外的整個列表,因爲該列表是第一項。

0

希望這有助於你理解嵌套一個好一點:

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 

此外,您可以訪問string S作爲他們是否list小號,因此,如果您有:

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

我希望這有助於您。 complex_list開頭的兩個大括號意味着complex_list中第一個項目complex_list [0]的長度是另一個列表,其中第一個項目位置[0]只是一個字符串'High Up'。

現在,你應該知道一個字符串是一組也可以使用索引訪問的人物,所以你也可以訪問項目的地方不同的元素[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]是不能被進一步編入索引的整數