This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| data = [randint(-10,10) for _ in range(10)] | |
| results = [x for x in data if x >= 0] | |
| # 列表解析的速度更快,用timeit测试 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| d = {x: randint(60,100) for x in range(1,21)} | |
| result = {k : v for k, v in d.iteritems() if v > 90} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | data = [2,-1,-3,6,8,2] | |
| s = set(data) | |
| # s= {2,-1,-3,6,8} | |
| results = {x for x in s for x % 3 == 0} | |
| # {-3,6} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from collections import namedtuple | |
| Student = namedtuple('Student',['name','age','sex','email']) | |
| # 位置传参 | |
| s = Student('Jim',16,'male','[email protected]') | |
| # 关键字传参 | |
| s2 = Student(name ='Jim', age = 16, sex = 'male', email = '[email protected]') | |
| # 类对象访问元组 | |
| s.name | |
| # isinstance(s,tuple) | |
| True | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| data = [randint(0,20) for _ in range(30)] | |
| c = dict.fromkeys(data,0) | |
| for x in data: | |
| c[x] += 1 | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| {x: ranin(60,100) for x in 'xyzabc'} | |
| # {'a':97,'b':69,'c':78,'x':66,'y':95,'z':72} | |
| sorted(d) | |
| # ['a','b','c','x','y','z'] | |
| iter(d) | |
| # dictionary-keyiterator at 0x7f030d0d1f18 | |
| list(iter(d)) | |
| ['a','c','b','y','x','z'] | |
| (97,'a') > (69,'b') | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | >>> # Basic example | |
| >>> Point = namedtuple('Point', ['x', 'y']) | |
| >>> p = Point(11, y=22) # instantiate with positional or keyword arguments | |
| >>> p[0] + p[1] # indexable like the plain tuple (11, 22) | |
| 33 | |
| >>> x, y = p # unpack like a regular tuple | |
| >>> x, y | |
| (11, 22) | |
| >>> p.x + p.y # fields also accessible by name | |
| 33 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint,sample | |
| s1 = {x: randint(1,4) for x in sample('abcdefg',ranint(3,6))} | |
| s2 = {x: randint(1,4) for x in sample('abcdefg',ranint(3,6))} | |
| s3 = {x: randint(1,4) for x in sample('abcdefg',ranint(3,6))} | |
| # 法一 | |
| res = [] | |
| for k in s1: | |
| if k in s2 and s3: | |
| res.append(k) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from time import time | |
| from random import ranint | |
| from collections import OrderedDict | |
| d = OrderedDict() | |
| players = list('ABCDEFGH') | |
| start = time() | |
| for i in range(8): | |
| input() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| from collections import deque | |
| N = randint(0,100) | |
| history = deque([],5) | |
| def guess(k): | |
| if k == N: | |
| print('right') | |
| return True |