5 b列表型別內建方法

2022-08-11 04:54:11 字數 4077 閱讀 2989

按索引取值(正向取值+反向取值),即可存也可以取

切片長度len

成員運算in和not in

刪除del

迴圈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

insert

popremove

count

index

clear

copy

extend

reverse

sort

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']

列表型別內建方法

1.用途 多個裝備 多個愛好 多門課程,甚至是多個女朋友 2.定義 內可以有多個任意型別的值,逗號分隔元素 my girl friend list jason tank sean my girl friend jason tank sean print f my girl friend my gir...

列表型別內建方法 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...

2019秋 計導練習5 B

問題描述 你的任務是實現帶小數的二進位制數到十進位制數的轉換。二進位制字串行以字元 結尾,如 1011.101 將二進位制數 1011.101 轉換為十進位制數的過程如下 二進位制字串行中只可能存在 0 1 四種字元,不會出現任何其他字元。為了提高結果的精度,建議使用double型別的變數儲存結果。...