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

Python list

編輯:Python

Python list

List with [] Express , List is a value , This value can be transferred to the function

1. Access the value of the list through the list subscript

a = ['1','2',3,4,['a','b']]
print(a[0])
print(a[1])
print(a[2])
print(a[3])
print(a[4])

2. Negative subscript

a = ['1','2',3,4,['a','b']]
print(a[-1],a[-2],a[-3],a[-4],a[-5])

3. section

Note that the last one does not contain

a = ['1','2',3,4,['a','b']]
print(a[1:])
print(a[1:4])

5.len To obtain the length of the

a = ['1','2',3,4,['a','b']]
print(len(a))

6. Modified value

a = ['1','2',3,4,['a','b']]
a[1] = 2
print(a[1:4])

7. Connection and replication of lists

a = ['1','2',3,4,['a','b']]
a = a + [5,6]
print(a)

8.del Statement to delete from the list

a = ['1','2',3,4,['a','b']]
del a[1]
print(a)

9. List and for Combination of cycles

a = [0,1,2,3]
for i in a :
print(i)

10. Common ways of listing

  • Returns the subscript index()

    If multiple values appear , The first subscript is returned

a = [0,1,2,3]
print(a.index(0))
print(a.index(1))
print(a.index(2))
print(a.index(3))
  • append() and insert()
a = [0,1,2,3]
a.append(5)
a.append(6)
print(a)

insert() Support arbitrary position insertion

a = [0,1,2,3]
a.insert('5',4)
a.insert('6',5)
print(a)
  • remove

remove If you want to remove elements from the list , An element appears many times , Only the first

a = [0,1,2,3,1]
a.remove(1)
print(a)
  • sort

sort Sort , Can order , It can also be in reverse order

Don't sort lists with both numbers and strings

The order

a = ['q','w','w','w','r','t']
# according to ASCLL Sort code
a.sort()
print(a)

The reverse

a = ['q','w','w','w','r','t']
a.sort(reverse = True)
print(a)

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