-list-range basis python

Compared to a list of strings, not only can store different types of data, and can store large amounts of data, 32-bit limit is python elements 536,870,912, restriction python 64 is 1152921504606846975 elements. And the list is ordered, an index value, can be sliced, convenient value.

Action list

increase

list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]

# insert 按照索引去添加
list_.insert(1, "insert_")
print(list_)  # [1, 'insert_', 2, 3, [4, 5, 6], 'Test', 'test1']

# 增加到最后
list_.append("666")
print(list_)  # [1, 'insert_', 2, 3, [4, 5, 6], 'Test', 'test1', '666']

# 迭代的去增加
list_.extend("abc")
print(list_)  # [1, 'insert_', 2, 3, [4, 5, 6], 'Test', 'test1', '666', 'a', 'b', 'c']

delete

list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]

# 按照位置去删除
list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]
ret = list_.pop(3)  # pop()删除是有一个返回值,返回值为删除的元素
print(list_)  # [1, 2, 3, 'Test', 'test1']
print(ret)  # [4, 5, 6]

# 切片删除

list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]
del list_[:3]
print(list_)  # [[4, 5, 6], 'Test', 'test1']
del list_[1]
print(list_)  # [[4, 5, 6], 'test1']
del list_
print(list_)  # NameError: name 'list_' is not defined

# 按照元素去删除
list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]
list_.remove(1)
list_.remove("Test")
print(list_)  # [2, 3, [4, 5, 6], 'test1']

# 清空列表
list_.clear()
print(list_)  # [] 列表还存在

change

list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]

list_[1] = "hello"
print(list_)  # [1, 'hello', 3, [4, 5, 6], 'Test', 'test1']

list_[:2] = "&&***"
print(list_)  # ['&', '&', '*', '*', '*', 3, [4, 5, 6], 'Test', 'test1']

list_[::2] = "!@#$%"  # 加上步长一定要对应长度相等
print(list_)  # ['!', '&', '@', '*', '#', 3, '$', 'Test', '%']

check

list_ = [1, 2, 3, [4, 5, 6], "Test", "test1"]

print(list_[0])  # 1
print(list_[-1])  # test1
print(list_[:4])  # [1, 2, 3, [4, 5, 6]]
print(list_[2:5])  # [3, [4, 5, 6], 'Test']
print(list_[1:5:2])  # [2, [4, 5, 6]]

for i in list_:
    print(i, end="")
print('\n')   # 123[4, 5, 6]Testtest1

other

list_ = [1, 2, 3, [4, 5, 6], "test", "test"]

# 获取列表的长度 len方法
print(len(list_))  # 6

# 计算某个元素出现的次数  count
print(list_.count("test"))  # 2

# index  通过元素找索引,找到第一个就返回,找不到就报错

print(list_.index(3))  # 2
print(list_.index("test"))  # 4

num_list = [1, 7, 2, 4]
num_list.sort()  # 排序是从小到大,reverse=True,则是从大到小
print(num_list)  # [1, 2, 4, 7]
num_list.sort(reverse=True)   
print(num_list)  # [7, 4, 2, 1]


list_ = [1, 2, 3, [4, 5, 6], "test", "test"]
list_.reverse()  # 反转
print(list_)  # ['test', 'test', [4, 5, 6], 3, 2, 1]

Nested list

l1 = [1, 2, 'Yang', [1, 'A', 3,]]
'''
1, 将l1中的'Yang'变成大写并放回原处。
2,给小列表[1,'A',3,]追加一个元素,'B'。
3,将列表中的'A'通过字符串拼接的方式在列表中变成'Asb'
'''
#1.
l1 = [1, 2, 'Yang', [1, 'A', 3, ]]
l1[2]=l1[2].upper()
print(l1)   # [1, 2, 'YANG', [1, 'A', 3]]
#2.
l1 = [1, 2, 'Yang', [1, 'A', 3, ]]
l1[-1].append("B")
print(l1)  # [1, 2, 'Yang', [1, 'A', 3, 'B']]
#3.
l1 = [1, 2, 'Yang', [1, 'A', 3, ]]
l1[-1][1] = l1[-1][1]+"sb"  # [1, 2, 'Yang', [1, 'Asb', 3]]
print(l1)

Tuple (tuple)

Tuples can check can not be changed

print(tuple_.count("test"))  # 2
print(tuple_.index("test"))  # 4

# 切片,索引

print(tuple_[0])  # 1
print(tuple_[-1])  # test
print(tuple_[::2])  # (1, 3, 'test')
print(tuple_[:4])  # (1, 2, 3, [4, 5, 6])


# 删除元组
del tuple_
print(tuple_)  # NameError: name 'tuple_' is not defined

# 元组的不可以更改的,但是嵌套在其中的可更改的类型是可以更改,
# 第一层是不能更改的

tuple_ = (1, 2, 3, [4, 5, 6], "test", "test")
# 可以对列表进行操作
tuple_[3].append(123)  # (1, 2, 3, [4, 5, 6, 123], 'test', 'test')
print(tuple_)

range

range () function creates a list of integers, generally used in a for loop.
The basic format:

range(start, stop[, step]

parameter:

  • start: start counting from the beginning.
  • stop: stop counting to the end, but not stop.
  • step: the step size, the default is 1.
>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2,15))
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> list(range(0,20, 2))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(-10, -1, ))
[-10, -9, -8, -7, -6, -5, -4, -3, -2]
>>> list(range(0, -10,-1 ))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

string_data = "string"

for i in range(len(string_data)):
    print(i, end=" ")
print("\n")  # 0 1 2 3 4 5 

Guess you like

Origin www.cnblogs.com/yangchangjie150330/p/10478962.html