本文實例講述了Python學習筆記之列表和成員運算符及列表相關方法。分享給大家供大家參考,具體如下:
列表和成員運算符
列表可以包含我們到目前為止所學的任何數據類型并且可以混合到一起。
lst_of_random_things = [1, 3.4, 'a string', True] # 這是一個包含 4 個不同類型元素的列表 print(lst_of_random_things[0]) # 1
獲取上述列表的第一個值和最后一個值
print(lst_of_random_things[0]) # 1 print(lst_of_random_things[len(lst_of_random_things) - 1]) # True
此外,你可以使用負數從列表的末尾開始編制索引,其中 -1 表示最后一個元素,-2 表示倒數第二個元素,等等
print(lst_of_random_things[-1]) # True print(lst_of_random_things[-2]) # a string
列表切片的應用: 我們可以使用切片功能從列表中提取多個值。在使用切片功能時,務必注意,下限索引包含在內,上限索引排除在外, 最終返回的是一個新的列表
正常的用法:
lst_of_random_things = [1, 3.4, 'a string', True] print(lst_of_random_things[1:2]) # [3.4]
從列表的開頭開始,也可以省略起始值
lst_of_random_things = [1, 3.4, 'a string', True] print(lst_of_random_things[:2]) # [1, 3.4]
要返回到列表結尾的所有值,可以忽略最后一個元素
lst_of_random_things = [1, 3.4, 'a string', True] print(lst_of_random_things[1:]) # [3.4, 'a string', True]
在列表里還是不在列表里?關鍵字 in 和 not in 返回一個布爾值, 表示某個元素是否存在于列表中,或者某個字符串是否為另一個字符串的子字符串
print('this' in 'this is a string') # True print('in' in 'this is a string') # True print('isa' in 'this is a string') # False print(5 not in [1, 2, 3, 4, 6]) # True print(5 in [1, 2, 3, 4, 6]) # False
列表的可變性和順序
可變性是指對象創建完畢后,我們是否可以更改該對象。如果對象(例如列表或字符串)可以更改,則是可變的。但是,如果無法更改對象以創建全新的對象(例如字符串),則該對象是不可變的。
列表可變性測試:
my_lst = [1, 2, 3, 4, 5] my_lst[0] = 'one' print(my_lst) # ['one', 2, 3, 4, 5]
下面嘗試修改字符串:
greeting = "Hello there" greeting[0] = 'M'
嘗試修改字符串,引發如下錯誤:
Traceback (most recent call last):
? File "", line 1, in
TypeError: 'str' object does not support item assignment
但可以修改字符串對象的地址:
greeting = "Hello there" greeting = 'Hi there' print(greeting) # Hi there
本節注意事項:對于你要使用的每種數據類型,你都需要注意兩個事項:可變嗎?有序嗎?
列表和成員運算符[相關練習]
請使用列表索引根據整型變量 month 判斷特定月份有多少天,并將該值存儲在整型變量 num_days 中
month = 8 days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31] # use list indexing to determine the number of days in month num_days = days_in_month[month - 1] print(num_days)
請使用列表切片記法從此列表中選擇列表中的最后三個元素。提示:切片可以使用負索引!
eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003', 'March 29, 2006', 'August 1, 2008', 'July 22, 2009', 'July 11, 2010', 'November 13, 2012', 'March 20, 2015', 'March 9, 2016'] # TODO: Modify this line so it prints the last three elements of the list print(eclipse_dates[-3:])
列表的常用方法
常用方法舉例
-
len()
返回列表中的元素數量。 -
max()
返回列表中的最大元素。最大元素的判斷依據是列表中的對象類型。數字列表中的最大元素是最大的數字。字符串列表中的最大元素是按照字母順序排序時排在最后一位的元素。因為 max() 函數的定義依據是大于比較運算符。如果列表包含不同的無法比較類型的元素,則 max() 的結果是 undefined。 -
min()
返回列表中的最小元素。它是 max() 函數的對立面,返回列表中的最小元素。 -
sorted()
返回一個從最小到最大排序的列表副本,并使原始列表保持不變。 -
join()
將字符串列表作為參數,并返回一個由列表元素組成并由分隔符字符串分隔的字符串。 -
append()
實用方法 append() 會將元素添加到列表末尾。
方法實踐舉例之join()
new_str = "\n".join(["fore", "aft", "starboard", "port"]) print(new_str)
輸出: 在此示例中,我們使用字符串 “\n” 作為分隔符,以便每個元素之間都有一個換行符。我們還可以在 .join 中使用其他字符串作為分隔符。以下代碼使用的是連字符。
fore aft starboard port
or
name = "-".join(["García", "O'Kelly"]) print(name)
輸出:
García-O'Kelly
方法實踐舉例之append()
letters = ['a', 'b', 'c', 'd'] letters.append('z') print(letters)
輸出:
['a', 'b', 'c', 'd', 'z']
列表的常用方法[相關練習]
以下代碼的輸出是什么?
a = [1, 5, 8] b = [2, 6, 9, 10] c = [100, 200] print(max([len(a), len(b), len(c)])) # 4 print(min([len(a), len(b), len(c)])) # 2
以下代碼的輸出是什么?
names = ["Carol", "Albert", "Ben", "Donna"] print(" & ".join(sorted(names))) # Albert & Ben & Carol & Donna
以下代碼的輸出是什么?
names = ["Carol", "Albert", "Ben", "Donna"] names.append("Eugenia") print(sorted(names)) # ['Albert', 'Ben', 'Carol', 'Donna', 'Eugenia']
更多關于Python相關內容可查看本站專題:《Python列表(list)操作技巧總結》、《Python字符串操作技巧匯總》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
