我正在为一个情节添加一个文本字段.要确定字母的大小,我使用fontsize参数:
import matplotlib.pyplot as plt
r = plt.Rectangle((2,2), 10, 10, fill = False)
plt.gca().add_patch(r)
plt.text(7, 7, 'my rectangle', fontsize = 12, ha='center', va='center')
plt.axis(xmin = 0, xmax = 14, ymin = 0, ymax = 14)
Run Code Online (Sandbox Code Playgroud)
我想文本'my rectangle'正好是2个单位(y轴)高.有没有办法做到这一点?
我正在为vuelidate我的组件中的验证编写单元测试.我发现该$touch()方法是异步调用的,所以我需要使用$nextTick()它expect().当我需要两个nextTick()s两个时出现问题expect()s.
describe('Validations', () => {
let data
let myComponent
beforeEach(() => {
data = () => {
propertyABC = 'not allowed value'
}
myComponent = localVue.component('dummy', {template: '<div></div>', validations, data})
it('Properly validates propertyABC', (done) => {
Vue.config.errorHandler = done
let wrapper = mount(myComponent, {localVue})
wrapper.vm.$v.$touch()
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.$v.propertyABC.$error).to.be.true
# fails, because propertyABC === 'allowed value', adn thus $error is false
done()
}
wrapper.vm.propertyABC = 'allowed value'
wrapper.vm.$v.propertyABC.$touch() …Run Code Online (Sandbox Code Playgroud) 让我们假设我有一个整数列表:
mylist = [101, 102, 103, 104, 105, 106]
Run Code Online (Sandbox Code Playgroud)
现在我需要创建每个可能的子列表分区(保留顺序):
sublists = [([101], [102, 103, 104, 105, 106]),
([101, 102], [103, 104, 105, 106]),
([101, 102, 103], [104, 105, 106]),
...
([101, 102], [103, 104], [105, 106]),
...
([101], [102, 103, 104], [105], [106]),
...
([101], [102], [103], [104], [105], [106])]
Run Code Online (Sandbox Code Playgroud)
任何的想法?将itertools是有益的?
我有一个项目由两个这样的包组成:
MyProjectDir
-Package1
--__init__.py
--file1_1.py
--file1_2.py
--file1_3.py
-Package2
--__init__.py
--file2_1.py
--file2_2.py
--file2_3.py
Run Code Online (Sandbox Code Playgroud)
现在,在包中,文件在文件之间有一些导入:
文件2_3.py:
from Package2.file2_1 import *
run_some_code()
Run Code Online (Sandbox Code Playgroud)
当我直接从 PyCharm 运行 file2_3.py 时,一切运行正常。但是当我尝试在终端中运行脚本时(我在 Windows 7 上工作):
D:\SVN Repo\MyProjectDir\Package2> python file2_3.py
Run Code Online (Sandbox Code Playgroud)
或者
D:\SVN Repo\MyProjectDir> python ./Package2/file2_3.py
Run Code Online (Sandbox Code Playgroud)
似乎 python 看不到我的包,我收到一个错误:
Traceback (most recent call last):
File "./Package2/file2_3.py", line 1, in <module>
from Package2.file2_1 import *
ImportError: No module named 'Package2'
Run Code Online (Sandbox Code Playgroud)
是什么原因?
编辑:如果在我使用的导入行中from file2_1.py import *没有包名,IDE 会将导入下划线为“未解析的参考包 2”(尽管它可以运行),并且终端工作......
我有一个 numpy ndarray 实例,但大小可变。
import numpy as np
dimensions = (4, 4, 4)
myarray = np.zeros(shape = dimensions)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到了一个“立方”形状的数组,如果我想索引一个切片,myarray我可以使用它myarray[:][:][0],因为我知道有 3 个维度(我使用 3 对[])。
如果是 4 维,我会使用myarray[:][:][:][0]. 但由于维度的数量可能会改变,所以我不能以这种方式对其进行硬编码。
如何根据维数对此类数组的切片进行索引?看似简单的问题,却想不出任何解决方案。
我正在调试编写代码的一部分,因此我使用大量调用print来检查变量的值.我遇到过一种情况,当打印变量导致Python跳过for循环时.
import numpy as np
import itertools as itr
(...)
class myclass():
def a_function_inside_a_class():
print('-------------')
current_size = self.get_current_size()
current_size[dimension] += 1
new_layer = [range(i) for i in current_size]
new_layer[dimension] = [current_size[dimension]]
print(new_layer)
new_layer = itr.product(*new_layer)
print(list(new_layer)) # THIS LINE CAUSES PROBLEMS
for c in new_layer:
print('for')
print(list(c))
(...)
a_function_that_causes_some_error(c)
Run Code Online (Sandbox Code Playgroud)
我创建一个列表,然后用于itertools创建此列表的组合,然后迭代它们.如果我打电话给a_function_inside_a_class()上面,我没有for打印.没有错误发生.解释器不进入循环.
(...)
-------------
[[2], range(0, 1), range(0, 1)]
[(2, 0, 0)]
-------------
[range(0, 1), [2], range(0, 1)]
[(0, 2, …Run Code Online (Sandbox Code Playgroud) 让我们有一个包含很多输入字段的搜索表单.用户可以点击:
我如何实现这一目标?我只找到了在两个单独的按钮上发出两种类型的POST请求的解决方案.
也许一个JavaScript onclick函数<form method="??">在提交之前设置好了吗?那会有用吗?
python ×5
javascript ×2
combinations ×1
for-loop ×1
forms ×1
html ×1
import ×1
indexing ×1
list ×1
matplotlib ×1
numpy ×1
plot ×1
vue.js ×1
vuelidate ×1
windows ×1