Python学习笔记

零碎的Python学习笔记,不定时更新。

支持utf-8

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

删除list里的重复条目

myList = list(set(myList))

从当前行退出,不再执行后面的命令

sys.exit("Error Message")
sys.exit("Usage: program.py <argement 1>")

Print文件里的某一行

with open(MYFILE) as f:
    for i, line in enumerate(f):
        line = line.rstrip() # remove tailing '\n'
        line = line.split("\t")
        if i == 1:
            print line
        if line[0] == "MYSTRING":
            print line

将文件读入Dictionary

d = {}
with open(MYFILE) as f:
    for line in f:
       line = line.rstrip() # remove tailing '\n' 
       (key, val) = line.split("\t")
       d[key] = val

for i in d:
    print i, d[i]

读取网面内容

import urllib2

user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3'
headers = { 'User-Agent' : user_agent }
request = urllib2.Request("https://url.com", None, headers)
response = urllib2.urlopen(request)
page_content = response.read()
response.close()

Non-greedy match: “?”

Reverse a string

string[::-1]

Reverse a list

list.reverse()
list

Sort a list

print list.sort()
sorted(list)

将string中的值分别赋予给不同的变量

string = "abc,123"
a,b = string.split(',')

将list中的值分别赋予给不同的变量

list = [1, 2, 3]
a, b, c = list

使用空变量_

list = [1, 2, 3, 5, 8, 0]
a, _, c, d, _ = list [1:] # d = 8

将list中的值组合成一个string

list = ["a","b","c"]
print " ".join(list) # 'a b c'
d = "".join(list) # 'abc'
e = "".join((list1, list2, list3)) # needs two ()

执行完后休眠一定时间 (1s - 10s)

from random import randint
from time import sleep

sleep(randint(1,10))

string查找

a = "abc 123"
print a[a.find(' '):] # ' 123'
print a[a.find(' '):].strip() # '123'

print

print '\t is the tab' # '	is the tab'
print r'\t is the tab' # print raw: '\t is the tab'

shared reference or equality

list1 = list2 
list1 is list2 # True
list1 == list2 # True
list1 = list(list2) # or list1 = list2[:]
list1 is list2 # False
list1 == list2 # True

dictionary操作

dict = {'a': 'air', 'b': 'berlin', 'c': 'china'}
dict['c'] # 'china'
dict['d'] = 'danmark' # insert 'd' 
dict.get('e', 'Unknown') # 'Unknown'
dict_dup = dict(dict.items()) # duplicate dict

变两个list为一个dictionary

new_dict = dict(zip(list1, list2)) # create a dict from two lists

从dictionary中提取key, value到list

keyList = []
valList = []
for key, val in dict.items():
    keyList.append(key)
    valList.append(val)
print '{},{},{},{}'.format(*valList)

在print中用format

print '{:d},{:.2f},{}'.format(arg1, arg2, arg3)

将变量赋予两个list中的对应值 zip

combList = zip(list1, list2)
for var1, var2 in combList:
    return var1 + var2

注意 *args 和 **kwargs 的用法

Lambda Expression 用法

temp_converter = {'c_to_f': lambda ctof: ctof * 9.0 / 5 + 32,
                  'f_to_c': lambda ftoc: (ftoc - 32) * 5.0 / 9}
temp_c = 25
print temp_converter['c_to_f'](temp_c)
print temp_converter['f_to_c'](77)

netto = lambda price, tax: price * (1 - tax)
netto(99, 0.1) # 89.10000000000001

使用 namedtuple

from collections import namedtuple

Flight = namedtuple("flightDetails", "departCity, arrivalCity, departTime, departDate,\
                    arriveTime, arriveDate, cost, time")
myFlight = Flight('HNL', 'HKG', '4:00', 'Monday', '8:00', 'Monday', 99.95, 4)
print myFlight 
print myFlight.cost # 99.95

Generator Function

  • use yield
def myRange(end):
    num =0
    while num < end:
        yield num
        num += 1
print myRange(9).next()

for num in myRange(9):
    print num
  • turn list into tumple ()
city_code_dict = {
    'HNL': 'Honolulu',
    'ITO': 'Hilo',
    'LHR': 'London/Heathrow',
    'ARN': 'Stockholm/Arlanda',
    'HKG': 'Hong Kong',
    'YYZ': 'Toronto',
    'CDG': 'Paris/Charles de Gaulle',
    'NRT': 'Tokyo/Narita',
    'GCM': 'Grand Cayman BWI',
    'CUR': 'Curacao Netherland Antilles' }

codelist = ['HNL', 'ITO', 'LHR', 'LGA', 'GCM', 'MSY']

codes1 = (code for code in codelist if code in city_code_dict)
for code in codes1:
    print code

codes2=[code for code in codelist if code not in city_code_dict]
print codes2

print set(codelist) - set(city_code_dict.keys())

Class 建立

class BankAccount(object):
    def __init__(self, name = None, balance = 99, type = None):
        self.name = name
        self.balance = balance
        self.type = type
## function inside class
    def cost(self, amount):
        self.balance -= amount

myAccount = BankAccount(name= 'A', balance=399, type='saving')

print myAccount.type # saving
myAccount.cost(33) 
print myAccount.balance # 366

myAccount.year = 5 # add another attribute 
print myAccount.__dict__  # {'balance': 366, 'type': 'saving', 'name': 'A', 'year': 5}

Print tailing comma

for num in range(1,4):
    print num
    print num, # 1 2 3

Pickle to store and re-create dictionaries

import pickle
with open('outfile.pkl', 'wb') as outfile:
    pickle.dump(your_dict, outfile)

new_dict = {}
with open('outfile.pkl', 'rb') as infile:
    new_dict = pickle.load(infile) 

Import module and use its function

import math
math.log(2)

import math as mh
mh.log(2)

from math import *
log(2)

Check module functions and help

dir(math)
help(doc)
help(math.log)
math.__doc__

Install Miniconda via bash script doesnot need admin

And you can install Python3 from it

conda list
conda install --name <name> 
pip install <name>

list from multiple range

seq = list(range(1,5)) + list(range(9,15)) + [64,] # [1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 64]

String functions

print("-".join["a", "b", "c"])
print("This is me".replace("me", "you"))
print("This is me".startWith("This"))
print("This is me".endWith("you."))
print("This is me".upper()) # lower()
print("a,b,c".split(","))

Anoonymous function

def my_func(f, arg):
    return f(arg)

my_func(lambda x: 2*x*x, 5)

map & filter

numbs = [11,22,33,44,55]

results = list(map(lambda x: x+5, numbs))
results = list(filter(lambda x: x%2 == 0, numbs))

Recursion

def factorial(x):
    if x == 1:
        return 1
    else:
        return x* factorial(x-1)

print(factorial(5))

itertools

from itertools import count / cycle / repeat / takewhile / chain / accumulate / product / permutation

numbs = list(accumulate(range(8)))
print(list(takewhile(lambda x: x<=6, numbs)))

Named / non-capturing groups

Named group: (?P<name>...)
non-capturing group (?:...)
pattern = r"(?P<first>abc)(?:def)(ghi)"

Ternary Operator (conditional expression)

a = 7
b = 1 if a >=5 else 40

main

def function():
    print("This is a module function")

if __name__ == "__main__":
    print("This is a script") # won't run when imported

Packaging

ParentDirectory/
  LICENSE.txt
  README.txt
  setup.py
  package/
    __init__.py # can be blank
    script1.py
    script2.py
    ...
Z. Lu avatar
Z. Lu
Data science, bioinfo, scripting, parasites, retro, plain text.
comments powered by Disqus