我有这个工作函数,它计算列表中每个元素的出现次数并从中构建一个映射:
import qualified Data.HashMap.Strict as M
counter :: [Int] -> M.HashMap Int Int
counter = foldr (\x -> M.insertWith (+) x 1) mempty
Run Code Online (Sandbox Code Playgroud)
现在我想将其概括为:
counter :: (Eq k) => [k] -> M.HashMap k Int
counter = foldr (\x -> M.insertWith (+) x 1) mempty
Run Code Online (Sandbox Code Playgroud)
但显然,我也需要Hashable k
类型约束,因为M.insertWith
需要它。我尝试了多种添加约束的方法,但在所有尝试中都失败了1。
是否有一种奇特的方式来添加类型约束或可能对我有帮助的语言编译指示?
完整的错误信息:
• Could not deduce (hashable-1.3.1.0:Data.Hashable.Class.Hashable
k)
arising from a use of ‘M.insertWith’
from the context: Eq k
bound by the type signature for:
counter :: forall …
Run Code Online (Sandbox Code Playgroud) 我有两个嵌套的嵌套列表,如下所示:
list_1 = [[100, 90, 90, 85, 70], [100, 90, 90, 85, 80], [105, 100, 90, 90, 85]]
list_2 = [[1, 2, 2, 3, 4], [1, 2, 2, 3, 4], [1, 2, 3, 3, 4]]
Run Code Online (Sandbox Code Playgroud)
我想使用list_1
with中的元素list_2
来创建一个字典,但它需要采用嵌套列表的形式,输出应该是这样的:
[{100:1,90:2,90:2,85:3,70:4},{100:1,90:2,90:2,85:3,80:4},{105:1,100:2,90:3,90:3,85:4}]
Run Code Online (Sandbox Code Playgroud)
在Python 3中有没有办法做到这一点?
我有这个QML进度栏:
import QtQuick.Controls 2.0 as QQC20
Item {
QQC20.ProgressBar {
id: progressbar_id
visible: false // even if "true", the progress bar does NOT show up on UI
from: editorScene.progressbarMin
to: editorScene.progressbarMax
value: editorScene.progressbarVal
onValueChanged: {
console.log("Progressbar value changed: ", progressbar_id.value)
}
onVisibleChanged: {
console.log("Progressbar visibility chanaged: ", progressbar_id.visible)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以确认进度条的值和可见性已被方法onValueChanged
和更改onVisibleChanged
。
但是,问题在于进度条没有显示在UI上!我实际上如何在UI上显示进度栏?有人可以给我提示吗?
我想知道,为什么我们使用count++
而不是,例如count += 0
,计算偶数的数量?
#include <iostream>
using namespace std;
int main()
{
int count = 1;
for (int i = 0; i <= 100; i++)
{
if (i % 2 == 0)
count += 0; // why it will give me only 1? as output
else
continue;
}
cout << "num of even: " << count << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何使用自定义版本的 fold(基本上用作 foldl)在 Haskell 中创建自定义解压缩函数,但我被卡住了。我可以得到它
unzip' :: [(a,b)] -> ([a],[b])
unzip' = fold (\x->([x!!0],[x!!1])) ([],[])
Run Code Online (Sandbox Code Playgroud)
但这会出错:
• Couldn't match expected type ‘[a]’
with actual type ‘(Integer, Integer)’
• In the first argument of ‘tail’, namely ‘(1, 2)’
In the expression: tail (1, 2)
In an equation for ‘it’: it = tail (1, 2)
• Relevant bindings include
it :: [a] (bound at <interactive>:114:1)
Run Code Online (Sandbox Code Playgroud)
据我所知,x
是,(1,2)
但我不确定如何将其进一步拆分为 1 和 2。这是我正在使用的 fold 函数:
fold :: (a -> b -> b) …
Run Code Online (Sandbox Code Playgroud) 我想使用嵌套循环创建一个二维数组。这段代码有什么问题?
import numpy
b = np.array([])
for i in range(2):
for j in range(5):
b[i][j]=i+j
print(b)
Run Code Online (Sandbox Code Playgroud) 我正在学习python,并开始在继承中学习MRO。
我已经阅读了super的用法及其使用方式,但是我无法对以下问题提出我的理解。如果不是超级,我知道B被调用。D-> B-> C-> A,但是当添加超级时,我不知道为什么输出是这样的
class A:
def test(self):
print("test of A called")
class B(A):
def test(self):
print("test of B called")
super().test()
class C(A):
def test(self):
print("test of C called")
super().test()
class D(B, C):
def test2(self):
print("test of D called")
obj=D()
obj.test()
Run Code Online (Sandbox Code Playgroud)
输出量
test of B called
test of C called
test of A called
Run Code Online (Sandbox Code Playgroud)
请帮助我了解super在这里的功能。
我正在使用 light gbm 进行一些机器学习任务。
我想使用提前停止来找到给定许多超参数的最佳树数。然而,lgbm 停止种植树木,同时仍在改进我的评估指标。
下面我附上了我的规格:
params = {
'max_bin' : [128],
'num_leaves': [8],
'reg_alpha' : [1.2],
'reg_lambda' : [1.2],
'min_data_in_leaf' : [50],
'bagging_fraction' : [0.5],
'learning_rate' : [0.001]
}
mdl = lgb.LGBMClassifier(n_jobs=-1, n_estimators=7000,
**params)
mdl.fit(X_train, y_train, eval_metric='auc',
eval_set=[(X_test, y_test)], early_stopping_rounds=2000,
categorical_feature=categorical_features, verbose=5)
Run Code Online (Sandbox Code Playgroud)
lgbm 得出结论,0.7326 上的 auc 不优于 0.70995 并停止。我究竟做错了什么?
我正在制作一个蛇游戏。每当我按箭头键向一个方向移动然后向另一个方向按一个键时,蛇就会对角移动。(例如,如果我先按向右然后向上按。)即使释放了前一个键,也会发生这种情况。我怎么能阻止这个?
# x and y marks the player's position
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
if event.key == pygame.K_UP:
y_change = -10
if event.key == pygame.K_DOWN:
y_change = 10
x += x_change
y += y_change
Run Code Online (Sandbox Code Playgroud) 我正在尝试1-65535
使用URL 验证URL 内的端口号(范围)urlparse
.
棘手的部分是如果URL中没有端口号,urlparse(url).port
则标识为NoneType
因此,我尝试根据数据类型进行简单的比较,但实际上并没有像您在本示例中看到的那样工作.
如果我NoneType
用作数据类型,
elif type(u.port) == NoneType:
Run Code Online (Sandbox Code Playgroud)
我收到了错误
NameError: name 'NoneType' is not defined
Run Code Online (Sandbox Code Playgroud)
我没有使用,validators
因为它无法正确验证端口号.
Python:`validators.url`不接受1-9的端口号,但接受大于65535的端口?
TCP/UDP端口号范围从1-65535开始.但是,validators
无法识别端口1-9
并仍然接受大于的无效端口65535
.
码
from urllib.parse import urlparse
def test(url):
global u
u = urlparse(url)
print(' Port : ', u.port)
print(' Data Type : u.port %s\n'% type(u.port))
for url in ['example.com', 'example.com:1', 'http://example.com:1']:
print(url)
test(url)
if type(u.port) == int:
print(' Integer')
elif type(u.port) == None:
print(' …
Run Code Online (Sandbox Code Playgroud) python ×6
haskell ×2
python-3.x ×2
boolean ×1
c++ ×1
dictionary ×1
fold ×1
function ×1
hashmap ×1
if-statement ×1
lightgbm ×1
list ×1
loops ×1
progress-bar ×1
pygame ×1
qml ×1
qt ×1
sprite ×1
unzip ×1