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

Python learning notes: List

編輯:Python
###########################################################
# 1、 List definition , List creation
# 1) Define an empty list
# list1 = []
# 2) Define a non empty list
# list2 = [1, 2, 3, 4]
# 3) Can contain various types of data
# list3 = ['datian', 1, 9.8, ('datian', 22, 33)]
# print(list3)
# 4) You can define an iteratable sequence
# list4 = list("datian")
# print(list4)
# 5)range function - Iterative sequence
# list5 = list(range(5))
# print(list5)
# 6) Tuple conversion list
# list6 = list((1, 2, 3))
# print(list6)
###########################################################
# 2、 List uses
# list7 = ["a", "b", "c", "d", "e"]
# 1) Access the first element of the list
# print(list7[0]) # Running results :a
# 2) Access the last tuple of the list
# print(list7[-1])
# 3) Slicing operation
# print(list7[1:3]) # Return to the new list , The second to the fourth are returned
# 4) At the end of the list , Add an element
# list7.append("datian")
# print(list7)
#
# # 5) Check the length of the list
# print(len(list7))
#
# # 6) Modify the first element in the list
# list7[0] = "test"
# print(list7)
#
# # 7) Delete the first element in the list
# del list7[0]
# print(list7)
###########################################################
# 3、 List functions
# list8 = ['aa', 'bb', 'cc', 'dd', 'ee']
# 1) Insert an element
# The index is 1 Add an element before bc
# list8.insert(1, 'bc')
# The index is 3 Add an element before cd
# list8.insert(3, 'cd')
# The index is -1 Add an element before de
# list8.insert(-1, 'de')
# 2) Remove elements
# Delete the last element of the list
# list8.pop()
# Delete index as 3 The elements of , It should be noted that pop Function has return value , And give it to list9
# list9 = list8.pop(3)
# print(list8,list9)
# Delete the list element as aa Value , It should be noted that remove Function has no return value
# list10 = list8.remove('aa')
# print(list8,list10)
# 3) clear list
# list8.clear()
# print(list8)
# 4) Add an element at the end of the list ff
# list8.append('ff')
# print(list8)
# 5) Expand list value
# list8.extend(['1', '2'])
# print(list8)
# 6) Flip list reverse
# list8.reverse()
# print(list8)
# 7) Sort the list
# list9 = ['4', '2', '5', '3', '1']
# Order
# list9.sort()
# Reverse order
# list9.sort(reverse=True)
# It should be noted that :; list sort The ranking is based on ASCII 0-9,a-z,A-Z Each has a number ,
# list10 = ['black', 'red', 'white', 'purple']
# list10.sort()
# print(list10)
# temp = [('2', 30), ('1', 37), ('3', 40)]
# Sort by first index
# temp.sort(reverse=True)
# key Parameter uses the second index of each element as the sort basis
# temp.sort(reverse=True, key=lambda x: x[1])
# print(temp)
# 8)copy function
# take list1 Assign a value to list2,list1 change ,list2 It will change too.
# list1 = [1, 2, 3]
# list2 = list1
# list1.append(4)
# print(list1, list2)
# list1 Elements copy To list3 in ,list1 Appending elements does not affect list3
# list3 = list1.copy()
# list1.append(5)
# print(list1, list3)
###########################################################
# 4、 List expression
# list11 = list(range(5))
# Output 10 An even number within
# list11 = [i for i in range(10) if i % 2 == 0]
# Multiply the elements in the list by 3
# list11 = [i * 3 for i in range(10)]
# To be added
# print(list11)


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