目录
Python2向文件中写入Unicode字符
def dict2list(dic:dict): ‘’’ 将字典转化为列表 ‘’’ keys = dic.keys() vals = dic.values() lst = [(key, val) for key, val in zip(keys, vals)] return lst
Python获取指定文件夹下的文件名 http://blog.csdn.net/lsq2902101015/article/details/51305825
Python os.path模块介绍 https://www.cnblogs.com/dkblog/archive/2011/03/25/1995537.html
Python移动文件示例代码:
#coding: utf-8
import os import shutil
input_dir_path = ‘Final_Output’ + os.sep output_dir_path = ‘Tmp_Ouput’ + os.sep
if(not os.path.exists(output_dir_path)): os.mkdir(output_dir_path)
for file in os.listdir(input_dir_path): file_path = os.path.join(input_dir_path, file) #print(file_path) output_cancer_dir_path = os.path.join(output_dir_path, file) if os.path.isdir(file_path):
if(not os.path.exists(output_cancer_dir_path)):
os.mkdir(output_cancer_dir_path)
#file_path = file_path + os.sep
print('file_path: ' + file_path)
for inner_file in os.listdir(file_path):
if os.path.splitext(inner_file)[1] == '.dat':
inner_file_path = os.path.join(file_path, inner_file)
print('inner_file_path: ' + inner_file_path)
outer_file_path = os.path.join(output_cancer_dir_path, inner_file)
shutil.copyfile(inner_file_path, outer_file_path)
Python语言在函数中传递的是列表的引用,在函数内部对列表对象的修改,会影响该列表对象的整个生命周期。
Python list extend和append方法添加元素时的异同(以前应该整理过)
Python语法: type(secondDict[key]).name == ‘dict’
函数名plotTree.totalW为全局变量??
pickle.dump(inputTree, fw)
lenses = [inst.strip().split(‘\t’) for inst in fr.readlines()]
[[‘young’, ‘myope’, ‘no’, ‘reduced’, ‘no lenses’], [‘young’, ‘myope’, ‘no’, ‘normal’, ‘soft’], [‘young’, ‘myope’, ‘yes’, ‘reduced’, ‘no lenses’], [‘young’, ‘myope’, ‘yes’, ‘normal’, ‘hard’], [‘young’, ‘hyper’, ‘no’, ‘reduced’, ‘no lenses’]]
https://segmentfault.com/a/1190000006158803 https://www.cnblogs.com/nju2014/p/5631033.html
注意Python set和list的用法: def createVocabList(dataSet): vocabSet = set([]) for document in dataSet: vocabSet = vocabSet | set(document) return list(vocabSet)
Python list的index函数 returnVec[vocabList.index(word)] = 1
retlist.insert(0,key)
frozenset
注意frozenset也会使得list中的元素位置改变
python dict.update函数
random选择index和np.matrix的使用 randIndex = int(random.uniform(0, len(dataIndex))) h = sigmoid(sum(dataMatrix[randIndex]*weights))
returnVec = [0] * len(vocabList)
Python str的split函数会返回一个list
mySent = ‘This book is the best book on Python or M.L. I have ever laid eyes upon.’ mySent.split() import re regEx = re.compile(‘\W*’) listOfTokens = regEx.split(mySent)
python在函数定义中写import re的好处。
python2和python3中range函数的区别 range(start, end, step),返回一个list对象,起始值为start,终止值为end,但不含终止值,步长为step。只能创建int型list。 arange(start, end, step),与range()类似,但是返回一个array对象。需要引入import numpy as np,并且arange可以使用float型数据。
python2和python3的区别 https://www.cnblogs.com/hanggegege/p/5840005.html
python exp函数在math模块中math.exp()
python random.shuffle是什么意思?
headerTable = {} for trans in dataSet: for item in trans: headerTable[item] = headerTable.get(item, 0) + dataSet[trans]
注意python dict的get方法的默认值用法
for k in headerTable与for k in headerTable.keys()等价。 另一个是for k in headerTable.values()
items = [v[0] for v in sorted(localD.items(), key=lambda p: p[1], reverse=True)]
python list items[1::]什么意思??
python zip函数
python2需要import的函数 from future import print_function, division
注意正则表达式 def parse_addy(r): so_zip = re.search(‘, NY(\d+)’, r) so_flr = re.search(‘(?:APT|#)\s+(\d+)[A-Z]+,’, r) if so_zip: zipc = so_zip.group(1) else: zipc = np.nan if so_flr: flr = so_flr.group(1) else: flr = np.nan return pd.Series({‘Zip’:zipc, ‘Floor’: flr})
zip函数的使用 for i, w, t in zip(rez[rez[‘wanted’]==’y’].index, rez[rez[‘wanted’]==’y’][‘wanted’], rez[rez[‘wanted’]==’y’][‘title’]): print(i, w, t)
zip函数如何使用? dfc[‘reds’], dfc[‘greens’], dfc[‘blues’] = zip(*dfc[‘main_rgb’].map(get_csplit))
repo_vocab = [item for sl in list(starred_repos.values()) for item in sl]