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

[Python knowledge] list initialization

編輯:Python

1、 The basic method .

lst = [1, 2, 3, 4, 5]

2、 Initializing consecutive numbers .

>>> lst = [n for n in range(5, 10)]
>>> print(lst)
[5, 6, 7, 8, 9]

3、 initialization n Same value .( Two ways )

>>> lst = ['x' for n in range(5)]
>>> print(lst)
['x', 'x', 'x', 'x', 'x']
>>> lst = ['z']*5
>>> print(lst)
['z', 'z', 'z', 'z', 'z']
>>> lst = [0]*3
>>> print(lst)
[0, 0, 0]

4、Python Dictionary of four data types 、 aggregate 、 list 、 Tuples , Use curly brackets 、 brackets 、 Parentheses indicate . Such as :

 Dictionaries :dic={'a':12, 'b':34}
aggregate :s = {1, 2, 3, 4}
list :li=[1, 2, 3, 3]
Tuples :tup=(1, 2, 3, 4) # Tuples are immutable lists 

5、 Orderly dictionary ( collections.OrderedDict)

from collections import OrderedDict
>>> OrderedDict([('b', 2), ('a', 1)])

 Python 3.6 in dict Realized.  PEP 468, So that the default dictionary can be kept in order , However, the function of saving order is considered not to be too dependent , The future may change .


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