程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning day-5

編輯:Python

Learning Diary for Python - 5

One 、 Know the list

1、 Containers

  • Containers - A variable can hold multiple data at the same time ( A box with multiple squares )
  • Non container - A variable can only hold one data ( A box with only one grid )

2、 list

  • features

    • Container data type ( A list can hold multiple data at the same time )
    • take [] As a sign of the container , Multiple elements use , separate
  • characteristic

    • The list is variable ( Number of elements in the list 、 Values and order are variable )
    • The list is ordered ( The position of each element has a corresponding index value )
    • No requirements for elements , Any type of data can , And can save multiple types of data at the same time
    # An empty list 
    list1 = []
    # The types of list elements can be the same or different 
    list2 = [10, 23.4, 'abc', True, None]
    

Two 、 Basic operation

1、 check

(1) Look up a single ( Value )

  • function : Get the corresponding element of the specified subscript ( An element )
  • explain
Noun function list Any expression that results in a list , Usually use the variable that saves the list [] Fixed writing Subscript A subscript is also called an index , It is the position information of elements in an ordered sequence . No element in the list has two subscript values ( Divided into forward and reverse )
  • 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)
    

(2) Check multiple ( section )

  • grammar
Noun function Start subscript 、 End subscript Subscript value ; Used to determine the effective range of slices : [ Start subscript , End subscript ) step Positive or negative integers ; Use the sign to determine the direction of acquisition ( just - Take... From the back ; negative - Take... Back and forth ), Use the absolute value to decide whether to jump to get , Jump a few : Fixed writing
  • The principle of acquisition
    • If the direction corresponding to the step size is inconsistent with the direction corresponding to the start subscript and the end subscript , Slice invalid , The result is empty.
    • If the slice is valid , First determine the effective range ([ Start subscript : End subscript ]), Then create a new list according to the absolute value of the step within the valid range
  • Omit grammar
    • list [ Start subscript : End subscript : Step value ]
    • Omit step size : list [ Start subscript : End subscript ] - The default step size is 1
    • Omit the closing subscript : list [ Start subscript :: Step value ] -
    • Omit start subscript : list [: End subscript : Step value ] - Step value is positive from 0 Start ; For the negative from -1 Start

(3) Traverse

  • Get elements directly ( Variable gets each element in the list in turn )
for Variable in list :
  • Now get the element subscript , Then get the element according to the subscript
for Subscript in range(len(l list ))
  • At the same time, get the elements in the list and their subscripts
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)

2、 increase

  • list .append( Elements ) - Add the specified element at the end of the list
  • list .insert( Subscript , Elements ) - Inserts the specified element before the element corresponding to the specified subscript
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]

3、 Delete

  • del list [ Subscript ] - Delete the element corresponding to the specified subscript in the list
  • list .remove( Elements ) - Remove the first matching item of an element in the list
  • list .pop() - Take out the last element of the list
  • list .pop( Subscript ) - Take out the element corresponding to the specified subscript in the list , And back to
#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]

4、 Change

  • list [ Subscript ]= The new element
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]

3、 ... and 、 Exercises

1. Basic questions

  1. 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)
    
  2. 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)
    
  3. 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)
    
  4. 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)
    
  5. 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]

  6. 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]

  7. 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

  8. 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]

2. Advanced questions

  1. 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]

  2. 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 ’]

  3. 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)
    
  4. 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]

  5. 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]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved