由于时间精力有限,就不整理了,在此放一些大佬写好的总结吧~
转载自大佬:双斜杠少年 --> python 语法学习小结
以下是本菜鸟练习的笔记..
# <editor-fold desc="一维列表基本操作">
# list
# a = [1, 2, 3, 4, 5]
# a.append(0) # list后面累加
# a.insert(0, 111) # list在指定位置插值
# a.remove(2) # 除去第一次出现的指定值
# print(a[1]) # 打印指定位置值
# print(a[-1]) # -1为最后一位的索引
# print(a[0:3]) # 打印指定位数的列表
# print(a[-3:]) # 后三位到最后
# print(a.index(3)) # 打印值的索引
# print(a.count(2)) # 出现值的次数
# a.sort(reverse=True) #不传参数默认->升序 reverse=True->降序
# print(a) # 排序后会代替之前的list
# </editor-fold>
# <editor-fold desc="多维列表的建立 更多请学习numpy">
# 多维列表
# a = [1, 2, 3, 4, 5]
# muti_dim_a = [[1, 2, 3], # 逗号不能少!
# [2, 3, 4],
# [3, 4, 5]]
# print(a[1])
# print(muti_dim_a[0][1])
# </editor-fold>
# <editor-fold desc="字典">
# 字典 没有顺序的容器
# a_list = [1, 2, 3, 4, 5, 4]
# d = {'apple': 1, 'pear': 2, 'orange': 3}
# d2 = {1: 'a', 'c': 'b'}
# 一个key对应一个值
# print(d['apple'])
# print(a_list[0])
# del d['pear'] # 删除一个元素
# print(d)
# d['b'] = 20 # 增加元素
# print(d)
# d3 = {'apple': [1, 2, 3], 'pear': {1: 3, 3: 'a'}, 'orange': 3} # 字典中的数据可以是任意类型 包括函数
# print(d3['pear'][1])
# </editor-fold>
# <editor-fold desc="导入模块">
# 载入模块
# import time as t # 1.自定义模块名 + as xxx
# print(t.localtime())
# from time import time,localtime # 2.取time中部分功能
# print(localtime())
# print(time())
# from time import * # 3.取time中所有功能
# print(localtime())
# print(time())
# </editor-fold>
# <editor-fold desc="自定义模块">
# 自己的模块(脚本)
# 1.确保自己写的模块(脚本)my.py 和当前文件在同一目录下
# 2.import my 自定义
# 3.调用方法
# else 把my.py放到python/lib目录(python默认外部模块)下,可以直接调用
# </editor-fold>
# <editor-fold desc="continue 和 break">
# continue 和 break
# a = True
# while a:
# b = input('type something')
# if b == '1':
# break # 跳出循环
# else:
# continue # 跳出当前,仍在循环里
# print('仍在循环里')
# print('终止')
# </editor-fold>
# <editor-fold desc="try">
# try
# try:
# file = open('eeee', 'r')
# except Exception as e:
# print('没有文件命名为eeee')
# response = input('你想重新输入文件名吗?')
# if response == 'y':
# file = open('eeee', 'w')
# else:
# pass
# else:
# file = open('eeee', 'w')
# file.write('ssss')
# file.close()
# </editor-fold>
# <editor-fold desc="zip,map,lambda">
# map,zip,lambda
# a = [1, 2, 3] # zip
# b = [4, 5, 6]
# zip(a, b)
# list(zip(a, b))
# for i, j in zip(a, b):
# print(i/2, j/2)
# list(zip(a, a, b))
# fun2 = lambda x,y: x+y # lambda
# print(fun2(2,3))
# def fun1(x,y): # map
# return (x+y)
# print(list(map(fun1, [1], [2])))
# print(list(map(fun1,[1,3], [2,5])))
# </editor-fold>
# <editor-fold desc="copy,deepcopy">
# copy,deepcopy
# import copy
# a = [1, 2, 3]
# b = a # 索引 绑定在同一位置 改a,b也变
# print(id(a))
# print(id(b))
# c = copy.copy(a) # copy 不会同时改变
# print(id(a) == id(c))
# a = [1, 2, [3, 4]]
# d = copy.copy(a)
# print(id(a) == id(d)) # 只是一层不会同时改变
# print(id(a[2]) == id(d[2])) # 深层次的还是会同时改变
# e = copy.deepcopy(a) # 完全的copy,任何东西都不会重复
# print(id(e[2]) == id(a[2]))
# </editor-fold>
# <editor-fold desc="pickle 存放数据, with">
# pickle 存放数据,with
# import pickle
# a_dict = {'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'sad'}}
# file = open('pickle_example.pickle', 'wb')
# pickle.dump(a_dict, file) # 将dict装在到file里
# file = open('pickle_example.pickle', 'rb')
# a_dict1 = pickle.load(file) # 读取pickle中的数据
# file.close()
# print(a_dict1)
# with open('pickle_example.pickle', 'rb') as file: # 用with会自动关闭file
# a_dict1 = pickle.load(file)
# print(a_dict1)
# </editor-fold>
# <editor-fold desc="set功能">
# set功能
# char_list = ['a', 'b', 'c', 'c', 'd', 'd', 'd']
# print(set(char_list)) # 把不同的(不重复的)找出来,但不排序
# sentence = 'Welcome Back to This Tutorial'
# print(set(sentence)) # 把不同的(不重复的)找出来,但不排序
# unique_char = set(char_list)
# unique_char.add('x') # 加单个值
# unique_char.clear() # 清空
# print(unique_char.remove('a')) # 减掉单个值(不存在时会报错)
# print(unique_char.discard('y')) # 减掉单个值(不存在时不会报错)
# print(unique_char)
# set1 = unique_char
# set2 = {'a', 'e', 'i'}
# print(set1.difference(set2)) # set1里面有,set2里面没有的东西
# print(set1.intersection(set2)) # set1里面有,set2里面也有的东西
# </editor-fold>
# <editor-fold desc="正则">
# 正则re
# import re # 导入模块
# 简单Python匹配
# pattern1 = "cat"
# pattern2 = "bird"
# string = "dog runs to cat"
# print(pattern1 in string)
# print(pattern2 in string)
# 简单正则使用
# pattern1 = "cat"
# pattern2 = "bird"
# string = "dog runs to cat"
# print(re.search(pattern1, string))
# print(re.search(pattern2, string))
# 匹配多种可能 使用[]
# ptn = r"r[au]n" # 匹配run 或者ran -》 r[au]n
# print(re.search(ptn, "dog runs to cat"))
# print(re.search(r"r[0-9a-z]n", "dog rans to cat"))
# 特殊种类匹配
# print(re.search(r"r\dn", "run r4n")) # \d数字
# print(re.search(r"r\Dn", "run r4n")) # \D字母
# • \d : 任何数字
# • \D : 不是数字
# • \s : 任何 white space, 如 [\t\n\r\f\v]
# • \S : 不是 white space
# • \w : 任何大小写字母, 数字和 “” [a-zA-Z0-9]
# • \W : 不是 \w
# • \b : 空白字符 (只在某个字的开头或结尾)
# • \B : 空白字符 (不在某个字的开头或结尾)
# • \\ : 匹配 \
# • . : 匹配任何字符 (除了 \n)
# • ^ : 匹配开头
# • $ : 匹配结尾
# • ? : 前面的字符可有可无
# 多行匹配
# string = """
# dogs runs to cat
# I run to dog
# """
# print(re.search(r"^I", string, flags=re.M)) # re.M 多行
# 重复匹配
# 如果我们想让某个规律被重复使用, 在正则里面也是可以实现的, 而且实现的方式还有很多. 具体可以分为这三种:
# • * : 重复零次或多次
# • + : 重复一次或多次
# • {n, m} : 重复 n 至 m 次
# • {n} : 重复 n 次
# group
# match = re.search(r"(\d+), Date: (.+)", "ID: 021523, Date: Feb/12/2017")
# print(match.group()) # 021523, Date: Feb/12/2017
# print(match.group(1)) # 021523
# print(match.group(2)) # Date: Feb/12/2017
# match = re.search(r"(?P<id>\d+), Date: (?P<date>.+)", "ID: 021523, Date: Feb/12/2017")
# print(match.group('id')) # 021523
# print(match.group('date')) # Date: Feb/12/2017
# # findall 寻找所有匹配
# print(re.findall(r"r[ua]n", "run ran ren")) # ['run', 'ran']
#
# # | : or
# print(re.findall(r"(run|ran)", "run ran ren")) # ['run', 'ran']
# sub 替换
# print(re.sub(r"r[au]ns", "catches", "dog runs to cat")) # dog catches to cat
# split 分裂
# print(re.split(r"[,;\.]", "a;b,c.d;e")) # ['a', 'b', 'c', 'd', 'e']
# compile 编译出来再匹配
# compiled_re = re.compile(r"r[ua]n")
# print(compiled_re.search("dog ran to cat")) # <_sre.SRE_Match object; span=(4, 7), match='ran'>
# </editor-fold>