我对以下代码感到有点困惑:
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
print key, 'corresponds to', d[key]
Run Code Online (Sandbox Code Playgroud)
我不明白的是这个key部分.Python如何识别它只需要从字典中读取密钥?keyPython中是一个特殊的词吗?或者它只是一个变量?
我有一个脚本,作为使用Python unittest模块编写的一些单元测试的包装器.除了清理一些文件,创建输出流和生成一些代码之外,它还将测试用例加载到套件中
unittest.TestLoader().loadTestsFromTestCase()
Run Code Online (Sandbox Code Playgroud)
我已经optparse用来提取几个用于确定输出位置的命令行参数,是否重新生成代码以及是否进行一些清理.我还想传递一个配置变量,即端点URI,以便在测试用例中使用.
我意识到我可以添加一个OptionParserTestCase的setUp方法,但我希望将选项传递给setUp.这可能用loadTestsFromTestCase()吗?我可以迭代返回TestSuite的TestCases,但是我可以手动调用setUp TestCases吗?
**编辑**我想指出我能够传递参数,setUp如果我迭代测试并setUp手动调用,如:
(options, args) = op.parse_args()
suite = unittest.TestLoader().loadTestsFromTestCase(MyTests.TestSOAPFunctions)
for test in suite:
test.setUp(options.soap_uri)
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用xmlrunner它,它的run方法以一个TestSuite参数为例.我假设它将运行setUp方法本身,所以我需要在其中可用的参数XMLTestRunner.
我希望这是有道理的.
我已经看过这个问题了:问题似乎已经实现了一种非常类似的技术来替换包括alpha值在内的单一颜色:
c = Image.open(f)
c = c.convert("RGBA")
w, h = c.size
cnt = 0
for px in c.getdata():
c.putpixel((int(cnt % w), int(cnt / w)), (255, 0, 0, px[3]))
cnt += 1
Run Code Online (Sandbox Code Playgroud)
但是,这很慢.我在互联网上发现了这个配方,但到目前为止还没有成功使用它:配方
我想要做的是拍摄由单一颜色,白色组成的各种PNG图像.每个像素都是100%白色,具有各种alpha值,包括alpha = 0.我想要做的是基本上使用新的设置颜色着色图像,例如#ff0000 <00-ff>.所以我的开始和结果图像看起来像这样,左边是我的起始图像,右边是我的结束图像(注意:背景已经变为浅灰色,所以你可以看到它,因为它实际上是透明的,你不会'能够看到左边的点.)

