我正在使用熊猫来分析一些选举结果.我有一个DF,结果,每个选区都有一行,代表各方投票的列(超过100个):
In[60]: Results.columns
Out[60]:
Index(['Constituency', 'Region', 'Country', 'ID', 'Type', 'Electorate',
'Total', 'Unnamed: 9', '30-50', 'Above',
...
'WP', 'WRP', 'WVPTFP', 'Yorks', 'Young', 'Zeb', 'Party', 'Votes',
'Share', 'Turnout'],
dtype='object', length=147)
Run Code Online (Sandbox Code Playgroud)
所以...
In[63]: Results.head()
Out[63]:
Constituency Region Country ID Type \
PAID
1 Aberavon Wales Wales W07000049 County
2 Aberconwy Wales Wales W07000058 County
3 Aberdeen North Scotland Scotland S14000001 Burgh
4 Aberdeen South Scotland Scotland S14000002 Burgh
5 Aberdeenshire West & Kincardine Scotland Scotland S14000058 County
Electorate Total Unnamed: 9 30-50 Above …Run Code Online (Sandbox Code Playgroud) 我一直在比较numpy与Python列表推导的相对效率,将随机数的数组相乘.(Python 3.4/Spyder,Windows和Ubuntu).
正如人们所预料的那样,对于除最小阵列之外的所有阵列,numpy迅速优于列表理解,并且为了增加阵列长度,您将获得预期的Sigmoid曲线以获得性能.但是sigmoid远非光滑,我很难理解.
显然,对于较短的阵列长度存在一定量的量化噪声,但是我得到了意外的噪声结果,特别是在Windows下.这些数字是各种阵列长度的100次运行的平均值,所以应该平滑任何瞬态效应(所以我想到).
Numpy and Python list performance comparison
Run Code Online (Sandbox Code Playgroud)
下图显示了使用numpy与列表理解的不同长度的乘法数组的比率.
Array Length Windows Ubuntu
1 0.2 0.4
2 2.0 0.6
5 1.0 0.5
10 3.0 1.0
20 0.3 0.8
50 3.5 1.9
100 3.5 1.9
200 10.0 3.0
500 4.6 6.0
1,000 13.6 6.9
2,000 9.2 8.2
5,000 14.6 10.4
10,000 12.1 11.1
20,000 12.9 11.6
50,000 13.4 11.4
100,000 13.4 12.0
200,000 12.8 12.4
500,000 13.0 12.3
1,000,000 13.3 12.4
2,000,000 13.6 12.0
5,000,000 13.6 11.9
Run Code Online (Sandbox Code Playgroud)
所以我想我的问题是任何人都可以解释为什么结果,特别是在Windows下如此嘈杂.我已多次运行测试,但结果似乎总是完全相同.
UPDATE.在Reblochon …
对于这个问题,我想解析命令行上传递给 Go 程序的日期/时间。目前,我使用该flag包填充字符串变量ts,然后使用以下代码:
if ts == "" {
config.Until = time.Now()
} else {
const layout = "2006-01-02T15:04:05"
if config.Until, err = time.Parse(layout, ts); err != nil {
log.Errorf("Could not parse %s as a time string: %s. Using current date/time instead.", ts, err.Error())
config.Until = time.Now()
}
}
Run Code Online (Sandbox Code Playgroud)
只要用户传递完全正确的格式(例如2019-05-20T09:07:33或类似的格式),这就可以正常工作。
然而,如果可能的话,我希望能够灵活地传递例如2019-05-20T09:07or2019-05-20T09或什至,并且2019-05-20在适当的情况下将小时、分钟和秒默认为 0。
有没有一种明智的方法可以做到这一点?
我已经找到了解决方案,虽然它不是特别优雅,但它似乎确实适用于我可能遇到的大多数情况。
package main
import (
"fmt"
"time"
)
func …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python 2.7和ReportLab生成具有不同奇数/偶数页面布局的PDF文档(以允许用于绑定的不对称边框).为了进一步复杂化,我试图每页生成两列.
def WritePDF(files):
story = []
doc = BaseDocTemplate("Polar.pdf", pagesize=A4, title = "Polar Document 5th Edition")
oddf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol1')
oddf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol2')
evenf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol1')
evenf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol2')
doc.addPageTemplates([PageTemplate(id='EvenTwoC',frames=[evenf1,evenf2],onPage=evenFooter),
PageTemplate(id='OddTwoC', frames=[oddf1, oddf2], onPage=oddFooter)])
...
story.append(Paragraph(whatever, style))
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何使ReportLab在左右(或奇数和偶数)页面之间交替.有什么建议?
我目前正在玩Python中的Project Euler问题53.解决方案很简单,但涉及以下列表理解:
[scipy.misc.comb(n, r, exact=True)
for n in range(1,maxn+1)
for r in range(0,n+1)
if scipy.misc.comb(n, r, exact=True) > threshold]
Run Code Online (Sandbox Code Playgroud)
我关心的是scipy.misc.comb()函数每次迭代都会被调用两次.有没有办法用某种参考替换它的一个或另一个出现; 或者是解释器足够智能,以实现两个实例将评估相同的东西?
有没有Python的方式可以返回列表中的第一个项目,而该列表也是另一个列表中的项目?目前,我正在使用蛮力和无知:
def FindFirstMatch(a, b):
"""
Returns the first element in a for which there is a matching
element in b or None if there is no match
"""
for item in a:
if item in b:
return item
return None
Run Code Online (Sandbox Code Playgroud)
如此FindFirstMatch(['Fred','Wilma','Barney','Betty'], ['Dino', 'Pebbles', 'Wilma', 'Bambam'])回报,'Wilma'但我想知道是否有更优雅/有效/ Pythonic的方式。
我写了一个短倍频脚本绘制功能sum(sin(2k-1)/(2k-1))的k = 1..n条款.(我试图模拟连续项如何使输出收敛到方波.
% Program to model square-wave using sum of sines
terms=3
theta=linspace(0, 6*pi, 1000);
k=[1:terms]';
n=2*k-1;
q=sin(n*theta)./n;
y=sum(q);
plot(theta, y);
Run Code Online (Sandbox Code Playgroud)
sum()对于术语> 1,它工作正常(即函数返回包含每列总和的向量)但是当术语== 1(即它应该只绘制一个正弦波)时,该sum()函数计算行的总和并返回一个标量.
sum()即使只有一行,我如何得到函数总计每列,或者如何重新整形或切片或任何行向量,使得它不是有效成为二维矩阵的维度n的一维向量尺寸1xn?