在python 3中,这有效:
Celsius = [39.2, 45.2, 37.3, 37.8, 89]
Fahrenheit = map(lambda x: ((float(9/5))*x + 32).__round__(3), Celsius)
print(list(Fahrenheit))
[102.56, 113.36, 99.14, 100.04, 192.2]
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,为什么?:
Fahrenheit = map(lambda x: ((float(9/5))*x + 32).round(3), Celsius)
Run Code Online (Sandbox Code Playgroud)
此外,现在很多python脚本都不使用这种旧格式__round__,我认为这样可以使脚本看起来更干净.还有其他核心原因吗?
在下面的循环中,最初lines_hp[0]不是NoneType. 但经过一些操作后,它会变成NoneType,我想在发生这种情况时打破循环。经过一些循环迭代后,lines_hp[0]更改为NoneType,但是当我想使用 来识别它时lines_hp[0] is not None,我收到错误:'NoneType' object has no attribute '__getitem__'。我怎样才能解决这个问题?
while lines_hp[0] is not None:
print lines_hp[0] is not None
#some operation to change lines_hp[0]
Run Code Online (Sandbox Code Playgroud)
我已经了解了许多有关如何检测 NoneType 项目的问题和答案,但我仍然收到上述错误。
我已经声明了一个类型:
type Foo = (Char, Char, Char)
Run Code Online (Sandbox Code Playgroud)
并且希望能够解析一个 3 个字母的字符串“ABC”以生成一个输出 Foo,其中每个 ABC 作为类型的三个属性。
我目前的尝试是;
parseFoo :: String ? Maybe Foo
parseFoo str = f where
f (a, _, _) = str[0]
f (_, b, _) = str[1]
f (_, _, c) = str[2]
Run Code Online (Sandbox Code Playgroud)
这是返回错误:
Illegal operator ‘?’ in type ‘String ? Maybe Foo’
Use TypeOperators to allow operators in types
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如何防止编译时出现此错误?
我是否在正确的轨道上?
myElement [] = []
myElement [h] = [h]
myElement (h:t)
| h == myElement t = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
大家好,我正在努力创建自己的elem函数,因为我不允许在 Haskell 中使用任何预定义的函数来做作业。我敢打赌我的代码实际上不起作用,但我想不出其他方式。
这是代码:
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . $i+1 . ' = '. $i*$x . ' <br>';
}
?>
Run Code Online (Sandbox Code Playgroud)
我的预期输出为:100 y 1 = 0
但实际结果是:101 = 0;
你去哪儿了?
O(3 log n)的时间复杂度是否等于或重写为O(n log 3)?这里,对数基数为2.
函数f由规则定义
写一个函数f(n),f通过迭代过程计算
我写了这个.并且仍然没有做对.请告知如何解决它.
def f(n):
if (n<3):
return n
else:
for x in range (n):
a = f(n-1) + 2*f(n-2) + 3*f(n-3)
return (a)
Run Code Online (Sandbox Code Playgroud) 此代码返回从2开始的Integer的第一个因子,或者如果它是素数则不返回任何内容.
示例:firstFactorOf 24返回"Just 2"
示例:firstFactorOf 11返回"Nothing"
我的问题是,如果有一个因子,我将如何返回值2而不是"Just 2",如果没有因子,我将如何返回值x.
firstFactorOf x
| m == Nothing = m
| otherwise = m
where m =(find p [2..x-1])
p y = mod x y == 0
//RETURNS:
ghci> firstFactorOf 24
Just 2
ghci> firstFactorOf 11
Nothing
Run Code Online (Sandbox Code Playgroud) 我听说Haskell变量是不可变的,但我能够重新分配和更新变量值

分类 n:根据以下模式返回基于数字等级的字母等级:
这是我的 Haskell 代码:
classify n
| n => 90 = 'A'
| 89 <= n <= 70 = 'B'
| 69 <= n <= 50 = 'C'
| otherwise = 'D'
Run Code Online (Sandbox Code Playgroud)
但它不断给我错误