1.用途:多個裝備、多個愛好、多門課程,甚至是多個女朋友
2.定義:內可以有多個任意型別的值,逗號分隔元素
# my_girl_friend = list(['jason','tank','sean'])
my_girl_friend = ['jason', 'tank', 'sean']
print(f"my_girl_friend: ")
my_girl_friend: ['jason', 'tank', 'sean']
l = list('hello nick')
print(f"l: ")
l: ['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']
3.常用操作+內建方法:常用操作和內建方法分為優先掌握(今天必須得記住)、需要掌握(一周內記住)、其他操作(了解)三個部分。
1.按索引取值(正向取值+反向取值),即可存也可以取
# list之索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list[0] = 'nick handsom'
# name_list[1000] = 'tank sb' # 報錯
print(f"name_list[0]: ")
name_list[0]: nick handsom
2.切片
# list之切片
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list[0:3:2]: ")
name_list[0:3:2]: ['nick', 'tank']
3.長度
# list之長度
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"len(name_list): ")
len(name_list): 4
4.成員運算in和not in
# list之成員運算in和not in
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"'tank sb' in name_list: ")
print(f"'nick handsome' not in name_list: ")
'tank sb' in name_list: false
'nick handsome' not in name_list: true
5.追加值
# list之追加值
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list: ")
name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']
6.刪除
# list之刪除
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]
print(f"name_list: ")
name_list: ['nick', 'jason', 'sean']
7.迴圈
# list之迴圈
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
print(name)
nick
jason
tank
sean
1.insert()
# list之insert()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.insert(1, 'handsome')
print(f"name_list: ")
name_list: ['nick', 'handsome', 'jason', 'tank', 'sean']
2.pop()
# list之pop(),pop()預設刪除最後乙個元素
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.pop(1): ")
print(f"name_list: ")
name_list.pop(1): jason
name_list: ['nick', 'tank', 'sean']
3.remove()
# list之remove()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.remove('nick'): ")
print(f"name_list: ")
name_list.remove('nick'): none
name_list: ['jason', 'tank', 'sean']
4.count()
# list之count()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.count('nick'): ")
name_list.count('nick'): 1
5.index()
# list之index()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.index('nick'): ")
name_list.index('nick'): 0
6.clear()
# list之clear()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.clear()
print(f"name_list: ")
name_list:
7.copy()
# list之copy()
name_list = ['nick', 'jason', 'tank', 'sean']
print(f"name_list.copy(): ")
name_list.copy(): ['nick', 'jason', 'tank', 'sean']
8.extend()
# list之extend()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list2 = ['nick handsome']
name_list.extend(name_list2)
print(f"name_list: ")
name_list: ['nick', 'jason', 'tank', 'sean', 'nick handsome']
9.reverse()
# list之reverse()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.reverse()
print(f"name_list: ")
name_list: ['sean', 'tank', 'jason', 'nick']
10.sort()
# list之sort(),使用sort列表的元素必須是同型別的
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.sort()
print(f"name_list: ")
name_list.sort(reverse=true)
print(f"name_list_reverse: ")
name_list: ['jason', 'nick', 'sean', 'tank']
name_list_reverse: ['tank', 'sean', 'nick', 'jason']
4.存乙個值or多個值:多個值
5.有序or無序:有序
hobby_list = ['read', 'run', 'girl']
print(f'first:')
hobby_list[2] = ''
print(f'second:')
first:4522187016
second:4522187016
6.可變or不可變:可變資料型別 列表型別內建方法 list
目錄my friend wang chen peng jiayi zhuang print my friend l list hello wang 字串強制型別轉換成列表 print l 結果為l h e l l o n i c k 1.1 優先掌握 name list nick jason tan...
列表內建方法
cmp 方法比較兩個列表的元素。以下是cmp 方法的語法 cmp list1,list2 引數 返回值如果元素是相同型別的,執行比較,並返回結果。如果元素是不同的型別,檢查,看看他們是否是數字 如果到達了列表中的乙個的結束,較長的列表是 大 如果耗盡列表和共享相同的資料,其結果是並列的,這意味著返回...
列表內建方法
目錄氣泡排序法 lt 1,2,3,4 print lt 1 lt 1 3 print lt 2 1,3,3,4 print lt 冒號左邊沒有左邊取到頭,右邊沒有則右邊取到頭 print lt 1 2 顧頭不顧尾,只列印索引號為1的元素 print lt 1 4 2 步長為2 1,3,3,4 3 3...