[PY] [Lc]python's Card knowledge points

Source: Internet
Author: User

Module collections-collections.namedtuple means that the tuple is not clear

If it represents a coordinate, T = (up), it is unclear.
If that's the case, 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)
Methods of the class: GetItemTo manipulate an instance as a 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
Class method: The __len__ method is used to measure the length of an instance of a class
class A:    arr = [1, 2, 3] #返回他的长度    def __len__(self):#用来丈量实例长度的        return len(self.arr)a = A()print(len(a))# 3
Some small knowledge points
- 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']
Comprehensive small Example
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's Card knowledge points

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.