python of the list [list]

1. What lists are?

  • list [ ]

  • Separated by commas
  • It is a container
  • You can store any type

列表 == 书包
书包里可以放水杯、衣服、袜子、钱包
钱包里可以放钱、身份证件,可以包套包

2. The list can you do?

  • Store large amounts of data
  • Ordered
  • Variable can be modified
  • Before what format on the list or what format
  • It can be iterated

3. Cut List

Method One: Index

li = [1,2,3,"123",True]
print(li[3],type(li[3]))
print(li[-1],type(li[-1]))
'''
输出结果
123 <class 'str'>
True <class 'bool'>
'''
# 个人见解
# 切片后还是原来的数据类型

Method Two: Slice

li = [1,2,3,"123",True]
print(li[1:4])
'''
[2,3,"123"]
'''

Method three: a slice index plus step

li = [1,2,3,"123",True]
print(li[1:4:2])
'''
输出结果
[2, '123']
'''
s = "alex"
print(type(s[1:3:1]))   
print(type(li[-1:-4:-1]))
# 切片后还是原来的数据类型

Sentence summary

Slice the list:

Index: li [2]

Slice: li [2: 3]

Step: li {2: 3: 2}

4. Create a list

Method One: directly create

li = [1,2,3,'123',True,[1,2,5]] 

Method two: a string list generation

l1 = list()
l1 = list('fhdsjkafsdafhsdfhsdaf')
print(l1)
# 输出结果
['f', 'h', 'd', 's', 'j', 'k', 'a', 'f', 's', 'd', 'a', 'f', 'h', 's', 'd', 'f', 'h', 's', 'd', 'a', 'f']

# 底层使用了for循环  "abc" 可迭代的字符串类型,所以打印出如此内容

Sentence summary

List creation method:

li = [1,2,3,'123',True,[1,2,5]]

l1 = list('fhdsjkafsdafhsdfhsdaf')

5. increasing list

5.1 append ( 'content to insert') (Results: appended at the end)

Method: Direct add elements

li = [1,2,3,'alex',3,[1,2,3]]
li.append('太亮') # 追加  添加在末尾  添加的操作
'''
输出结果
[1,2,3,'alex',3,[1,2,3],'太亮']
'''

5.2 instert (index, 'content to insert') (Results: Low efficiency)

Methods: index element

li.insert(0,"大象")           
# 插入 第一个参数(索引),第二参数要插入的内容

5.3 extend ( 'To add a content') (result: recursive addition, the characters will open)

Method : Direct add an element, not an iteration object: int bool

li = [1,2,3]
li.append('abc')
print(li)
'''
[1,2,3,'a','b','c']
'''

5.4 List of merger (two spliced ​​into a list)

Usage : + together

l2 = [1,2,3,'abc']
l3 = [7,8,9,'hello']
print(l2 + l3)
[1,2,3,'abc',7,8,9,'hello']

Sentence summary

A method of increasing the list:

append ( 'Content') Characteristics: The default suffix is ​​added

instert (index, 'content') Features: Low Efficiency

Extend ( 'content') Features: iterations, open string

6. list of deleted

6.1 pop ( 'element to be removed / not write to delete the last element is the default') (Result: returns a value, the value returned is content to be deleted)

Method One : Default Removes the last element, and returns a value, the element is deleted

# 思考删除abc
li = [1,2,3,'abc']
li.pop()
print(li)
print(li.pop())
'''
输出结果
[1,2,3] #删除后的列表
abc # 被删除的元素
''' 
# abc被删除

Method Two: remove elements according to indexes

li = [7,8,9,'abc']
li.pop(2)
print(li)
'''
输出结果
[7,8,abc]
'''

Sentence summary

pop deleted in three ways:
  • The default removing the trailing li.pop ()
  • Delete elements li.pop ( 'abc')
  • Delete indexes, delete the corresponding element is li.pop (2)
pop delete the return value, the return value is deleted elements, print mode print (li.pop ())

This section Problem:

列表li = [1,2,3,'abc'],请用2种方法删除元素'abc'

6.2 remove ( 'removed element') (deleted by elemental)

Method : Remove the element name in accordance with

li = [1,2,3,'abc',True]
li.remove(1)
print(li)
[2,3,'abc',True]

