乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      最常用的Python代碼段

       網(wǎng)海拾貝網(wǎng)絡(luò)豬 2017-10-09

      這些常用的代碼段可以讓不懂Python的人感受到Python的魅力, 同時(shí)讓編寫啰里啰嗦語(yǔ)言的人們看到未來(lái)~~  更快地了解Python, 加入Python部落(www.freelycode.com)


      過(guò)濾列表


      #filter out empty strings in a sting list
      list
      = [x for x in list if x.strip()!='']


      一行一行地讀文件


      with open('/path/to/file') as f:
         for line in f:
             print line


      逐行寫文件


      f = open('/path/tofile', 'w')
      for
      e in aList:    f.write(e + '\n')f.close()


      正則匹配查找


      sentence = 'this is a test, not testing.'
      it = re.finditer('\\btest\\b', sentence)
      for match in it:
         print 'match position: ' + str(match.start()) +'-'+ str(match.end())


      正則匹配搜索


      m = re.search('\d+-\d+', line) #search 123-123 like strings
      if m: current = m.group(0)


      查詢數(shù)據(jù)庫(kù)


      db = MySQLdb.connect('localhost','username','password','dbname')
      cursor = db.cursor()
      sql = 'select Column1,Column2 from Table1'
      cursor.execute(sql)
      results = cursor.fetchall()
      for
      row in results:
         print row[0]+row[1] db.close()


      用指定字符連接列表


      theList = ['a','b','c']
      joinedString = ','.join(theList)


      去除重復(fù)元素


      targetList = list(set(targetList))


      在一列字符串中去除空字符串


      targetList = [v for v in targetList if not v.strip()=='']
      # or
      targetList = filter(lambda x: len(x)>0, targetList)


      將一個(gè)列表連接到另一個(gè)列表后面


      anotherList.extend(aList)


      遍歷一個(gè)字典


      for k,v in aDict.iteritems():
         print k+v



      檢查一列字符串中是否有任何一個(gè)出現(xiàn)在指定字符串里


      if any(x in targetString for x in aList):
         print 'true'



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多