Python语法
xyhhhhhhhha
码农
展开
-
Python2转Python3 中报错AttributeError: ‘generator‘ object has no attribute ‘next‘
g.next() has been renamed to g.__next__(). The reason for this is consistency: special methods like __init__() and __del__() all have double underscores (or "dunder" in the current vernacular), and .next() was one of the few exceptions to that rule. This原创 2021-05-06 09:21:42 · 346 阅读 · 0 评论 -
map函数中Python2与Python3的区别
Python2中map函数之后返回的是一个list而Python3中map函数返回的是一个对象,为了等价转化,我们需要再Python3中的map前加上list()原创 2021-05-06 09:16:40 · 147 阅读 · 0 评论 -
python中loc和iloc
loc函数:通过行索引 "Index" 中的具体值来取行数据(如取"Index"为"A"的行) iloc函数:通过行号来取行数据(如取第二行的数据) 本文给出loc、iloc常见的五种用法,并附上详细代码。 1. 利用loc、iloc提取行数据 import numpy as npimport pandas as pd#创建一个Dataframedata=pd.DataFrame(np.arange(16).reshape(4,4),index=list('...转载 2021-04-20 15:20:32 · 758 阅读 · 0 评论 -
python Isomap函数数据降维
模型原型class sklearn.manifold.Isomap(n_neighbors=5,n_components=2,eigen_solver=’auto’,tol=0,max_iter=300,path_method=’auto’,neighbors_algorithm=’auto’)参数n_neighbors:近邻参数kn_components:指定低维的维数eigen_solver:指定求解特征值的算法 ’auto’:由算法自动选取 ‘arpack’:Arpack分解算法 ‘转载 2020-12-30 15:23:19 · 2220 阅读 · 0 评论 -
argparse用法教程
import argparseparser.add_argument('--input_data', metavar='input_data', type=str,default="./data/train.txt")args = parser.parse_args()train_data, train_labels = read_data(args.input_data)原创 2020-11-06 16:47:29 · 111 阅读 · 0 评论 -
关于python2和python3 sort函数出错的问题
在python 2中np.sort函数可以正常运行。在python3中会出错返回axis=-1,解决方法,将np.sort()函数转化为sorted()函数即可。原创 2019-11-14 16:27:17 · 684 阅读 · 0 评论 -
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 0: ordinal not in range(128)
将training_data, validation_data, test_data = cPickle.load(f)改为training_data, validation_data, test_data = cPickle.load(f,encoding=‘bytes’)————————————————原文链接:https://blog.csdn.net/qq_41185868/ar...转载 2019-09-22 16:24:05 · 319 阅读 · 0 评论 -
Python除法
Python除法保留小数点位数(三种方法实现)python 自带的除法如a=1, b=3, a/b=0.333333333 自带小数的方法1 a = 1 b = 3 print(round(a/b,2))方法2 a = 1 b = 3 print(format(float(a)/float(b), '.2f'))方法3 a = 1 b = 3 print('...转载 2019-06-17 17:00:30 · 1283 阅读 · 0 评论 -
Python list操作
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。 Python有6个序列的内置类型,但最常见的是列表和元组。序列都可以进行的操作包括索引,切片,加,乘,检查成员。此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出...转载 2019-06-02 18:31:26 · 883 阅读 · 0 评论 -
pycharm import中出现红色波浪线,运行无误,改变默认查找路径
前提是在pycharm中执行没问题,解决方法是在pycharm中设置一下:file——setting——project:xxx——project structure——选中.py模块所在路径然后点击“source”——ok红色波浪线就可以消除!! ...转载 2019-06-02 18:23:04 · 1430 阅读 · 0 评论 -
python str除去某一字符
python中的strip()可以去除头尾指定字符ss = '我的电话是18827038663,也是微信号,\n 请加入,谢谢\n\n\n'print(ss.strip('\n'))结果:我的电话是18827038663,也是微信号, 请加入,谢谢...转载 2019-06-02 15:56:55 · 14166 阅读 · 2 评论 -
Python array 删除固定元素
import numpy as npa = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])index = [2, 3, 6]new_a = np.delete(a, index)print(new_a) #Prints [1, 2, 5, 6, 8, 9]注意,numpy.delete()从array scalars开始返回一个新数组是不可变的,类似于Pyt...转载 2019-06-07 11:13:32 · 13632 阅读 · 0 评论 -
删除list中的元素 del方法
A= [4, 6, 8]del A[1]print(A)# Output [4,8]原创 2019-06-07 11:08:07 · 1626 阅读 · 0 评论 -
Python str类型在指定位置插入字符
a= “20190607”A= list(a) # 转化A.insert(4, ‘/’) # 注意不用重新赋值A.insert(7, ‘/’)a = ‘’.join(A) # 转化回来print(a)输出:2019/06/07原创 2019-06-07 10:19:59 · 14573 阅读 · 0 评论