Sentence summary

remove ( 'element'), from left to right to delete, delete the first to have the same name on the left side

6.3 del list name (deleting the entire list)

Method one: del li delete the entire list

li = [1,2,3,"abc",True]
del li
print(li)
'''
输出结果
NameError: name 'li' is not defined, li列表没有被定义
'''

Method two : Delete Index

del li[2]

Method three : Slice delete

del li[0:3]

Method four : Delete step

del li[::2]

Sentence summary:

del li entire list

del [2] Index

del [2: 3] slices

del [2: 3: 2] step

6.4 clear () empty, an empty list obtained

Methods :

li.clear() # 格式
print(li)
[]

7. change list

Method a : by changing the index

li = ["水杯",2,3,"abc",]
li[-1] = "奶瓶" # 将'abc'改成奶瓶
print(li)
'''
["水杯",2,3,"奶瓶",]
'''

li[1] = [1,2,3] # 将元素改成一个列表
'''
["水杯",[1,2,3],3,"abc",]
'''

Method two : by changing slice

  • Can be more or less
  • You can specify empty
li = ["水杯",2,3,"abc",]
li[0:3] = [1,2,3],[1234]
'''
[[1, 2, 3], [1234], 'abc']
'''
# 将0-3改成[1,2,3],[1234]
li[0:4] = [1,2,3],[1234]
'''
[[1,2,3],[1234]]
'''

Method three : The step size change can take several put several more, no less

li = ["水杯",2,3,"abc",]
li[0:3:2] = [1,2,3],[1234]
print(li)
'''
[[1, 2, 3], 2, [1234], 'abc']
'''

8. check list

Methods : The index check

li = ['abc',5,'太白','老吉普','d']
print(li[3])
'''
老吉普
'''

Methods : By slicing

li = ['abc',5,'太白','老吉普','d']
print(li[1:4])
'''
[5,'太白','老吉普']
'''

Method : by step

li = ['abc',5,'太白','老吉普','d']
print[li[1:4:2]]
'''
[5,'老吉普']
'''

Method : for loop

li = ['abc',5,'太白','老吉普','d']
for i in li:
  print(i)
'''
['abc',5,'太白','老吉普','d']
'''

9. nested list

li = ["水杯","衣服","鞋子","书","电脑","屁",["手机","钥匙",["身份证","银行卡"]],]
l2 = li[6]
print(l2)
'''
["手机","钥匙",["身份证","银行卡"]]
'''
l3 = l2[2]
print(l3)
'''
["身份证","银行卡"]
'''
l4 = l3[1]
print(l4)
'''
银行卡
'''
可以写成如下:
l5 = li[6][2][1] # 第一个列表的索引6内部列表索引2,再内部列表索引1
print[l5]
'''
银行卡
'''

Problem list:

li = [1,2,3,"123",True]
1. 用学过的方法输出以下结果
123 <class 'str'>
True <class 'bool'>
[2,3,"123"]
[2, '123']
2. 查看下s = "alex"的数据类型

li = ["水杯",2,3,"abc",]
3. 两种方法将abc删除

li = [1,2,3,'alex',3,[1,2,3]]
4. 在列表末尾添加一个'太白'
5. 低效率方法在第一个位置插入一个'大象'
6. 用两种方法在列表末尾位置添加一个'a','b','c'

l3 = [1,2,3]
l2 = ["a","b","c"]
7. 将这两个列表合并成一个

li = [1,2,3,"abc",True]
8. 将这个列表中的'abc'删除
9. 两种方法将这个列表中的True删除

li = [1,2,3,'abc']
10. 将第二个字符改成一个列表
11. 用改的方法将列表里元素2和3删除

li = [1,2,3,4,"abc",5]
li[0:2] = "ABC"
12. 上题结果

li = ["水杯",2,3,"abc",]
13. 将'水杯'和3分别改成[1,2,3],[1234]

li = ["水杯",2,3,"abc",]
14. 将列表变成这样[[1,2,3],[1234]]

li = ["水杯","衣服","鞋子","书","电脑","屁",["手机","钥匙",["身份证","银行卡"]],]
15. 将'银行卡'提取出来

Guess you like

Origin www.cnblogs.com/zanao/p/10990198.html