zoukankan      html  css  js  c++  java
  • ScipyLectures-simple学习笔记

    Chapter 1

    • 1.4.3 中的常用 magic function。

    Chapter 2

    • 字符串复制
    >>> 2*b
    'hellohello' 
    
    • 类型转换
    >>> float(1)
    1.0 
    
    • 注意 整数除法 Python2 和Python3 的差别
    # Python 2
    >>> 3 / 2
    1 
    # Python 3
    >>> 3 / 2
    1.5 
    
    • Python3中除法实现Python2 中类似的效果
    >>> 3.0 // 2
    1.0 
    
    • Future behavior
    # 在Python2中实现Python3类似的效果
    >>> from __future__ import division
    >>> 3 / 2
    1.5 
    
    • list中的负数索引
    >>> colors = ['red', 'blue', 'green', 'black', 'white'] 
    >>> colors[-1]
    'white'
    >>> colors[-2]
    'black' 
    
    • 切片 注意 colors[start:stop] 中包含的元素索引是 start<= i **< stop ** , 共stop-start个元素。
    >>> colors[2:4]
    ['green', 'black'] 
    >>> colors[3:]
    ['black', 'white']
    >>> colors[:3]
    ['red', 'blue', 'green']
    >>> colors[::2]
    ['red', 'green', 'white'] 
    
    • list 是可变类型
    >>> colors[0] = 'yellow'
    >>> colors
    ['yellow', 'blue', 'green', 'black', 'white']
    >>> colors[2:4] = ['gray', 'purple']
    >>> colors
    ['yellow', 'blue', 'gray', 'purple', 'white'] 
    
    • list可以有不同类型的元素
    >>> colors = [3, -200, 'hello']
    >>> colors
    [3, -200, 'hello']
    >>> colors[1], colors[2]
    (-200, 'hello') 
    
    >>> colors = ['red', 'blue', 'green', 'black', 'white']
    >>> colors.append('pink')
    >>> colors
    ['red', 'blue', 'green', 'black', 'white', 'pink']
    >>> colors.pop() # removes and returns the last item
    'pink'
    >>> colors
    ['red', 'blue', 'green', 'black', 'white']
    >>> colors.extend(['pink', 'purple']) # extend colors, in-place
    >>> colors
    ['red', 'blue', 'green', 'black', 'white', 'pink', 'purple']
    >>> colors = colors[:-2]
    >>> colors
    ['red', 'blue', 'green', 'black', 'white'] 
    
      • 反转
        list(list_object) 复制list_object对象
    1. 通过切片实现
    2. 调用reverse
    >>> rcolors = colors[::-1]
    >>> rcolors
    ['white', 'black', 'green', 'blue', 'red']
    >>> rcolors2 = list(colors)
    >>> rcolors2
    ['red', 'blue', 'green', 'black', 'white']
    >>> rcolors2.reverse() # in-place
    >>> rcolors2
    ['white', 'black', 'green', 'blue', 'red'] 
    
      • 连接与复制
    >>> rcolors + colors
    ['white', 'black', 'green', 'blue', 'red', 'red', 'blue', 'green', 'black', 'white']
    >>> rcolors * 2
    ['white', 'black', 'green', 'blue', 'red', 'white', 'black', 'green', 'blue', 'red'] 
    
      • 排序
    1. sorted产生新对象,list的内容未变
    2. list.sort 替换原来的list, list的内容已变
    >>> sorted(rcolors) # new object
    ['black', 'blue', 'green', 'red', 'white']
    >>> rcolors
    ['white', 'black', 'green', 'blue', 'red']
    >>> rcolors.sort() # in-place
    >>> rcolors
    ['black', 'blue', 'green', 'red', 'white'] 
    
    • 字符串
    s = """Hi,
    what's up?""" 
    
    • 字符串 是类似list的容器,可以索引和切片,规则与语法与list是一样的
    >>> a = "hello, world!"
    >>> a[3:6] # 3rd to 6th (excluded) elements: elements 3, 4, 5
    'lo,'
    >>> a[2:10:2] # Syntax: a[start:stop:step]
    'lo o' 
    
    • 字符串 不可变 ,需要通过创建新字符串的方式实现。
    In [53]: a = "hello, world!"
    In [54]: a[2] = 'z'
    ---------------------------------------------------------------------------
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    In [55]: a.replace('l', 'z', 1)
    Out[55]: 'hezlo, world!'
    In [56]: a.replace('l', 'z')
    Out[56]: 'hezzo, worzd!' 
    
    >>> d = {'a':1, 'b':2, 3:'hello'}
    >>> d
    {'a': 1, 3: 'hello', 'b': 2} 
    
    • 元组 不可变
    • 元组可以不写括号
    >>> t = 12345, 54321, 'hello!'
    >>> t[0]
    12345
    >>> t
    (12345, 54321, 'hello!') 
    
    • set 无序元素唯一
    • 注意定义,不是一般的一对括号,而是两对括号
    >>> s = set(('a', 'b', 'c', 'a'))
    >>> s
    set(['a', 'c', 'b']) 
    
    • set.difference()
    >>> s.difference(('a', 'b'))
    set(['c']) 
    
    • 赋值
      • 一个变量,多个绑定
    In [1]: a = [1, 2, 3]
    In [2]: b = a
    In [3]: a
    Out[3]: [1, 2, 3]
    In [4]: b
    Out[4]: [1, 2, 3]
    In [5]: a is b
    Out[5]: True 
    In [6]: b[1] = 'hi!'
    In [7]: a
    Out[7]: [1, 'hi!', 3] 
    
    • ** is ** 两边是否是同一对象
    >>> 1 is 1.
    False
    >>> a = 1
    >>> b = 1
    >>> a is b
    True 
    
    • ** in ** 元素是否在集合中
    >>> b = [1, 2, 3]
    >>> 2 in b
    True
    >>> 5 in b
    False 
    
    • enumeration 在for循环中生成索引以及对应的item
    >>> words = ('cool', 'powerful', 'readable') 
    >>> for index, item in enumerate(words):
    ... print((index, item))
    (0, 'cool')
    (1, 'powerful')
    (2, 'readable') 
    

    代码

  • 相关阅读:
    返回一个随机数组中的子数组中的数相加最大的和
    四则运算二之结果
    四则运算二
    UVA 11741 Ignore the Blocks
    UVA 1408 Flight Control
    UVA 10572 Black & White
    CF1138D(545,div2) Camp Schedule
    UVA 1214 Manhattan Wiring
    UVA 11270 Tiling Dominoes
    BZOJ 3261 最大异或和
  • 原文地址:https://www.cnblogs.com/DataNerd/p/7988835.html
Copyright © 2011-2022 走看看