python基礎-17列表類型內置方法

列表類型內置方法(list)

  • 1.用途:多個裝備、多個愛好、多門課程,甚至是多個女友python

  • 2.定義:[]內能夠有多個任意類型的值,逗號分隔元素
# my_girl_friend = list(['jason','tank','sean'])
my_girl_friend = ['jason', 'tank', 'sean']

print(f"my_girl_friend: {my_girl_friend}")

my_girl_friend: ['jason', 'tank', 'sean']app

l = list('hello nick')
print(f"l: {l}")

l: ['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']
3*.經常使用操做+內置方法:code

 1.按索引取值(正向取值+反向取值),便可存也能夠取索引

 2.切片rem

 3.長度lenclass

 4.成員運算in和not in循環

 5.追加append數據類型

 6.刪除del方法

 7.循環數據

1.按索引取值(正向取值+反向取值),便可存也能夠取

# list之索引取值
name_list = ['nash', 'langyigang', 'fujiachen', 'jinyi']
name_list[0] = 'nash handsom'
# name_list[1000] = 'tank sb'  # 報錯

print(f"name_list[0]: {name_list[0]}")

name_list[0]: nash handsom

2.切片

# list之切片
name_list = ['nash', 'langyigang', 'fujiachen', 'jinyi']
print(f"name_list[0:3:2]: {name_list[0:3:2]}")

name_list[0:3:2]: ['nash', 'fujiachen']

3.長度

# list之長度
name_list = ['nash', 'langyigang', 'fujiachen', 'jinyi']

print(f"len(name_list): {len(name_list)}")

len(name_list): 4

4.成員運算in和not in

# list之成員運算in和not in
name_list = ['nash', 'fujiachen', 'langyigang', 'jinyi']

print(f"'langyigang sb' in name_list: {'langyigang sb' in name_list}")
print(f"'nash handsome' not in name_list: {'nash handsome' not in name_list}")

'fujiachen sb' in name_list: False 'nash handsome' not in name_list: True

5.追加值

# list之追加值
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
name_list.append('langyigang sb')

print(f"name_list: {name_list}")

name_list: ['nash', 'xuguangduo', 'fujiachen', 'jinyi', 'langyigang sb']

6.刪除

# list之刪除
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
del name_list[2]

print(f"name_list: {name_list}")

name_list: ['nash', 'xuguangduo', 'jinyi']

7.循環

# list之循環
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

for name in name_list:
    print(name)

nash xuguangduo fujiachen jinyi

其餘方法

insert

pop

remove

count

index

clear

copy

extend

reverse

sort

1.insert()

# list之insert()
name_list = 
name_list.insert(1, 'handsome')

print(f"name_list: {name_list}")

name_list: ['nash', 'handsome', 'xuguangduo', 'fujiachen', 'jinyi']

2.pop()

# list之pop(),pop()默認刪除最後一個元素
name_list = ['nash','xuguangduo', 'fujiachen', 'jinyi']

print(f"name_list.pop(1): {name_list.pop(1)}")
print(f"name_list: {name_list}")

name_list.pop(1): jason name_list: ['nash', 'fujiachen', 'jinyi']

3.remove()

# list之remove()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

print(f"name_list.remove('nash'): {name_list.remove('nick')}")
print(f"name_list: {name_list}")

name_list.remove('nick'): None name_list: ['xuguangduo', 'fujiachen', 'jinyi']

4.count()

# list之count()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

print(f"name_list.count('nash'): {name_list.count('nash')}")

name_list.count('nash'): 1

5.index()

# list之index()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

print(f"name_list.index('nash'): {name_list.index('nash')}")

name_list.index('nash'): 0

6.clear()

# list之clear()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
name_list.clear()

print(f"name_list: {name_list}")

name_list: []

7.copy()

# list之copy()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

print(f"name_list.copy(): {name_list.copy()}")

name_list.copy(): ['nash', 'xuguangduo', 'fujiachen', 'jinyi']

8.extend()

# list之extend()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
name_list2 = ['nash handsome']
name_list.extend(name_list2)

print(f"name_list: {name_list}")

name_list: ['nash', 'xuguangduo', 'fujiachen', 'jinyi', 'nash handsome']

9.reverse()

# list之reverse()
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
name_list.reverse()

print(f"name_list: {name_list}")

name_list: ['jinyi', 'fujiachen', 'xuguangduo', 'nash']

10.sort()

# list之sort(),使用sort列表的元素必須是同類型的
name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi']
name_list.sort()

print(f"name_list: {name_list}")

name_list.sort(reverse=True)
print(f"name_list_reverse: {name_list}")

name_list = ['nash', 'xuguangduo', 'fujiachen', 'jinyi'] name_list.sort() print(f"name_list: {name_list}") name_list.sort(reverse=True) print(f"name_list_reverse: {name_list}") ``

存一個值or多個值:

多個值

有序or無序:有序

hobby_list = ['read', 'run', 'girl']
print(f'first:{id(hobby_list)}')
hobby_list[2] = ''
print(f'second:{id(hobby_list)}')
first:4522187016
second:4522187016

可變or不可變:可變數據類型

相關文章
相關標籤/搜索