# An empty list
list1 = []
# The types of list elements can be the same or different
list2 = [10, 23.4, 'abc', True, None]
Be careful : Subscript cannot be out of bounds , The valid range is 0 To -1 and -1 To (- length )
Length acquisition :
#len()
nums = [10, 45, 78, 9]
x=len(nums)
print(x)
for Variable in list :
for Subscript in range(len(l list ))
for Subscript , Elements in enumera( list )
nums = [34, 56, 78, 9, 23]
# Print nums All even numbers in
for x in nums:
if x % 2 == 0:
print(x)
# seek nums The sum of all elements in
result = 0
for x in nums:
result += x
print(result)
#len
nums = [34, 56, 78, 9, 23]
for x in range(len(nums)):
print(x, nums[x])
#index
nums = [34, 56, 78, 9, 23]
for index,item in enumerate(nums):
print(index, item)
nums = [20, 30]
#append
nums.append(100)
print(nums) # [20, 30, 100]
#insert
nums.insert(-1, 400)
print(nums) # [100, 200, 400, 300]
# practice 1: Extract a list of numbers nums All even numbers in
nums = [11, 20, 20, 35, 8, 7, 90, 22, 23, 5, 7, 10]
result = []
for x in nums:
if x % 2 == 0:
result.append(x)
print(result)# [20, 20, 8, 90, 22, 10]
#del
nums = [11, 20, 20, 35, 8, 7, 90, 22, 23, 5, 7, 10]
del nums[-1]
print(nums) # [11, 20, 20, 35, 8, 7, 90, 22, 23, 5, 7]
#remove
nums = [11, 20, 20, 35, 8, 7, 90, 22, 23, 5, 7, 10]
nums.remove(10)
print(nums) # [11, 20, 20, 35, 8, 7, 90, 22, 23, 5, 7]
#pop()
nums = [11, 20, 35, 8, 7, 90, 22, 20, 23, 5, 7, 10]
x = nums.pop()
print(nums, x) # [11, 20, 35, 8, 7, 90, 22, 20, 23, 5, 7]
#pop( Subscript )
x = nums.pop(1)
print(nums, x) # [11, 35, 8, 7, 90, 22, 20, 23, 5, 7]
nums = [11, 20, 35, 8, 7, 90, 22, 20, 23, 5, 7, 10]
nums[1] = 200
print(nums) # [11, 200, 35, 8, 7, 90, 22, 20, 23, 5, 7, 10]
Know a list of numbers , Print all odd numbers in the list
nums=[1,2,3,4,5,6,7,8]
for x in nums:
if x%2:
print(x)
Know a list of numbers , Print all that can be in the list 3 Divisible but not by 2 Divisible number
nums=[1,2,3,4,5,6,7,8,9,12]
for x in nums:
if x%2:
if x%3==0:
print(x)
Know a list of numbers , Calculate the sum of all even numbers
sums=0
nums=[1,2,3,4,5,6,7,8,9,12]
for x in nums:
if x%2==0:
sums+=x
print(sums)
Know a list of numbers , The ten digits in the statistical list are 1
The number of
sums=0
nums=[5,8,12,11,16,25,47,146,211,876,1217]
for x in nums:
if x//10%10==1:
sums+=1
print(sums)
Know a list , Get all elements in the list whose subscript is odd ( from 0 Starting subscript value )
list1 = [10, 20, 5, 34, 90, 8]
print(list1[1::2])
for example : list1 = [10, 20, 5, 34, 90, 8]
result :[20, 34, 8]
Know a list of numbers , Multiply all elements in the list by 2
# Method 1
nums = [10, 3, 6, 12]
list=[]
for x in nums:
y=x*2
list.append(y)
print(list)
# Method 2
nums = [10, 3, 6, 12]
for index,item in enumerate(nums):
nums[index]=item*2
print(nums)
for example : nums = [10, 3, 6, 12] ride 2 after : nums = [20, 6, 12, 24]
Know a list , Get the central element of the list
nums = [10, 2, 6, 12]
num2=[]
x=(len(nums))
if x %2:
y=int((x-1)/2)
num2.append(nums[y])
else:
y=int(x/2)
num2.append(nums[y-1])
num2.append(nums[y])
print(num2)
for example :nums = [10, 2, 6, 12] -> The central element is : 2 and 6
nums = [10, 2, 6, 12, 10] -> The central element is :6
Know a list , Get all integer elements in the list
list1 = [10, 1.23, 'abc', True, 100, 'hello', '20', 5]
result=[]
for x in list1:
if type(x)==int:
result.append(x)
print(result)
for example :list1 = [10, 1.23, ‘abc’, True, 100, ‘hello’, ‘20’, 5]
The result is : [10, 100, 5]
Define a list to hold scores of multiple students , Delete all items in the list 60 Value of score
scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
# Method 1
nums=[]
for x in scores:
if x <60:
continue
else:
nums.append(x)
print(nums)
# Method 2
for index in range(len(scores)-1,-1,-1):
if scores[index]<60:
del scores[index]
print(scores)
for example : scores = [45, 60, 89, 30, 12, 59, 99, 80, 71, 66] After deleting : scores = [60, 89, 99, 80, 71, 66]
It is known that a list holds the names of multiple students , Ask to remove duplicate names from the list
names = [' Xiao Ming ', ' Zhang San ', ' Li Si ', ' Zhang San ', ' Zhang San ', ' Xiao Ming ', ' Wang Wu ', ' Wang Wu ']
list=[]
for x in names:
if x in list:
continue
else:
list.append(x)
print(list)
for example :names = [‘ Xiao Ming ’, ‘ Zhang San ’, ‘ Li Si ’, ‘ Zhang San ’, ‘ Zhang San ’, ‘ Xiao Ming ’, ‘ Wang Wu ’, ‘ Wang Wu ’]
After weight removal :names = [‘ Xiao Ming ’, ‘ Zhang San ’, ‘ Li Si ’, ‘ Wang Wu ’]
Know a list of numbers , Get the element with the largest value in the list ( Out of commission max function )
list= [45, 60, 89, 30, 12, 59, 99, 80, 71, 66]
# Method 1
list.sort()
print(list[-1])
# Method 2
y=list[0]
for x in list:
if y<=x:
y=x
print(y)
Two known sequence tables ( The elements in the list have been arranged in order from small to large ), Ask to merge two lists , After merging, the elements are sorted from small to large
list1 = [10, 23, 39, 41, 52, 55, 80]
list2 = [9, 38, 55, 70]
list3=list2+list1
list3.sort()
print(list3)
for example : list1 = [10, 23, 39, 41, 52, 55, 80] list2 = [9, 38, 55, 70]
The result of the merger : [9, 10, 23, 38, 39, 41, 52, 55, 55, 70, 80]
Given an ordered list of numbers ( From small to large ), Enter any number , Insert the entered number into the list , It is required that the list still keeps the relationship of sorting from small to large after insertion
list1 = [10, 23, 45, 67, 91]
y=50
index=0
for x in list1:
if x<=y:
index+=1
else:
list1.insert(index,y)
break
print(list1)
for example : list1 = [10, 23, 45, 67, 91] Input : 50 -> list1 = [10, 23, 45, 50, 67, 91]