列表类型内置方法

列表类型内置方法

一、列表类型内置方法(list)

1.用途:多个装备、多个爱好、多门课程。

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

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

l: ['h', 'e', 'l', 'l', 'o', ' ', 'n', 'i', 'c', 'k']

3.常用操作+内置方法:

1.1优先掌握(*************)

  1. 按索引取值(正向取值+反向取值),即可存也可取
  2. 切片
  3. 长度len
  4. 成员运算in和not in
  5. 追加append
  6. 删除del
  7. 循环

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

# list之索引取值
name_list = ['lh', 'hello', 'word']
name_list[0] = 'nick'
# name_list[1000] = 'handsome'  #报错

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

name_list[0]: nick

2.切片

# list之切片
name_list = ['lh', 'hello', 'word', 'handsome']

print(f'name_list[0:3:2]: {name_list[0:3:2]}')

name_list[0:3:2]: ['lh', 'word']

3.长度

# list之长度
name_list = ['lh', 'hello', 'word', 'handsome']

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

len(name_list): 4

4.成员运算in和not in

# list之成员运算in和not in
name_list = ['lh', 'hello', 'word', 'handsome']

print(f"'tank' in name_list: {'tank' in name_list}")
print(f"'nick' not in name_list: {'nick' not in name_list}")

'tank' in name_list: False
'nick' not in name_list: True

5.追加值

# list之追加值
name_list = ['lh', 'hello', 'word', 'handsome']
name_list.append('nick handsome')

print(f'name_list: {name_list}')

name_list: ['lh', 'hello', 'word', 'handsome', 'nick handsome']

6.删除

# list之删除
name_list = ['lh', 'hello', 'word', 'handsome']
del name_list[2]

print(f'name_list: {name_list}')

name_list: ['lh', 'hello', 'handsome']

7.循环

# list之循环
name_list = ['lh', 'hello', 'word', 'handsome']

for name in name_list:
    print(name)

lh
hello
word
handsome

1.2需要掌握(************)

  1. insert
  2. pop
  3. remove
  4. count
  5. index
  6. clear
  7. copy
  8. extend
  9. reverse
  10. sort

1.insert()

# list之insert()
name_list = ['lh', 'hello', 'word', 'handsome']
name_list.insert(1, 'handsome')

print(f"name_list: {name_list}")

name_list: ['lh', 'handsome', 'hello', 'word', 'handsome']

2.pop()

# list之pop(),pop()默认删除最后一个元素
name_list = ['lh', 'hello', 'word', 'handsome']

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

name_list.pop(1): hello
name_list: ['lh', 'word', 'handsome']

3.remove()

# list之remove()
name_list = ['lh', 'hello', 'word', 'handsome']

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

name_list.remove('lh'): None
name_list: ['hello', 'word', 'handsome']

4.count()

# list之count()
name_list = ['lh', 'hello', 'word', 'handsome']

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

name_list.count('lh'): 1

5.index()

# list之index()
name_list = ['lh', 'hello', 'word', 'handsome']

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

name_list.index('lh'): 0

6.clear()

# list之clear()
name_list = ['lh', 'hello', 'word', 'handsome']
name_list.clear()

print(f"name_list: {name_list}")

name_list: []

7.copy()

# list之copy()
name_list = ['lh', 'hello', 'word', 'handsome']

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

name_list.copy(): ['lh', 'hello', 'word', 'handsome']

8.extend()

# list之extend()
name_list = ['lh', 'hello', 'word', 'handsome']
name_list2 = ['lh handsome']
name_list.extend(name_list2)

print(f"name_list: {name_list}")

name_list: ['lh', 'hello', 'word', 'handsome', 'lh handsome']

9.reverse() 逆序排列

# list之reverse()
name_list = ['lh', 'hello', 'word', 'handsome']
name_list.reverse()

print(f"name_list: {name_list}")

name_list: ['handsome', 'word', 'hello', 'lh']

10.sort()

# list之sort(),使用sort列表的元素必须是同类型的
name_list = ['lh', 'hello', 'word', 'handsome']
name_list.sort()

print(f"name_list:{name_list}")

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

name_list:['handsome', 'hello', 'lh', 'word']
name_list_reverse: ['word', 'lh', 'hello', 'handsome']

4.存一个值or多个值:多个值

5.有序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

6.可变or不可变:可变数据类型

原文地址:https://www.cnblogs.com/Lin2396/p/11305804.html