hfj1975 发表于 2015-11-30 11:13:05

随学随用的python-note

  Python笔记---------------[]
  
  
  列表解析
  >>> [(x,y) for x in range(3) for y in range(5)]
  [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]
  生成器表达式(每次只加载一个元素到内存里)
  返回可迭代对象
  
  多重继承
  (新式类采用广度优先,经典类采用深度优先)
  s = (1,2,)
  i = s#前拷贝id(i) == id(s) s的改变会影响i
  i = copy.deepcopy(s)深拷贝
  >>> d = [(x,y,z,a,v,c) for x in range(9) for y in range(9) for z in range(9) for a in range(9) for v in range(9) for c in range(9)] 暴力破解
  Isinstance(s, str)判断s这个对象是不是’str’object
  
  For i in range(10:
  If i == 2:
  Continue
  Print i
  
  Class Girl(human):
  Def __init__(self, name, old):
  Print ‘We are People’, name
  Switch = {
  ‘-’:a-b,
  ‘+’:a+b
  }
  Print Switch.get(c,’Please input +-*/’)#用字典的get这样不报错
  
  >>> hello[:3:-1]
  'dlrow o'
  >>> hello
  'lo worl'
  >>> hello
  'o world'
  >>>
  
  S = 'abcdefghijk'
  for i in range(0,len(S),2):
  print S   #小技巧
  --------------------------------------------
  L1 =
  L2 =
  返回L1中的数值这个数值必须是L2中没有的。
  Set(L1).difference(L2)   --------------------------------------------------------
  
enumerate()
利用enumerate()函数,可以在每次循环中同时得到下标和元素:
S = 'abcdefghijk'
for (index,char) in enumerate(S):    print index    print char
实际上,enumerate()在每次循环中,返回的是一个包含两个元素的定值表(tuple),两个元素分别赋予index和char。
  with open(file) as myfile:
  Print myfile.read() #不用再关闭myfile
  ------------------------------------------------
  class num(object):
  def __init__(self, value):
  self.value = value
  def getNeg(self):
  return -self.value
  def setNeg(self, value):
  self.value = -value
  def delNeg(self):
  print("value also deleted")
  del self.value
  neg = property(getNeg, setNeg, delNeg, "I'm negative")
  
  x = num(1.1)
  print(x.neg) x.neg = -22
  print(x.value)
  print(num.neg.__doc__)
  del x.neg
  装饰器可以对一个函数、方法或者类进行加工。
  def decorator(F):
  def new_F(a, b):
  print("input", a, b)
  return F(a, b)
  return new_F
  # get square sum
  @decorator
  def square_sum(a, b):
  return a**2 + b**2#square_sum = decorator(square_sum)
  Def func(formal_args, i = 0, *args, **kwargs):
  print formal_args
  print i
  print args
  print kwargs
  以上是不带参数的装饰器
  def Func1(F):
  def new_fun():
  print('func1')
  F()
  return new_fun
  
  def Func21(F):
  def new_fun2():
  print('func2')
  F()
  return new_fun2
  @Func1
  @Func21
  def Func4():
  pass
  
  print(0b1110) # 二进制,以0b开头
  print(0o10) # 八进制,以0o开头
  print(0x2A) # 十六进制,以0x开头
  环境配置$export PYTHONPATH=$PYTHONPATH:/home/vamei/mylib
  
安装非标准包
Python的标准库随着Python一起安装。当我们需要非标准包时,就要先安装。
(1)、使用Linux repository (Linux环境)
这是安装Python附加包的一个好的起点。你可以在Linux repository中查找可能存在的Python包 (比如在Ubuntu Software Center中搜索matplot)。
(2)、使用pip。pip是Python自带的包管理程序,它连接Python repository,并查找其中可能存在的包。
比如使用如下方法来安装、卸载或者升级web.py:
$pip install -i http://mirrors.aliyuncs.com/pypi/simple web.py$pip uninstall web.py$pip install -i http://mirrors.aliyuncs.com/pypi/simple --upgrade web.py
如果你的Python安装在一个非标准的路径(使用$which python来确认python可执行文件的路径)中,比如/home/vamei/util/python/bin中,你可以使用下面方法设置pip的安装包的路径:
$pip install -i http://mirrors.aliyuncs.com/pypi/simple --install-option="--prefix=/home/vamei/util/" web.py

str1 = ['21+23=', '32/8=', '32*12=']                                                   
e=[]
for s in str1:
    s2 = s + str(eval(s[:-1]))
    e.append(s2)
print(e)
页: [1]
查看完整版本: 随学随用的python-note