python a basic data type (list)

List

List python is one of the basic data types, other programming languages ​​have similar data types.

. For example the JS array, java, etc. It is the array [] enclosed, with each element ',' spaced apart and can store various types of data:

The list is one of the basic data types in python, and other languages ​​are also similar to the list of data types, such as the call js array, he is [] enclosed, with each element separated by commas, and he can be stored inside each data types such as:

li = [‘alex’,123,Ture,(1,2,3,’wusir’),[1,2,3,’小明’,]]

Compared to a list of strings, not only can store different types of data, and can store large amounts of data,

32 is a python limit elements 536,870,912,

64 is a restriction python 1152921504606846975 elements. And the list is ordered, an index value, can be sliced, convenient value.

lst = [1,2,3,"meet",[1,2,3]]

We define a list, we now take a look at how memory is stored

image-20190624212208882

lst we can imagine into a bag, stored in a bag of clothes, computers, books, purse, wallet, put the ID card, bus cards, house keys

1 index list

And also has a list of strings as index:

lst = ['刘德华','周润发','周杰伦','向华强']
print(lst[0])  # 列表中第一个元素
print(lst[1])  # 列表中第二个元素
print(lst[2])  # 列表中第三个元素

Note: The list can be modified, and the string is not the same here

lst[3] = '王健林'
print(lst)

Modify the string

s = '王思聪'
s[0] = '李'
print(s)
结果:
Traceback (most recent call last):
  File "D:/python_object/path2/test.py", line 1076, in <module>
    s[0] = '李'
TypeError: 'str' object does not support item assignment

Slice 2 of the list

lst = ["麻花藤", "王剑林", "马芸", "周鸿医", "向华强"] 
print(lst[0:3])     # ['麻花藤', '王剑林', '马芸'] 
print(lst[:3])      # ['麻花藤', '王剑林', '马芸']
print(lst[1::2])    # ['王剑林', '周鸿医'] 也有步长 
print(lst[2::-1])   # ['马芸', '王剑林', '麻花藤'] 也可以倒着取 
print(lst[-1:-3:-2])    # 倒着带步长

3 additions and deletions to change search list

increase

Note that list and str are not the same. Lst can change so directly carried out the operation on the original object

Append mode

lst = ["麻花藤", "林俊杰", "周润发", "周芷若"] 
print(lst) 
lst.append("wusir") 
print(lst)

Insert mode

lst = ["麻花藤", "张德忠", "孔德福"]
lst.insert(1, "刘德华")    # 在1的位置插入刘德华. 原来的元素向后移动一位
print(lst)

Add iterative

# 迭代添加
lst = ["王志文", "张一山", "苦海无涯"]
lst.extend(["麻花藤", "麻花不疼"])
print(lst)

delete

pop 通过下标删除元素(默认删除最后一个)

lst = ["麻花藤", "王剑林林", "李李嘉诚", "王富贵"] 
print(lst)
lst.pop()

deleted = lst.pop()
print('被删除的',deleted)
print(lst)

el = lst.pop(2)  # 删除下标位2的元素
print(el)        # 被删除的元素
print(lst)

remove deleted by elemental

lst = ["麻花藤", "王剑林", "李嘉诚", "王富贵"]
lst.remove('王剑林')
print(lst)

结果:
['麻花藤', '李嘉诚', '王富贵']

lst.remove('哈哈')   # 删除不存在的元素
结果:
Traceback (most recent call last):
  File "D:/python_object/path2/test.py", line 1115, in <module>
    lst.remove('哈哈')   # 删除不存在的元素
ValueError: list.remove(x): x not in list

Empty clear

lst = ["麻花藤", "王剑林", "李嘉诚", "王富贵"]
lst.clear()
print(lst)

结果:
[]

modify

Modify slice index

# 修改 
lst = ["太白", "太黑", "五色", "银王", "⽇天"] 
lst[1] = "太污"   # 把1号元素修改成太污 print(lst) 
lst[1:4:3] = ["麻花藤", "哇靠"]     # 切片修改也OK. 如果步长不是1, 要注意元素的数 
print(lst) 
lst[1:4] = ["我是哪个村的村长王富贵"]  # 如果切片没有步长或者步长是1. 则不用关心个数 
print(lst)

Inquire  

列表是一个可迭代对象,所以可以进行for循环

lst = ["麻花藤", "王剑林", "李嘉诚", "王富贵"]

for i in lst:
    print(i)

结果:
麻花藤
王剑林
李嘉诚
王富贵

Nested list

Note: The dimensionality reduction operation, one level of look like

lst = [1,'太白','wusir',['麻花疼',['可口可乐'],'王健林']]

# 找到wusir
print(lst[2])

# 找到太白和wusir
print(lst[1:3])

# 找到太白的白字
print(lst[1][1])

# 将wusir拿到,然后首字母大写 在扔回去

s = lst[2]
s = s.capitalize()
lst[2] = s
print(lst)

# 简写
lst[2] = lst[2].capitalize()
print(lst)

# 把太白换成太黑
lst[1] = lst[1].replace('白','黑')

# 把麻花疼换成麻花不疼
lst[3][0] = lst[3][0].replace('疼','不疼')
print(lst)

# 在可口可乐后边添加一个雪碧
lst[3][1].append('雪碧')
print(lst)

You need to store large amounts of data and require these data when ordered. Develop some special data groups: in order, according to the rules, custom design data

Guess you like

Origin www.cnblogs.com/luckinlee/p/11619845.html