鉴于n列表中包含m字典作为元素,我想生成一个新列表,其中包含一组连接的字典.每个字典都保证有一个名为"索引"的键,但除此之外可以有一组任意键.非索引键永远不会在列表中重叠.例如,想象以下两个列表:
l1 = [{"index":1, "b":2}, {"index":2, "b":3}, {"index":3, "green":"eggs"}]
l2 = [{"index":1, "c":4}, {"index":2, "c":5}]
Run Code Online (Sandbox Code Playgroud)
("b"永远不会出现l2,因为它出现了l1,同样地,它"c"永远不会出现l1,因为它出现在l2)
我想制作一个联合列表:
l3 = [{"index":1, "b":2, "c":4},
{"index":2, "b":3, "c":5},
{"index":3, "green":"eggs"}]
Run Code Online (Sandbox Code Playgroud)
在Python中执行此操作的最有效方法是什么?
我想使用Python 2.6.5计算大型矩阵(约1000x1000)的特征值.我一直无法做到这一点.我还没有发现任何其他线程来解决这个问题.
我跑的时候
a = rand(1000,1000);
tic;
for i =1:10
eig(a);
end
toc;
Run Code Online (Sandbox Code Playgroud)
在MATLAB中大约需要30秒.Python中的类似测试需要216秒.使用RPy通过R运行它并没有明显加快计算速度.Octave的测试耗时93秒.我对速度上的差异感到有点困惑.
我可以在网上找到这样一个问题的唯一例子就是这个,这已经有好几年了.该问题中的海报有一个不同的Python目录结构(我将其归因于帖子的年龄,虽然我可能会弄错),所以我没有足够的信心尝试按照记者发布的说明进行操作.
我的包管理器说我安装了LAPACK,我使用NumPy和SciPy进行Python计算:
from numpy import *
from scipy import *
from numpy.linalg import *
import time
a = randn(1000,1000)
tic = time.clock()
for i in range(0,10):
eig(a)
toc = time.clock()
print "Elapsed time is ", toc-tic
Run Code Online (Sandbox Code Playgroud)
我是Python的新手,所以我可能做了些傻事.如果我需要提供更多信息,请告诉我.
在某些情况下,我们的团队必须使用Python 2.4.1.在Python 2.4.1 strptime中的datetime.datetime模块中不存在:
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.strptime
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
Run Code Online (Sandbox Code Playgroud)
与2.6相反:
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> …Run Code Online (Sandbox Code Playgroud) 我最近一直在考虑转换到Python编程语言.目前,Matlab是我部门快速开发和编写代码原型的首选语言.它非常擅长这一点,但是Mathworks(生产Matlab的公司)一直在修改许可条款,导致无法存在的麻烦.
所以我将一些数据存储为两个列表,并使用它们绘制它们
plot(datasetx, datasety)
Run Code Online (Sandbox Code Playgroud)
然后我设置了趋势线
trend = polyfit(datasetx, datasety)
trendx = []
trendy = []
for a in range(datasetx[0], (datasetx[-1]+1)):
trendx.append(a)
trendy.append(trend[0]*a**2 + trend[1]*a + trend[2])
plot(trendx, trendy)
Run Code Online (Sandbox Code Playgroud)
但我有第三个数据列表,这是原始数据集中的错误.我很好地绘制了错误栏,但我不知道是使用这个,如何在多项式趋势线的系数中找到错误.
所以说我的趋势线是5x ^ 2 + 3x + 4 = y,需要在5,3和4值上出现某种错误.
是否有使用NumPy的工具可以为我计算?
我正在使用scipy.optimize.curve_fit,但我怀疑它正在收敛到局部最小值而不是全局最小值.
我尝试以下列方式使用模拟退火:
def fit(params):
return np.sum((ydata - specf(xdata,*params))**2)
p = scipy.optimize.anneal(fit,[1000,1E-10])
Run Code Online (Sandbox Code Playgroud)
specf我试图适应的曲线在哪里.p尽管结果显然比返回的最小值更差,curve_fit即使返回值表明已达到全局最小值(参见退火).
如何改善结果?SciPy中是否有全球曲线钳工?
python simulated-annealing scientific-computing curve-fitting scipy
我正在寻找一种更有效的方法来重新设置优先级队列中的项目的优先级.我有一个(非常天真)优先级队列实现基于heapq.相关部分如下:
from heapq import heapify, heappop
class pq(object):
def __init__(self, init= None):
self.inner, self.item_f= [], {}
if not None is init:
self.inner= [[priority, item] for item, priority in enumerate(init)]
heapify(self.inner)
self.item_f= {pi[1]: pi for pi in self.inner}
def top_one(self):
if not len(self.inner): return None
priority, item= heappop(self.inner)
del self.item_f[item]
return item, priority
def re_prioritize(self, items, prioritizer= lambda x: x+ 1):
for item in items:
if not item in self.item_f: continue
entry= self.item_f[item]
entry[0]= prioritizer(entry[0])
heapify(self.inner)
Run Code Online (Sandbox Code Playgroud)
这是一个简单的协同例程,可以在我的实际应用程序中演示重新优先级特性.
def fecther(priorities, …Run Code Online (Sandbox Code Playgroud)
假设我们有一个a = "01000111000011"带n=5 "1"s 的字符串.第i个 "1",我想替换为第i个角色"ORANGE".我的结果应该是这样的:
b = "0O000RAN0000GE"
Run Code Online (Sandbox Code Playgroud)
什么是在Python中解决这个问题最好的方法?是否可以将索引绑定到每个替换?
非常感谢!海尔格
我正在尝试集成此功能:

但是我遇到了以下错误:
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File "siestats.py", line 349, in NormalDistro
P_inner = scipy.integrate(NDfx,-dev,dev)
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
我的代码运行这个:
# Definition of the mathematical function:
def NDfx(x):
return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
# This Function normailizes x, u, and o2 (position of interest, mean and st dev)
# and then calculates the probability up to position 'x'
def NormalDistro(u,o2,x):
dev = abs((x-u)/o2)
P_inner = scipy.integrate(NDfx,-dev,dev)
P_outer = 1 - P_inner
P = P_inner …Run Code Online (Sandbox Code Playgroud) 我真的不明白这里发生了什么但是:
当我做:
colorIndex += len - stopPos;
for(int m = 0; m < len - stopPos; m++)
{
colorUniPos++;
}
Run Code Online (Sandbox Code Playgroud)
它没有给我与做的相同的结果:
colorIndex += len - stopPos;
colorUniPos += len - stopPos;
Run Code Online (Sandbox Code Playgroud)
我认为它会因某种原因而消失.这两个都不应该获得相同的结果吗?
谢谢
我有两个这样的清单
volB = [(Tarp, 3440, 7123), (Greg, 82, 1083)]
Run Code Online (Sandbox Code Playgroud)
和
# 500B = [(Tarp, 85, 203), (Greg, 913, 234)]
B500 = [(Tarp, 85, 203), (Greg, 913, 234)]
Run Code Online (Sandbox Code Playgroud)
我想把第二个元素相互分开.(在这种情况下,我想将3440除以85,将82除以913,依此类推.感谢您的帮助?
python ×10
numpy ×3
scipy ×3
matlab ×2
c++ ×1
datetime ×1
eigenvalue ×1
function ×1
list ×1
loops ×1
performance ×1
replace ×1
string ×1
substitution ×1
trendline ×1