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

[learn Python from simple to deep] list

編輯:Python

One 、 Create a list of

Create a list , Just use different data items separated by commas square brackets [ ] Just round it up .
The data items of a list do not need to have the same type .

list1 = ["qing", "ping", 27]
list2 = [1, 2, 3, 4]
list3 = ["a", "b", "c"]

Two 、 Common operations

L1=[10,20,"PYTHON"]
print(L1)

[10, 20, ‘PYTHON’]

1. Slice marker

# Slice marking method 
print(L1[2])

PYTHON

print(L1[1:3])# Less than 3

[20, ‘PYTHON’]

# Negative index 
print(L1[-1])

PYTHON

# List length 
print(len(L1))

3

# List splicing 
L2=[10,90,"java"]
print(L1+L2)

[10, 20, ‘PYTHON’, 10, 90, ‘java’]

#append Finally, add the element 
L2.append("docker")
print(L2)

[10, 90, ‘java’, ‘docker’, ‘docker’, ‘docker’]

#apend And for Loop to create a list 
list1=[]
for i in range (1,10):
list1.append(i)
print (list1)

[1, 2, 3, 4, 5, 6, 7, 8, 9]

#del Delete an element 
del L2[3]
print(L2)

[10, 90, ‘java’, ‘docker’]

#pop eject 
L2.pop()
print(L2)

[10, 90, ‘java’]

# Specify an element to pop up 
L2.pop(0)
print(L2)

[90, ‘java’]

#sort Sort From small to large 
L3=[7,2,3,78]
L3.sort()
print(L3)

[2, 3, 7, 78]

#sorted Temporary sort 
L3=[10,3,4,6,7,2]
print(sorted(L3))
print(L3)

[2, 3, 4, 6, 7, 10]
[10, 3, 4, 6, 7, 2]

# Descending order 
L3=[7,2,3,78]
L3.sort(reverse=True)
print(L3)

[78, 7, 3, 2]

#count Count the number of times an element appears 
L3.count(7)

1


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