
python
sihsd
Be happy, be positive, be aggressive.
展开
-
【Python】浮点数精度问题与解决
a = 1.1b = 2.2r1 = b-aprint(r1)import decimaldecimal.getcontext().prec = 2a = decimal.Decimal('1.1')b = decimal.Decimal('2.2')r2 = b-aprint(r2)a = float(a)b = float(b)r3 = b-aprint(r3)print(r1==r2,r1==r3,r2==r3)decimal.getcontext().prec =原创 2022-05-17 23:12:50 · 825 阅读 · 0 评论 -
[python]TypeError: unsupported operand type(s) for /: ‘int‘ and ‘IntervalMath‘
自定义类型IntervalMath的实例a,求a/2可以进入类的内置函数__truediv__,而2/a进不去__rdiv__,只能在外面将2转化为IntervalMath类型。否则会报错:TypeError: unsupported operand type(s) for /: ‘int’ and 'IntervalMath’但a/2时就不会报错。# -*- coding: utf-8 -*-"""Created on Wed Mar 9 21:10:37 2022@author: REE原创 2022-03-11 20:20:48 · 1863 阅读 · 0 评论 -
[python]TypeError: only size-1 arrays can be converted to Python scalars
当在代码中同时import numpy库和math库时,在进行sin、exp等运算时需要使用 np.exp()或者 np.sin(),否则就会报这个错误。# -*- coding: utf-8 -*-"""Created on Wed Mar 9 19:45:00 2022@author: REESE"""from math import *import numpy as npdef f(x): return np.exp(-x**2)*np.sin(10*x)def trap原创 2022-03-11 19:59:50 · 1747 阅读 · 0 评论 -
[Python]内置函数sorted()在列表、元组、字典排序时的使用方法【全】
原创 2020-03-20 13:47:11 · 642 阅读 · 1 评论 -
[Python]DataFrame的创建、数据读取、增加删除行列
DataFrame数据读取读取具体单元格值方法一 df[‘ColName’][行号]方法二 df.values[1][1]df.values 去掉行名、列名只剩下数组构成的数组 ‘numpy.ndarray’可用df.values[行号]取出整行数据用df.values[行号][列号]取出单个数据读取一列df[‘ColName’]读取多列df[[‘ColName1’,‘ColN...原创 2020-03-19 14:56:33 · 2698 阅读 · 1 评论 -
[Python]数组、列表、矩阵的相互转换
原创 2020-03-18 15:59:49 · 1137 阅读 · 0 评论 -
[Python]数组与矩阵的区别
原创 2020-03-18 15:59:04 · 211 阅读 · 0 评论 -
[Python]数组的创建、更新、读取、属性
原创 2020-03-18 15:57:37 · 1091 阅读 · 0 评论 -
【Python】数据分析注意事项
1.报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte在pandas.read_csv()的参数中加encoding="gbk"即可2.报错:找不到路径pd.read_csv(r"C:\Users\REESE\Desktop\a.csv")...原创 2020-02-23 21:07:37 · 313 阅读 · 0 评论 -
【Python】运算符优先级
转载 2020-02-21 19:06:10 · 183 阅读 · 0 评论 -
【python】恺撒密码的五种方法
方法一A = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']a = input()for i in a: if i in A: b = A[(ord(i) - ord('A') + 3) % 26] ...原创 2020-02-17 00:01:55 · 1964 阅读 · 0 评论 -
【python】数字变换的三种方法
方法一 字符串转化成列表,赋值后,再连接成字符串NumStr = input()NumList = list(NumStr)for i in range(0,len(NumStr)):#最后一个字符的下标是长度-1 switch(eval(NumList[i])) { case(0): eval(NumList[i]) =...原创 2020-02-09 03:26:51 · 1261 阅读 · 0 评论