python之資料結構

來源:互聯網
上載者:User

標籤:

 

List:list.append(x)list.extend(lis)list.insert(i, x)list.remove(x)list.pop(i)  #會同時返回移除的值,如果沒有設定i,則返回最後一個值del list[i]   del list[i:j]list.index(x)#返回x第一次出現的位置list.count(x)#返回x出現的次數list.sort()  #sorted()  見blog sort進階用法list.reverse()#list快速建立:lis = [x**2 for x in range(10)][(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]-----------------------------------[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]][[row[i] for row in matrix] for i in range(4)]------------------------------------[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]zip:>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = zip(x, y)>>> zipped[(1, 4), (2, 5), (3, 6)]>>> x2, y2 = zip(*zipped)>>> x == list(x2) and y == list(y2)True
Dictionary:>>> a = dict(one=1, two=2, three=3)>>> b = {‘one‘: 1, ‘two‘: 2, ‘three‘: 3}>>> c = dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3]))>>> d = dict([(‘two‘, 2), (‘one‘, 1), (‘three‘, 3)])>>> e = dict({‘three‘: 3, ‘one‘: 1, ‘two‘: 2})>>> a == b == c == d == eTruedel d[key]dic.clear()   #Remove all items from the dictionary.dic.copy()    #Return a shallow copy of the dictionarydic.get(key[, default])   #f default is not given, it defaults to Nonedic.pop(key[, default])   #If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.dic.popitem()     #Remove and return an arbitrary (key, value) pair from the dictionary.dic.items()    #Return a copy of the dictionary’s list of (key, value) pairs.dic.keys()     #Return a copy of the dictionary’s list of keysdic.values()   #eturn a copy of the dictionary’s list of valuesdic.iteritems() dic.iterkeys() dic.itervalues()
set:python的set和其他語言類似, 是一個無序不重複元素集。準系統包括關係測試和消除重複元素. 集合對象還支援union(合), intersection(交), difference(差)和sysmmetric difference(對稱差集)等數學運算.    sets 支援 x in set, len(set),和 for x in set。作為一個無序的集合,sets不記錄元素位置或者插入點。因此,sets不支援 indexing, slicing, 或其它類序列(sequence-like)的操作。 s = set([3,5,9,10])      #建立一個數值集合  t = set("Hello")         #建立一個唯一字元的集合 ------------ set([‘H‘, ‘e‘, ‘l‘, ‘o‘])a = t | s          # t 和 s的並集  b = t & s          # t 和 s的交集  c = t – s          # 求差集(項在t中,但不在s中)  d = t ^ s          # 對稱差集(項在t或s中,但不會同時出現在二者中)用set去除列表中重複元素>>> [i for i in set([11,22,33,44,11,22])][33, 11, 44, 22]  

 

python之資料結構

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.