python
watermelon12138
心有猛虎,细嗅蔷薇
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
python中的单下划和双下划线属性
1、_对象名级别:受保护的属性使用:父类对象和子类对象都可以直接调用,父类中受保护的属性可以被子类覆盖。class A(object): def __init__(self): self.name = 'yx' self._age = 24 self.__gender = 'm' def _printAge(self): print(self._age) def __printGender(self):原创 2020-09-05 12:48:31 · 574 阅读 · 0 评论 -
python中的copy
浅拷贝1、对象.copy()2、copy.copy()import copya = [1, 2, 3, [4, 5]]b = a.copy()c = copy.copy(a)b == cTrueb is c # b和c是两个值相同的不同对象False# 浅拷贝意味着对a中的子对象进行修改会同步修改b和c中的子对象,a本身就是一个list对象,# 而a中还包含一个list对象[4, 5],所以这个叫做子对象。对a进行append, remove, pop#等操作不影响b和c的值,但原创 2020-09-05 11:32:42 · 579 阅读 · 0 评论 -
牛客算法题的输入写法记录(python版)
情况1输入41 2 33 4 54 5 67 8 9输出1345解释输入: 第一行是一个整数n,意味着接下来有n行输入数据。输出: 每行输入数据对应的一行输出结果。while True: try: n = int(input()) # 拿到第一行的n for _ in range(n): # 接下来的n行输入对应n个输出 # 拿到当前行的输入 input_data = input() # 处理当前行的输入 outpu原创 2020-07-15 18:17:57 · 1588 阅读 · 2 评论 -
二分查找(返回查找值的左右边界索引)
1、返回查找值的任意索引搜索区间是 [left, right] 的左闭右闭写法int binary_search(int[] nums, int target) { int left = 0, right = nums.length - 1; while(left <= right) { int mid = left + (right - left) / 2; if (nums[mid] < target) { l转载 2020-07-04 18:01:20 · 986 阅读 · 0 评论 -
python——解决pip的“failed to create process”问题,以及用国内镜像批量下载requirements.txt中包
一、pip的“failed to create process”问题原因1:pip.exe一般在python下面的Scripts文件夹里,这个文件夹里面还有一个pip-script.py文件(注意pip3.exe对应的是pip3-script.py,以此类推)。用编辑器打开pip-script.py文件,它的首行是你的python解析器的位置,检查一下位置有没有出错。原因2:在使用pip的时候最好用"where pip"检查一下是否显示的路径只有一个,或者用“pip -V”看一下当前用的pip的路径是什原创 2020-05-22 16:57:52 · 3523 阅读 · 0 评论 -
python3创建虚拟环境并配置环境变量(windows)
前提:python3环境,python2的可以看看环境变量配置。1、建立一个venv文件夹专门存放各个项目的虚拟环境。如:我在F盘下建立venv2、win + r 召唤出命令行,执行python,记住哦,python还要分python2还是python3。如果你是python3环境那一会儿建立的就是python3的虚拟环境,python2亦是如此。3、执行" python -m venv ...原创 2020-04-04 18:27:54 · 6861 阅读 · 1 评论 -
DataFrame数据处理笔记(长期更新)
本文使用的案列:>>> df a b c0 1 2.0 31 4 5.0 62 7 8.0 9>>> df.dtypesa objectb float64c int64dtype: objectDataFrame的数据类型改变1、单列数据类型改变方法一:pd.to_numeric(...原创 2019-12-02 16:23:04 · 463 阅读 · 0 评论 -
python计算阶乘的两个函数
reduce()#从functools中调用reduce()函数from functools import reduce#使用lambda,匿名函数,迭代num = reduce(lambda x,y:x*y,range(1,n)) # 计算n的阶乘print(num)factorial()import mathvalue = math.factorial(n) #...原创 2019-11-06 15:33:45 · 1563 阅读 · 1 评论 -
按照步长切割字符串形成列表
记录一下小知识哈!s = '123456'l = [s[i:i+2] for i in range(0, len(s), 2)] # 2是步长,可随意改变"得:l = ['12', '34', '56']"原创 2019-10-10 17:53:42 · 1245 阅读 · 2 评论 -
pandas——往excel的现有sheet里面添加数据
一、往excel的现有sheet里面添加数据例子:将final_group_calc_c2b.xlsx和final_group_calc_b2b.xlsx的数据导入到final_group_calc_b2c.xlsx里面,这3个excel里面的sheet名称和sheet数量都相同。# coding:utf-8import pandas as pdfrom openpyxl import ...原创 2019-09-17 16:39:56 · 11223 阅读 · 0 评论 -
python——爬虫中使用xpath过滤结果为空以及几种UnicodeEncodeError
情况1:在网页中使用xpath可以查找到内容,但在pycharm中查找结果为空情况2:出现UnicodeEncodeError: ‘latin-1’ codec can’t encode character ‘\u2026’ in position 30: ordinal not in range(256)情况3:出现UnicodeEncodeError: ‘ascii’ codec c...原创 2019-03-20 18:26:11 · 1498 阅读 · 0 评论 -
python——初始化数组
因为画图中x轴与y轴的数据通常为数组格式的数据,所以先总结一下如何初始化数组:(1)list得到数组# 通过array函数传递list对象 L = [1, 2, 3, 4, 5, 6] a = np.array(L)# 若传递的是多层嵌套的list,将创建多维数组 b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, ...原创 2018-12-30 09:23:27 · 47695 阅读 · 0 评论 -
python——cross_val_score()函数、ShuffleSplit()函数、zip()函数
cross_val_score():# cross_val_score(estimator, X, y=None, scoring=None, cv=None, n_jobs=1)# 该函数用交叉检验(cross-validation)来估计一个模型的得分# estimator:需要一个模型对象# X和y分别表示自变量和因变量# scoring:需要string, callable or...原创 2019-01-17 17:20:44 · 8711 阅读 · 1 评论 -
python——defaultdict()函数和shuffle()函数
defaultdict():# defaultdict(default_factory=None, **kwargs)# 普通的dict当查询某个不存在的key的value时会报错,而defaultdict不会报错它会返回工厂函数对应的返回值# 工厂函数可以取list,set,str,int等,对应的返回值为[],set(),'',0np.random.shuffle():# np.r...原创 2019-01-17 17:23:40 · 498 阅读 · 0 评论 -
python——0-1矩阵、prod()连乘函数、字符串和list转换
(1)DataFrame格式的数据得到0-1矩阵DataFrame格式的数据如下:data: 0 1 2 30 a c e NaN1 b d NaN NaN2 b c NaN NaN3 a b c d4 a b NaN NaN5 b c NaN NaN6 a b NaN NaN7 a ...原创 2019-01-21 18:10:06 · 9945 阅读 · 1 评论 -
python——tile()函数、Counter()函数、将float型的字符串转为int型
tile()函数(1)一维数组使用tile()import numpy as np# a = np.array([0, 1, 2])# print('a: \n', a)# 原数组a是一个一维数组,shape为(3,),是一个列向量# 将原数组a中的元素看作一个整体按行复制2次得到新的数组(新的列向量[a, a]),shape为(6,)# a1 = np.tile(a, 2)# ...原创 2019-03-02 16:58:51 · 514 阅读 · 0 评论 -
Python——pandas中的concat()函数和append()函数
一、concat()函数拼接多个Series# 1、按列(axis=0)拼接多个Series,拼接后仍为Series格式import pandas as pd# s1 = pd.Series(['a', 'b', 'c'])# print('s1: \n', s1)# s2 = pd.Series(['A', 'B', 'C'])# print('s2: \n', s2)# #...原创 2019-02-26 17:27:53 · 15818 阅读 · 1 评论 -
python——罗马数字和阿拉伯数字的互相转换以及单元测试
(1)roman.py# -*-coding:utf-8-*-"""this mode implements two functions, one implements the change from integer to roman numeral,another implements the opposite.""""""functional requirements:(1)...原创 2019-03-06 21:47:08 · 2932 阅读 · 0 评论 -
python——annotate函数
一、annotate函数该函数的详细参数可调用内置属性__doc__查看。 import matplotlib.pyplot as plt # plt.annotate(str, xy=data_point_position, xytext=annotate_position, # va="center", ha="center", xycoord...原创 2019-03-10 16:25:15 · 21286 阅读 · 0 评论 -
python——sorted()排序函数和round()函数
sorted()函数:# sorted()默认为升序排列,reverse=True指定为降序排列# sorted()函数可以对list,dict,array,tuple进行排序# 例子:# (1)排序字符串列表# scores = ['A', 'B', 'C', 'D']# scores = sorted(scores, reverse=True)# 得:['D', 'C', 'B'...原创 2018-12-29 16:01:21 · 638 阅读 · 0 评论
分享