📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • range
  • 列表推导 (List Comprehensions)
  • 字符串 (Strings)
  • 切片 (Slice)
  • 序列函数
  • sum
  • max
  • all & any

Was this helpful?

  1. CS 61A

List

>>> [41,42,43,44]
[41,42,43,44]
>>> [41,42,43,44] + [41,42,43,44]
[41,42,43,44,41,42,43,44]
>>> len([41,42,43,44])
4
>>> 41 in [41,42,43,44]
True
>>> 41 not in [41,42,43,44]
False
>>> [41,42] in [41,42,43,44]
False
>>> for i in [41,42,43,44]:
        print(i)
41
42
43
44
>>> for i,j in [[41,42],[43,44]]:  # 适用于成对出现的序列
        print(i,j)
41 42
43 44

range

range 是连续数字的序列

>>> list(range(-2,2))
[-2, -1, 0, 1]
>>> list(range(2))
[0, 1]
>>> for _ in range(3):   # 用_表示不关心序列里的值
        print("Go bear!")
Go bear!
Go bear!
Go bear!

列表推导 (List Comprehensions)

>>> odds = [1,3,5]
>>> [x+1 for x in odds]
[2,4,6]
>>> [x for x in odds if 25 % x == 0]
[1,5]
>>> [x+1 for x in odds if 25 % x == 0]
[2,6]

字符串 (Strings)

>>> city = 'Berkeley'
>>> len(city)
8
>>> city[3]
'k'
>>> 'here' in "Where's"
True

切片 (Slice)

  • slice 是创建出了新的 list

    >>> odds = [1,3,5,7,9]
    >>> odds[1:3]
    [3, 5]
    >>> odds[:3]
    [1, 3, 5]
    >>> odds[1:]
    [3, 5, 7, 9]
    >>> odds[:]
    [1, 3, 5, 7, 9]

序列函数

sum

sum(iterable, start=0) -> value

  • 返回“开始”值(默认值:0)加上一个可迭代的数字,当可迭代为空时,返回开始值。

>>> sum([[2,3],[4]],[])  # 相当于 [] + [2,3] + [4]
[2, 3, 4]

max

max(...) max(iterable, , key=func) -> value max(arg1, arg2, args, [, key=func]) -> value

>>> max(range(10),key = lambda x: 7-(x-4)*(x-2))  # y = 7-(x-4)*(x-2) 当 y 取最大值时, x是?
3

all & any

all(iterable, /) 如果bool(x)对迭代中的所有x都为真,则返回真。 如果可迭代为空,则返回True。 any(iterable, /) 如果bool(x)对迭代中的有一个x为真,则返回真。 如果可迭代为空,则返回False。

>>> all([2,4])
True
>>> all([2,4,0])
False
>>> all([2,4,[]])
False
>>> all([2,4,[1]])
True
PreviousRecursive FunctionNextNon-Local Assignment

Last updated 4 years ago

Was this helpful?