027 列表類型內置方法

列表類型內置方法(list)

1.做用

用來描述多個裝備、多個愛好、多種狀態等python

2.定義

在[]裏能夠存放多個任意類型的值,逗號分隔元素app

hobby_list = ['run','play','read']
hobby_list = list ['run','play','read']

print(f'my hobby_list:{hobby_list}')
# my hobby_list:['run','play','read']

3.經常使用操做+內置方法

**優先掌握**code

1.按索引取值排序

2.切片(split)繼承

3.長度(len)索引

4.成員運算(is/is not)ci

5.for循環rem

6.刪除(del)it

7.追加(append)io

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

# list之索引取值
hobby_list = ['play', 'swimming', 'dancing', '666']

print(1, hobby_list[-1])
hobby_list[-1] = '233'  # 索引修改
print(2, hobby_list[-1])

2.切片(split)

# list之切片
print(hobby_list[:])  # ['play', 'swimming', 'dancing', '666']
print(hobby_list[::-1])  # 從右到左  # ['666', 'dancing', 'swimming', 'play']

# ['play', 'swimming', 'dancing', '233']
# ['233', 'dancing', 'swimming', 'play']

3.長度(len)

# list之長度
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"len(name_list): {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: {'tank sb' in name_list}")
print(f"'nick handsome' not in name_list: {'nick handsome' not in name_list}")

# 'tank sb' in name_list: False
# 'nick handsome' not in name_list: True

5.循環(for)

# list之循環
name_list = ['nick', 'jason', 'tank', 'sean']

for name in name_list:
    print(name)
    
##結果    
nick
jason
tank
sean

6.刪除(del)

# list之刪除
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]

print(f"name_list: {name_list}")

# name_list: ['nick', 'jason', 'sean']

7.追加值(append)

# list之追加值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.append('tank sb')

print(f"name_list: {name_list}")

# name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']

**須要掌握**

1.insert(插入,在列表的某一個位置插入一個元素)

2.pop(刪除元素.默認刪除列表最後一個元素)

3.remove(移除元素)

4.count(計數.u哦你過來計數列表裏面某一元素出現的次數)

5.index(索引.返回列表裏某一元素的位置)

6.claar(清空列表)

7.copy(複製列表)

8.extend(繼承,也就是兩個列表何在一塊兒)

9.reverse(反轉列表的元素,從右到左進行排序)

10.sort(排序.默認升序排)

1.insert(插入元素)

hobby_list = ['run','play','read']
hobby_list.insert(1,'sleep')

print(hobby_list)
# ['run', 'sleep', 'play', 'read']

2.pop(刪除元素)

# pop默認刪除的是最後一個元素,可是也能夠指定索引的位置
hobby_list = ['run','play','read']
hobby_list.pop()
print(hobby_list)
# ['run', 'play']

hobby_list = ['run','play','read']
hoppy_list.pop(0)
print(hobby_list)
# ['play','read']

3.remove(移除)

hobby_list = ['run','play','read']
hobby_list.remove('run')
print(hobby_list)
# ['play','read']

4.count(統計某一元素在列表出現的次數)

hobby_list = ['run','play','read']

print(hobby_list.count('run'))

# 1

5.index()

hobby_list = ['run','play','read']
print(hobby_list(play))

# 1

6.clear()

hobby_list = ['run','play','read']
hobby_list.clear()

print(hobby_list)

# []

7.copy()

hobby_list = ['run','play','read']
hobby_list2 = hobby_list.copy

print(hobbby_list2)

# ['run','play','read']

8.extend()

hobby_list = ['run','play','read']
hobby_list2 = ['sing']

hobby_list.extend(hobby_list2)

print(hobby_list)

# ['run', 'play', 'read', 'sing']

9.reverse()

hobby_list = ['run','play','read']

hobby_list.reverse()

print(hobby_list)

# ['read', 'play', 'run']

10.sort()

hobby_list = ['run','play','read']

hobby_list.sort()
print(hobby_list)
['play', 'read', 'run']

4.列表是存多個值的

5.列表是有序的

hobby_list = ['run','play','read']
print(id(hobby_list))
hobby_list[2] = 'sing'
print(id(hobby_list))

#結果
2855383215368
2855383215368

6.可變數據類型

相關文章
相關標籤/搜索