有更好的方法吗?
我正处于一直在进行的项目的最后阶段.一切都运行顺利,但我有一个瓶颈,我无法解决.
我有一个元组列表.该列表的长度范围为40,000-1,000,000条记录.现在我有一个字典,其中每个(值,键)都是列表中的元组.
所以,我可能会
myList = [(20000, 11), (16000, 4), (14000, 9)...]
myDict = {11:20000, 9:14000, ...}
Run Code Online (Sandbox Code Playgroud)
我想从列表中删除每个(v,k)元组.
目前我在做:
for k, v in myDict.iteritems():
myList.remove((v, k))
Run Code Online (Sandbox Code Playgroud)
从包含20,000个元组的列表中删除838个元组需要3到4秒.我很可能会从1,000,000的列表中删除更多像10,000个元组,所以我需要更快.
有一个更好的方法吗?
我可以提供用于测试的代码,如果需要,还可以提供实际应用程序中的pickle数据.
我有几十万个端点URL,我想为其生成统计信息.例如,我有:
/a/b/c
/a/b/d
/a/c/d
/b/c/d
/b/d/e
/a/b/c
/b/c/d
Run Code Online (Sandbox Code Playgroud)
我想创建一个看起来像这样的字典
{
{'a':
{'b':
{'c': 2 },
{'d': 1 }
},
{'c':
{'d': 1 }
}
},
{'b':
{'c':
{'d': 2}
},
{'d':
{'e': 1}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么聪明的方法吗?
编辑
我应该提到路径并不总是3个部分.可能有
/a/b/c/d/e/f/g/h...等等
我试图计算numpy数组M的行列式,np.shape(M)=(N,L,L)是这样的:
import numpy as np
M = np.random.rand(1000*10*10).reshape(1000, 10, 10)
dm = np.zeros(1000)
for _ in xrange(len(dm)):
dm[_] = np.linalg.det(M[_])
Run Code Online (Sandbox Code Playgroud)
有没有循环的方法?"N"比"L"大一些数量级.我想到了类似的东西:
np.apply_over_axes(np.linalg.det(M), axis=0)
Run Code Online (Sandbox Code Playgroud)
做我想要的更快的方式吗?我想循环开销是一个性能瓶颈,因为小矩阵的行列式是一个相对便宜的操作(或者我错了?).
我看过几篇类似的帖子,但没有解决方案,所以我想我会提出一个更有记录的问题.
来自清单文件的我的问题 JS不包括或编译任何JS.
在本地运行我的服务器,并打开JS文件时,我看不到任何编译,只是标准的application.js清单文件:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST …Run Code Online (Sandbox Code Playgroud) 我有一个运行的服务,它包含大约1,000,000个字典的列表,并执行以下操作
myHashTable = {}
myLists = { 'hits':{}, 'misses':{}, 'total':{} }
sorted = { 'hits':[], 'misses':[], 'total':[] }
for item in myList:
id = item.pop('id')
myHashTable[id] = item
for k, v in item.iteritems():
myLists[k][id] = v
Run Code Online (Sandbox Code Playgroud)
所以,如果我有以下词典列表:
[ {'id':'id1', 'hits':200, 'misses':300, 'total':400},
{'id':'id2', 'hits':300, 'misses':100, 'total':500},
{'id':'id3', 'hits':100, 'misses':400, 'total':600}
]
Run Code Online (Sandbox Code Playgroud)
我结束了
myHashTable =
{
'id1': {'hits':200, 'misses':300, 'total':400},
'id2': {'hits':300, 'misses':100, 'total':500},
'id3': {'hits':100, 'misses':400, 'total':600}
}
Run Code Online (Sandbox Code Playgroud)
和
myLists =
{
'hits': {'id1':200, 'id2':300, 'id3':100},
'misses': {'id1':300, 'id2':100, 'id3':400}, …Run Code Online (Sandbox Code Playgroud) 我有一个从pickle文件加载的大数据.数据是包含datetime和int的元组的排序列表
[ (datetime.datetime(2010, 2, 26, 12, 8, 17), 5594813L),
(datetime.datetime(2010, 2, 26, 12, 7, 31), 5594810L),
(datetime.datetime(2010, 2, 26, 12, 6, 4) , 5594807L),
etc
]
Run Code Online (Sandbox Code Playgroud)
我希望根据一些时间间隔获得人口密度.例如,我想在5分钟/ 1分钟/ 30秒的时间内获取记录数.
这样做的最佳方法是什么?我知道我可以遍历列表中的每个实例,但是正在寻找更好的方法(如果存在的话).
期望的输出将是这样的:
2010-01-01 04:10:00 --- 5000
2010-02-04 10:05:00 --- 4000
2010-01-02 13:25:00 --- 3999
Run Code Online (Sandbox Code Playgroud) 在这里,我正在尝试在go命令行应用程序上进行BDD.我正在使用Ginkgo,它包装了testing.go并让你做更具表现力的BDD.https://github.com/onsi/ginkgo
我在阅读stdout以解决问题时遇到了问题.
发现在pkg/testing示例中运行之前存根输出但我无法找到读取该输出的方法:http://golang.org/src/pkg/testing/example.go
这就是我想做的事情:
package cli
import "fmt"
func Run() {
fmt.Println("Running cli")
}
Run Code Online (Sandbox Code Playgroud)
package cli_test
import (
. "github.com/altoros/bosh_deployer_cli/lib/cli"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cli", func() {
It("should parse update stemcell flag", func() {
Run()
Expect(stdout).To(Equal("running cli"))
})
})
Run Code Online (Sandbox Code Playgroud) python ×8
colors ×1
dictionary ×1
ginkgo ×1
go ×1
numpy ×1
python-2.7 ×1
recursion ×1
unit-testing ×1