[py][lc]python的紙牌知識點

來源:互聯網
上載者:User

標籤:知識點   cti   pre   int   sel   結構   list   name   tuple   

模組collections-collections.namedtuple表示tuple欲言不清

如表示一個座標, t = (1,2), 搞不清楚.
如果這樣就對了Point(x=1, y=2)

from collections import namedtuplePoint = namedtuple('Point', ['x', 'y']) #做一些kv形式的易辨別的資料結構p = Point(1, 2)print(p)# Point(x=1, y=2)
類的方法: getitem 把執行個體當list來操作
class Student:    arr = [0, 1, 2, 3, 4] #返回arr的第3項    def __getitem__(self, n):        return self.arr[n]s = Student()print(s[3])# 3
類的方法: __len__方法用於丈量類的執行個體的長度
class A:    arr = [1, 2, 3] #返回他的長度    def __len__(self):#用來丈量執行個體長度的        return len(self.arr)a = A()print(len(a))# 3
一些小知識點
- str轉list>>> list('JQKA')['J', 'Q', 'K', 'A']- 產生str類型的數字序列>>> [str(n) for n in range(2,11)]['2', '3', '4', '5', '6', '7', '8', '9', '10']
綜合小例子
from collections import namedtuplePoint = namedtuple('Point', ['x', 'y'])p = Point(1, 2)print(p)arr1 = [str(i) for i in range(10)]arr2 = [str(i) for i in range(10)]s = [Point(k1,k2) for k1 in arr1 for k2 in arr2]print(s)# Point(x=1, y=2)# [Point(x='0', y='0'), Point(x='0', y='1'), Point(x='0', y='2'), Point(x='1', y='0'), Point(x='1', y='1'), Point(x='1', y='2'), Point(x='2', y='0'), Point(x='2', y='1'), Point(x='2', y='2')]

[py][lc]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.