list : Data sequence , Is a continuous storage space that can store multiple data .
a_list = [1,2,3]
a_list = ['this','is','a','list']
a_list = ['this','is','test',1,2,3]
a_list = list('hello')
# ['h', 'e', 'l', 'l', 'o']
a_list = list(range(1,20,2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
a_list = ['this','is','a','list']
print(a_list[0])
# this
print(a_list[-1])
# list
print(a_list[5])
# IndexError: list index out of range
print(a_list[1:3])
# ['is', 'a']
print(a_list[1:])
# ['is', 'a', 'list']
a_list = ['this', 'is', 'a', 'list']
for x in a_list:
print(x)
for i in range(len(a_list)):
print(a_list[i])
list = list(range(1,20,2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
x = int(input(' Please enter an integer to find :'))
for i in range(len(list)):
if i == x:
print(' eureka , Integers {} In the list '.format(x))
list = list(range(1, 20, 2))
print(list)
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
sign = False
x = int(input(' Please enter an integer to find :'))
for i in range(len(list)):
if list[i] == x:
print(' eureka , Integers {} In the list '.format(x))
sign = True
if not sign:
print(' I didn't find it ')
# Data search correlation function
list = list(range(1, 20, 2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(list.index(13))
# 6
# The first data returned is 13 The subscript
print(list.index(15, 2, 10))
# 7
# return 2 To 9(10-1) The first data between is 15 The subscript
temp_list = [1, 1, 1, 1, 1, 2, 3]
print(temp_list.count(1))
# 5
# The returned data is 1 The number of
list = [1, 2, 2, 3, 3, 3, 4, 4, 5, 6]
x = int(input(' Please enter an integer to find x:'))
n = list.count(x)
if n == 0:
print(' Did not find {}'.format(x))
else:
j = 0
for i in range(n):
j = list.index(x, j, len(list)) + 1
# Specify a range to find
print(' eureka , The first {} The number is {}'.format(j, x))
# Two points search
list = list(range(1, 20, 2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
x = int(input(' Please enter an integer to find :'))
start = 0
end = len(list) - 1
while start <= end:
mid = (start + end) // 2
if list[mid] == x:
break
elif x < list[mid]:
end = mid - 1
else:
start = mid + 1
if start <= end:
print(' eureka , The first {} The number is {}'.format(mid + 1, x))
else:
print(' Did not find ')
list = [1, 3, 5, 7, 9, 2, 4, -1, -1]
# here -1 Said is empty , Represents a placeholder
x = int(input(' please enter an integer x:'))
i = len(list) - 1
while i >= 0:
if list[i] != -1:
break
i -= 1
list[i + 1] = x
print(list)
# [1, 3, 5, 7, 9, 2, 4, 1, -1]
# append( Add data to the end of the list )
list = list(range(1,10,2))
# [1, 3, 5, 7, 9]
x = int(input(' please enter an integer x:'))
list.append(x)
print(list)
# [1, 3, 5, 7, 9, 1]
# ---
# extend() Function to append multiple values in another sequence at the end of the list at one time ( Extend the original list with the new list ).
list = list(range(1,10,2))
# [1, 3, 5, 7, 9]
list.extend([1,2,3])
print(list)
# [1, 3, 5, 7, 9, 1, 2, 3]
# ---
# insert() Function is used to insert the specified object into the specified position in the list .
list = list(range(1, 10, 2))
# [1, 3, 5, 7, 9]
list.insert(1, 100)
# list.insert(index, obj)
print(list)
# [1, 100, 3, 5, 7, 9]
list = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
x = int(input(' Please enter an integer to delete x:'))
n = len(list)
for i in range(n):
if list[i] == x:
list[i] = -1
print(list)
# [-1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
# pop() Function to remove an element from the list ( Default last element ), And returns the value of that element
list = [1, 3, 5, 7, 9]
list.pop()
# [1, 3, 5, 7]
# Delete the last... By default
list.pop(1)
# [1, 5, 7]
# Delete the second element of the list
# ---
# remove() Function to remove the first match of a value in the list
list = [1, 3, 5, 7, 9]
list.remove(3)
# [1, 5, 7, 9]
list = [1, 1, 1, 1, 1, 1, 3, 5, 5, 5, 5, 5, 5, 5, 7, 9]
x = int(input(' Please enter an integer to delete :'))
n = list.count(x)
# n How many for the list x, perform n Time remove() Function to delete all in the list x value
for i in range(n):
list.remove(x)
print(list)
# [3, 5, 5, 5, 5, 5, 5, 5, 7, 9]