Python入门2

又到了中文+Python基础的时刻了,多开心多轻松的时刻,It's show time! 伴随着这首刺破耳膜的歌,指尖起舞。。。(---你是在弹Piano吗? ---我在编程 ---She精病)

  1. 推导式
  2. 装饰器
  3. 字典
  4. Try
  5. OS
  6. 正则表达式及re包的运用
  7. 文档输入与输出
  8. Homework

推导式

[i**2 for i in range(10)]#也可以用for啥啥,啥啥啥循环,但这个更优雅和快速

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

装饰器-不改变原函数的情况下更新输出

def mse_box(list_input):
    """
    input: list
    output: int, mean square error of the list
    """
    mean_list = sum(list_input)/len(list_input)
    list_square_error = [(i-mean_list)**2 for i in list_input]
    mean_square_error = sum(list_square_error)/len(list_square_error)
    return(mean_square_error)

mse_box([1, 2, 3, 2])

0.5

放出装饰器看看

def outer(func):#参数为函数func,接下来func会放mse_box()
    def decorate(*args, **kwargs):#*args, **kwargs代表可以接收各类参数了
        print("Input list is: {0}".format(*args))
        input0 = list(*args)#不能直接print(*args),要加list()
        print("Function name is: {}, output is:".format(func.__name__))
        print (func(*args, **kwargs))

        mse0 = func(*args, **kwargs)
        print("make adjustment to {0}:".format(func.__name__))
        print("each element of input decreases mse, and the new output is: ")
        print([i-mse0 for i in input0])#如果要输出该部分,加个return()即可
    return decorate

@outer
def mse_box(list_input):
    """
    input: list
    output: int, mean square error of the list
    """
    mean_list = sum(list_input)/len(list_input)
    list_square_error = [(i-mean_list)**2 for i in list_input]
    mean_square_error = sum(list_square_error)/len(list_square_error)
    return(mean_square_error)

mse_box([1, 2, 3, 2])

装饰器的作用:此例中,为了修改mse_box函数,单独在外面将mse_box该func及其参数拎出来,在外头修改。
单独修改的好处:假如第二个模块引用了mse_box,且该模块只需要计算均方差,不需要进行任何改动,那装饰器就make a difference了。

Dict

keys = ["a", "b", "c"]
value = [1, 2, 3]

dict(zip(keys, value))#用dict嵌套zip可以比较快地生成字典

{'a': 1, 'b': 2, 'c': 3}

同样地,用推导式将数据转换成字典比较方便,但有些费脑

raw_data = "水果:柿子,蔬菜:西红柿,其他:灯笼"

{a:b for a,b in (j.split(":") for j in [i for i in raw_data.split(",")])}

{'其他': '灯笼', '水果': '柿子', '蔬菜': '西红柿'}

try_except

使用try可以避免报错就中断整个程序,使用except来发现是不是我们注释的地方中断了,方便找错

try:
    print(raw_data[100])
except:
    print("If you see this, raw_data doesn't have length as long as 100")

try:
    print(raw_data[:2] == "水果")
except:
    print("If you see this, first two words of raw_data are not 水果")

try:
    print(raw_data+1)
except:
    print("If you see this, raw_data can't add number")

If you see this, raw_data doesn't have length as long as 100
True
If you see this, raw_data can't add number

OS文档操作

import os#自带包,不需要额外pip install xxx
#不清楚os有什么功能,输入os.摁TAB吧
os.getcwd()# get current working directory

os.listdir()[-5:]#list directory of current working directory,输出格式为list

#查看所有Jupyter notebook格式文档
[file for file in os.listdir() if file[-5:] == 'ipynb']#推导式,怎么又是你

# 改变目前工作路径
os.chdir("/code/python_data_analysis")

正则表达式及re包的运用

Ans:

  • [c,m,f]an: can, man, fan; [^b]og to skip bog
  • [^a-z]\w+ skip lower case begined string; \w means [A-Za-z0-9]; \d means [0-9]
  • z{3} match z three times: uzzz, wuzzzz; .{2,6} match string with length of 2-6
  • ? match ?
  • whitespace: space (␣), the tab (\t), the new line (\n) and the carriage return (\r)
  • \s will match any of the specific whitespaces above
  • \D represents any non-digit character, \S any non-whitespace character, and \W any non-alphanumeric
  • ^Mission: successful$ ^为字符串开始 and $为字符串结尾
  • ^(file_\w+) can match file_record_transcript in file_record_transcript.pdf
  • ^([A-Z]\w{2} (\d{4})) 括号中为提取的信息,此处不但提取Jan 1987,还提取1987
  • ^I love cats|I love dogs$ match "I love cats"或"I love dogs"
  • ^The.* match string starting with "The"

正则表达式的练习在线网址: https://regexone.com/

触发支线任务--爬虫
image

dir_name = os.listdir()
dir_name[:5]

import re
for i in [re.findall("csv_\d{1,2}\.csv", i) for i in dir_name]:
    if i != []:
        print(i[0])

os.rename("Untitled Folder 1", "New Folder 1")#可以重命名

文档输入与输出

import pandas as pd#经常用到的一个包,装了anaconda就自带
df1 = pd.read_csv("csv1.csv")#这个文档自己随意创建的
#也可以试试pd.read_excel("表格1.xlsx"); 输入pd.read_摁TAB可以查看其他输入
df1.head()

df1_head = df1.head()
df1_head.to_csv("csv1_top5.csv")#输出到文档,也可以试试df_data.to_excel("target.xlsxa)
#设置index=False可以不打印额外的index

#接着,制造一些数据留给你做Homework吧
for i in range(10):
    df = df1.head(i+1)
    df.to_csv("csv_{}.csv".format(i+1))#也可以试试输出为excel格式(.xlsx)

Homework

背景:有没有遇到这样傻逼又无奈的事情:打开一堆Excel,然后把指定位置的数据摘出来放个新的表格。。。考验你百度(Google)的能力了

例如,将csv_n.csv打开(n为1到10),然后分别要"csv_n.csv"的第n行数据

For Reference

posted @ 2019-04-28 16:59  水云疏柳  阅读(151)  评论(0编辑  收藏  举报