在List內使用方法,其實跟String內使用方法,很像

一樣的首先,建立一個list,然後我們來看看33在Python 是第幾個index,這時要使用"方法"index()

# Create list areas
areas = [11, 18, 19, 33, 21, 21, 23]

# Print out the index of the element 33
print(areas.index(33))

答案是3,因為對於Python來紓,11的index是0

3


那我們再來數數看21在裡面出現幾次,使用"方法"count()

# Print out how often 21 appears in areas
print(areas.count(21))

答案是2囉

2

 

回到目錄

arrow
arrow
    文章標籤
    Python
    全站熱搜

    Cheng yichung 發表在 痞客邦 留言(0) 人氣()