【python – Swig从Base *向下转换为Derived *】教程文章相关的互联网学习教程文章

转载:python库Pyproj进行坐标转换

原文链接:https://www.baidu.com利用Pyproj进行坐标转换 作者:郜科科 两个坐标系统的参考椭球不同,实地一个点的不同坐标系的值是不同的,不同的部门采用的坐标系统经常是不一致,所以要转换后才能相互利用。例如目前使用的北京市观测站点位置根据GPS的定位而来,GPS使用的地理坐标系为GCS_WGS_1984,所以其坐标的地理坐标系也为GCS_WGS_1984,而假如需要将这些点显示在Web端的地图上,Web端的投影坐标系WGS_1984_Web_Mercator_A...

将python ndarray转换为theano张量类型变量【代码】

我有像ndarray:diag = [] diag.append(np.diag([1,1,0])) diag.append(np.diag([0,1,1])) diag[array([[1, 0, 0],[0, 1, 0],[0, 0, 0]]), array([[0, 0, 0],[0, 1, 0],[0, 0, 1]])]我如何将其转换为float类型为矩阵的Theano张量变量?因为我需要执行点操作Theano.dot(diag, X) where X is shared variable of type float 64, matrix.解决方法:像这样创建一个SharedVariablediag_ = theano.shared(np.array(diag).astype("float64"...

python-将PySpark数据框列类型转换为字符串并替换方括号【代码】

我需要将PySpark df列类型从数组转换为字符串,还要删除方括号.这是数据框的架构.需要处理的列是CurrencyCode和TicketAmount>>> plan_queryDF.printSchema()root|-- event_type: string (nullable = true)|-- publishedDate: string (nullable = true)|-- plannedCustomerChoiceID: string (nullable = true)|-- assortedCustomerChoiceID: string (nullable = true)|-- CurrencyCode: array (nullable = true)| |-- element: st...

Python的日期时间转换【代码】

这是我的代码:from datetime import datetimedef get_local_time(time_str):"""takes a string in the format of '27 March at 3:00' which is UTCand converts it to local time and AM/PM:param time_str:"""offset = datetime.now() - datetime.utcnow()time_dt = datetime.strptime(time_str, '%d %b at %H:%M')return (time_dt + offset).strftime('%I:%M %p')我遇到的问题是使用time_str,这仅是时间,并且不包括日/月.即:“...

python-将数据帧的unicode数据转换为字符串【代码】

我从读取xls文件获得的数据帧遇到一些麻烦.这样的数据帧上的每个数据都具有“ unicode”类型,对此我无能为力.我想将其更改为str值.另外,如果可能的话,我想知道这个事实的原因.我听到了有关“外部数据”的一些信息,而且我知道列和索引在这些名称之前都还显示了unicode的“ u”.我对编码几乎一无所知,如果有人另外解释一下,我将不胜感激. 我正在使用Python 2,并尝试使用功能如下逐列解决它.astype(str) .astype(basestring) .apply(...

Python Pandas子集十六进制字符串,转换为十进制【代码】

我有一个数据框. B列包含4个字符的十六进制值:dict = {'A': ['foo', 'bar', 'baz'], 'B': ['1346', '0f46', '5a46']} df = pd.DataFrame(dict)我只对B列中十六进制的前两个字符感兴趣.我想用十六进制中仅前两个字符替换B列,然后将它们转换为十进制. 因此,最终结果应该是一个数据帧,如下所示:A B foo 19 bar 15 baz 90我什至不知道如何将前两个字符设置为子集.看来应该可以,但是不能:df.B.str[:2]任何帮助将不胜感激.解决方...

python-以任何方式在Pandas DataFrame查询中强制转换类型吗?【代码】

假设我有一个3列的数据框,都为浮点型,将其命名为DT1.现在,如果我想通过查询DT1从DT1创建另一个数据帧,请说第二个称为DT2.DT2 = DT1.query(‘(column1/column2) == (column3/column2)’)仅当方程式的两边完全匹配时,此方法才有效.如果我只想比较两侧的整数结果怎么办? 喜欢:DT2 = DT1.query(‘(column1/column2).astype(int) == (column3/column2)’).astype(int)上面的示例不起作用,有解决方案吗? PS:DT2 = DT1.loc(‘(DT1[col...

python-Matplotlib协调转换【代码】

我正在尝试理解以下代码片段:def add_inset(ax, rect, *args, **kwargs):box = ax.get_position()inax_position = ax.transAxes.transform(rect[0:2])infig_position = ax.figure.transFigure.inverted().transform(inax_position)new_rect = list(infig_position) + [box.width * rect[2], box.height * rect[3]]return fig.add_axes(new_rect, *args, **kwargs)此代码将插图添加到现有图形.看起来像这样: 原始代码来自this not...

python-在matplotlib中转换整个轴(或散点图)【代码】

我正在用以下代码绘制一些数据的均值和方差的变化import matplotlib.pyplot as pyplot import numpyvis_mv(data, ax = None):if ax is None: ax = pyplot.gca()cmap = pyplot.get_cmap()colors = cmap(numpy.linspace(0, 1, len(data)))xs = numpy.arange(len(data)) + 1means = numpy.array([ numpy.mean(x) for x in data ])varis = numpy.array([ numpy.var(x) for x in data ])vlim = max(1, numpy.amax(varis))# varianceax.i...

为什么此Python强制转换在map和没有map时表现不同【代码】

当从int到string的两个强制转换看起来执行相同的操作时,为什么这两个print语句会产生不同的结果?我想念什么? board是一个整数列表#ex.1 print ' '.join(map(str, board[:3])) #ex.2 print ' '.join(str(board[:3]))#out.1 0 1 2 #out.2 [ 0 , 1 , 2 ]解决方法: print(" ".join(map(str, board[:3])))将切片后的板的每个项目映射到一个文字整数,并用空格(可能是您想要的,以及正确的做法)将其连接起来print(' '.join(str(board...

ValueError:无法将字符串转换为float:在python上绘制图形【代码】

我已经导入了一个csv文件,该文件全部包含带指数的小数,例如(5.5006250364943992 ** 02).我不断收到ValueError:无法将字符串转换为float.这是我所做的:import matplotlib.pyplot as plt import csv x = [] y = [] with open('DNSdata.csv', 'r') as csvfile:plots = csv.reader(csvfile, delimiter=',')for row in plots:x.append(float(row[0]))y.append(float(row[1])) plt.plot(x, y, label='DNSdata') plt.xlabel('x') plt.yl...

python-将多列表理解转换为单列表理解【代码】

我正在尝试使用列表理解来更改列表的值,我可以通过使用3个列表理解来做到这一点clr = [1,2,2,1,3,1,2,3] clr= ["green" if i== 1 else i for i in clr] clr = ["yellow" if i==2 else i for i in clr] clr = ["black" if i == 3 else i for i in clr]使用下面提到的代码会引发语法错误clr = ["green" if i== 1 else "yellow" if i==2 else "black" if i == 3 for i in clr]有什么更好的方法吗?解决方法:是.例如,您可以定义字...

快速将日期时间字符串转换为秒(Python3)【代码】

尝试将大量记录(时间序列)转换为int,如下所示:seconds_time = int(time.mktime(time.strptime(parts[0], '%Y%m%d %H%M%S')))不幸的是,这是代码的瓶颈(耗时增加约20倍).有什么建议可以改善吗? 提前致谢解决方法:实际上,有一种方法可以大大减少解析时间.import timestart = time.time()nb_loops = 1000000 time_string = "20170101 201456" for i in range(nb_loops):seconds_time = int(time.mktime(time.strptime(time_string, '...

python-使用numpy视图将int32转换为int8【代码】

我试图将numpy int32数组视为int8类型.>>> a = np.array([1, 2, 3, 4], dtype='int32') >>> a array([1, 2, 3, 4], dtype=int32) >>> a.view('int8') array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=int8)我希望将1转换为[0,0,0,1],但是为什么原来是[1、0、0、0]?这与号码在内存中的存储方式有关吗? 谢谢.解决方法:Is this related to how the number is stored in memory?是的,有big endian and low endian.要...

使用python将包含参数的字符串转换为数组【代码】

我有这个python脚本,它将参数作为以“,”分隔的字符串,但是我不能只拆分它,因为有些参数包含“,”.输入是这样的:"hello, how are you","how old are you"我想让他们成为:["hello, how are you","how old are you"]解决方法:由于您的字符串看起来像csv,因此也许可以使用csv模块.import csv my_str = '"hello, how are you","how old are you"' my_csv = [my_str] # Wrap in a list because the csv module expects it csv_reader ...