<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      python技巧31[pythonTips1]

       

      1 使用%來格式字符串

      print("hello %s : %s" % ("AAA""you are so nice"))

       

      2 使用zip來將兩個list構造為一個dict

      names = ['jianpx''yue']   
      ages 
      = [2340]   
      = dict(zip(names,ages)) 
      print (m)

       

      3 不使用臨時變量來交換兩個值

      a,b = b,a

       

      4 使用str.join來連接字符串

      fruits = ['apple''banana']   
      result 
      = ''.join(fruits) 

       

      5 使用in dict.keys()來判斷dict中是否包含指定的key

      = {1:"v1"2:"v2"}
      if 1 in d.keys():
        
      print("d dict has the key 1")

       

      6 使用set來去除list中的重復元素

      = [1,2,2,3]
      l2 
      = set(l)
      print(l2)

       

      7 對于in操作,set要快于list,因為set是使用hash來存儲和查找的

       

      8 使用with來讀寫文件,保證file對象的釋放

        with open("myfile.txt") as f:
           
      for line in f:
               
      print (line)
        f.readline() 
      #f is cleanup here, here will get ValueError exception

       

      9 使用emumerate來遍歷list

      = [1,34]
      for index, value in enumerate(l):
          
      print ('%d, %d' % (index, value))

       

      10 分割字符串且去掉空白

      names = 'jianpx, yy, mm, , kk'
      result 
      = [name for name in names.split(','if name.strip()]

       

      11 python中的a?b:c

      return_value = True if a == 1 else False  

       

      12 Zen of python

      >>> import this
      The Zen of Python, by Tim Peters

      Beautiful 
      is better than ugly.
      Explicit 
      is better than implicit.
      Simple 
      is better than complex.
      Complex 
      is better than complicated.
      Flat 
      is better than nested.
      Sparse 
      is better than dense.
      Readability counts.
      Special cases aren
      't special enough to break the rules.
      Although practicality beats purity.
      Errors should never 
      pass silently.
      Unless explicitly silenced.
      In the face of ambiguity, refuse the temptation to guess.
      There should be one
      -- and preferably only one --obvious way to do it.
      Although that way may 
      not be obvious at first unless you're Dutch.
      Now is better than never.
      Although never 
      is often better than *right* now.
      If the implementation 
      is hard to explain, it's a bad idea.
      If the implementation is easy to explain, it may be a good idea.
      Namespaces are one honking great idea 
      -- let's do more of those!
      >>>

       

      13 匿名函數lambda

      add = lambda x,y : x + y
      print(add(1,2))

       

      14 filter(bool_func,seq),在python3以后,filter為類,filter的功能相當于過濾器。調用一個布爾函數bool_func來迭代遍歷每個seq中的元素;返回一個使bool_seq返回值為true的元素的序列。
      等價于(item for item in iterable if function(item)) 如果function不是None;等價于(item for item in iterable if item) 如果函數是None。

      a=[0,1,2,3,4,5,6,7]
      b
      =filter(None, a)
      print (list(b))
      c
      =filter(lambda x : x %2 == 0, a)
      print(list(c))
      d
      =filter(lambda x:x>5, a)
      print (list(d))
      #[1, 2, 3, 4, 5, 6, 7]
      #
      [0, 2, 4, 6]
      #
      [6, 7]

       

      15 map(func,seq1[,seq2...]):在python3以后,map為類,map將函數func作用于給定序列的每個元素,并用一個列表來提供返回值;如果func為None,func表現為身份函數,返回一個含有每個序列中元素集合的n個元組的列表。

      a=[0,1,2,3,4,5,6,7]
      = map(lambda x:x+3, a)
      print(list(m))
      #[3, 4, 5, 6, 7, 8, 9, 10]

       

      16 reduce(func,seq[,init]):在python3以后,reduce一到functools模塊下,func為二元函數,將func作用于seq序列的元素,每次攜帶一對(先前的結果以及下一個序列的元素),連續的將現有的結果和下一個值作用在獲得的隨后的結果上,最后減少我們的序列為一個單一的返回值:如果初始值init給定,第一個比較會是init和第一個序列元素而不是序列的頭兩個元素。

      import functools
      = [1,2,3,4,5]
      = functools.reduce(lambda x,y:x+y,a)
      print(s)
      #15

       

      17 range用來返回一個list

      strings = ['a''b''c''d''e']
      for index in range(len(strings)):
          
      print (index)
      # prints '0 1 2 3 4'

       

      18 all用來檢查list中所有的元素都滿足一定的條件

      numbers = [1,2,3,4,5,6,7,8,9]
      if all(number < 10 for number in numbers):
          
      print ("Success!")
      # Output: 'Success!'

       

      19 any用來檢查list中是否至少由一個元素滿足一定的條件

      numbers = [1,10,100,1000,10000]
      if any(number < 0 for number in numbers):
          
      print ('Success!')
      else:
          
      print('Fail!')
      # Output: 'Fail!'

       

      20 使用set來檢查list是否有重復的元素

      numbers = [1,2,3,3,4,1]
      if len(numbers) == len(set(numbers)):
          
      print ('List is unique!')
      # In this case, doesn't print anything

       

      21 從已有的dict構造新的dict

      emails = {'Dick''bob@example.com''Jane''jane@example.com''Stou''stou@example.net'}
      email_at_dotcom 
      = dict( [name, '.com' in email] for name, email in emails.items() )
      print(email_at_dotcom)
      # email_at_dotcom now is {'Dick': True, 'Jane': True, 'Stou': False}

       

      22 And+or的執行過程

      對于and語句,如果and左邊的是true,and右邊的值將被返回作為and的結果。

      對于or語句,如果or左邊的是false,or將右邊的值將被返回作為or的結果。

      test = True
      # test = False
      result = test and 'Test is True' or 'Test is False'
      print(result)
      # result is now 'Test is True'

       

      23 檢查字符串是否包含子字符串 

      string = 'Hi there' # True example
      #
       string = 'Good bye' # False example
      if string.find('Hi'!= -1:
          
      print ("Success!")

      string 
      = 'Hi there' # True example
      #
       string = 'Good bye' # False example
      if 'Hi' in string:
          
      print ('Success!')

       

      24 從list構造新的list

      numbers = (1,2,3,4,5# Since we're going for efficiency, I'm using a tuple instead of a list ;)
      squares_under_10 = (number*number for number in numbers if number*number < 10)
      # squares_under_10 is now a generator object, from which each successive value can be gotten by calling .next()
      for square in squares_under_10:
          
      print ( square)
          
      # prints '1 4 9'

       

       

      參考:

      http://www.siafoo.net/article/52#id26

      http://jeffxie.blog.chinabyte.com/2010/06/08/10/

      http://jianpx.javaeye.com/blog/736669

       

      完! 

       

      posted @ 2011-01-13 18:50  iTech  閱讀(1469)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 国产精品日日摸夜夜添夜夜添无码 | 狠狠做五月深爱婷婷天天综合 | 先锋影音男人av资源| 中文字幕乱码人妻综合二区三区| 精品国产午夜福利在线观看| 久久精品夜夜夜夜夜久久| 正在播放酒店约少妇高潮| 免费国精产品wnw2544| 亚洲av成人网在线观看| 69精品无人区国产一区| 岛国大片在线免费播放| 国产精品午夜福利精品| 一区二区免费高清观看国产丝瓜 | 精品九九热在线免费视频| 91密桃精品国产91久久| 青青草国产精品日韩欧美| 国产精品亚洲А∨天堂免下载| 一区二区三区精品自拍视频| 在线观看国产午夜福利片 | 特黄少妇60分钟在线观看播放| 亚洲中少妇久久中文字幕| 欧美成人h精品网站| 精品国产乱子伦一区二区三区| 暖暖 免费 高清 日本 在线观看5| 在线观看免费人成视频色9| 国产精品午夜福利清纯露脸| 亚洲a人片在线观看网址| 精品 无码 国产观看| 午夜片神马影院福利| 九九热视频在线观看一区| 韩国无码AV片午夜福利| 起碰免费公开97在线视频| 国模少妇无码一区二区三区| 人妻被猛烈进入中文字幕| 久久青青草原精品国产app| 国精产品999国精产| av永久免费网站在线观看| 国精品午夜福利不卡视频| 尤物国产精品福利在线网| 激情五月日韩中文字幕| 1区2区3区4区产品不卡码网站|