Python :數據結構

LearnPython :數據結構javascript

 

 

 

Python 數據結構基礎

Ref: linkcss

My questions:html

  • dictionary -- 字典 :
  • array -- 數組
  • list -- 列表
  • sequence -- 序列?
  • 元組 -- tuple
 

1. dictionary

In [ ]:
 
 

2. sequence

Python有6個序列的內置類型,但最多見的是列表和元組。 Sequence Types -- str, unicode, list, tuple, buffer, xrange refhtml5

  • 序列均可以進行的操做包括*索引,切片,加,乘,檢查成員
  • Python已經內置肯定序列的長度以及肯定最大和最小的元素的方法
 

2.1 list 列表

列表是最經常使用的Python數據類型,它能夠做爲一個方括號內的逗號分隔值出現。 列表的數據項不須要具備相同的類型. 例如:java

In [2]:
#!/usr/bin/python3
 
list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
 
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])
 
list1[0]:  Google
list2[1:5]:  [2, 3, 4, 5]
 

2.2 tuple 元組

refpython

  • Python 的元組與列表list相似,但,元素不可修改jquery

  • 元組使用圓括號(),列表List使用方括號[]。linux

  • 元組建立: 括號中添加元素,用逗號隔開便可。單元素元組tuple 要帶一個 , 逗號。android

實例:css3

In [3]:
#!/usr/bin/python3

t = 12345, 54321, 'hello!'
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )

print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
print ("t:", t)
 
tup1[0]:  Google
tup2[1:5]:  (2, 3, 4, 5)
t: (12345, 54321, 'hello!')
 

2.3 String

字符串是 Python 中最經常使用的數據類型。咱們能夠使用引號('或")來建立字符串。

建立字符串很簡單,只要爲變量分配一個值便可。例如:

var1 = 'Hello World!' var2 = "Runoob"

使用[]方括號來訪問:

In [4]:
#!/usr/bin/python3
 
var1 = 'Hello World!'
var2 = "Runoob"
 
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])
 
var1[0]:  H
var2[1:5]:  unoo
 

2.4 unicode, buffers, xrange

 

3.Array 對象

  • array 是一個模型module,它定義了一個對象

  • 這個對象能夠對基本的值(好比 characters, integers, floating_point)表示成一個array的形式。

  • Arrays 是序列形式的,它的行爲很像是列表list。除了一點不一樣: array裏的對象的類型是被限制constrained的

  • The type is specified at object creation time by using a type code, which is a single character.

Type code 及更多信息: https://docs.python.org/3/library/array.html

 

4.集合

集合是一個無序不重複元素的集。基本功能包括關係測試消除重複元素

能夠用大括號({})建立集合

注意:若是要建立一個空集合,你必須用 set() 而不是 {} ;後者建立一個空的字典,下一節咱們會介紹這個數據結構。

如下是一個簡單的演示:

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket) # 刪除重複的 {'orange', 'banana', 'pear', 'apple'}

In [ ]:
相關文章
相關標籤/搜索