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

1、 Python learning notes - basic data types - List

編輯:Python
"""
The list is Python The most basic data structure in . Each element in the sequence is assigned a number - Its location , Or index , The first index is 0, The second index is 1, And so on
"""

Create and delete

# Create a list of
list1 = [1, 2, 'abc']
# Print the content
print(list1)
# Print type
print(type(list1))
# Delete
del list1

Add, delete, change and check the list

""" List query ( section )
1、 Do the following python command , Intercept the list by position , Positive numbers represent numbers from left to right , Negative numbers represent numbers from right to left , Positive numbers start with 0 Start , Negative number from -1 Start ( because 0 Fixed means the first one on the left ), The intercepted content includes the number to the left of the square bracket , The number to the right of the square brackets is not included
"""
list1 = [1, 2, 3, 'abc', 'aaa']
print(list1) # Output list all values
print(list1[0:-1]) # Output everything from the first to the penultimate , The last but one is -1 instead of 0
print(list1[0]) # Output the first character of the list
print(list1[2:4]) # Output the contents of the list from the third to the fourth ,4 The corresponding position is the fifth character , But it will not be intercepted
print(list1[2:]) # Output all characters after the third one
""" increase
1、 Add content to the list
"""
# Add to end
list1.append('abc')
print(list1)
# Add to specified line
list1.insert(0, '0')
print(list1)
""" modify
1、 Similar to query , Just add the assignment operation on the query
"""
# Modify a single value according to the index
list1[4] = 'cba'
print(list1)
# Modify multiple values according to the index
list1[0:2] = [9, 8]
print(list1)
""" Delete
1、 Delete content from list
"""
# Delete by content
list1.remove('aaa')
print(list1)
# Delete... According to index
del list1[2]
print(list1)
# pop, After deleting , The deleted value will be returned to
r = list1.pop(1)
print(list1)
print(r)

Other list operations

# Other list operations
# count
"""
1、 Count the number of times a value appears in the list
"""
list1 = [1, 1, 2, 4]
print(list1.count(1))
# extend
"""
1、 take list3 List values are added to list2
2、list3 unchanged
"""
list2 = [1, 2, 3, 4]
list3 = [5, 6, 7, 8]
list2.extend(list3)
print(list2)
print(list3)
# index
"""
1、 Get a worthy position ( Indexes )
"""
list4 = ['a', 'b', 'c']
print(list4.index('a'))
# reverse
"""
1、 Reverse the order of the list
"""
list5 = [1, 2, 3, 4]
list5.reverse()
print(list5)
# sort
"""
1、 Sort , From small to large
2、 Letters from a To z
3、 Sort in reverse order
"""
list6 = [3, 2, 1, 4]
list6.sort()
print(list6)
list7 = ['c', 'b', 'd', 'a']
list7.sort()
print(list7)
list8 = [3, 2, 1, 4]
list8.sort(reverse=True)
print(list8)

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