我有一个字符串列表,我需要通过某个字符的外观来命令它,比方说"+".所以,例如,如果我有这样的列表:
["blah+blah", "blah+++blah", "blah+bl+blah", "blah"]
Run Code Online (Sandbox Code Playgroud)
我需要得到:
["blah", "blah+blah", "blah+bl+blah", "blah+++blah"]
Run Code Online (Sandbox Code Playgroud)
我一直在研究这种sort()方法,但我不完全了解如何将密钥参数用于复杂的订单标准.显然sort(key=count("+"))不起作用.可以像我想要的那样订购列表,sort()还是我需要为它制作一个功能?
我想多次要求用户输入整数、浮点数,有时还输入分数。我一直在阅读文档,但我对存储用户输入的最佳方式感到困惑。我应该使用 read、readline 还是其他?让我们考虑整数:
print("Input an integer: ")
n = read(STDIN,UInt8)
println(n) #returns the ASCII number correspondent
#to the first input character
print("Input an integer: ")
n = parse(UInt8,readline(STDIN))
println(n) #returns the input number correctly
#but I wonder if there is a better way to do it
Run Code Online (Sandbox Code Playgroud) 如果我有一个数字数组:
a = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
并打印它,我得到
[1,2,3]
Run Code Online (Sandbox Code Playgroud)
但如果我有一个元组数组:
b = [(1,2),(3,)]
Run Code Online (Sandbox Code Playgroud)
当我打印它时我得到:
Tuple{Int64,Vararg{Int64}}[(1,2),(3,)]
Run Code Online (Sandbox Code Playgroud)
如何避免打印类型?
在样式指南中,它表示修改其参数的函数应标记为!.但是在给定的示例中,函数double!返回修改后的参数a.如果它已被修改,为什么要返回?有必要吗?
假设我想创建一个按钮,每次点击它都会改变颜色。在构造函数中需要它的起始颜色。
以下代码工作正常,但是当我将鼠标悬停在 TestButton 顶部时,我收到以下消息:“此类(或此类继承自的类)被标记为‘@immutable’,但它的一个或多个实例字段不是最终:TestButton.color”。
如果它应该是最终的但我需要改变它,那么解决方案是什么?如果它仍然有效,为什么它是最终的?
class TestButton extends StatefulWidget {
TestButton({this.color});
Color color;
@override
_TestButtonState createState() => _TestButtonState();
}
class _TestButtonState extends State<TestButton> {
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
setState(() {
widget.color = widget.color == Colors.red ? Colors.blue : Colors.red;
});
},
child: Icon(
Icons.add,
size: 80,
),
color: widget.color,
);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在测试我的代码,我认为这段代码是正确的:
while True:
try:
p = Decimal(raw_input(...))
if ...condition... : break
else: raise ValueError
except ValueError:
print "Error! ..."
Run Code Online (Sandbox Code Playgroud)
但事实并非如此,因为当我输入"a"时,这就是我得到的:
File "multiple.py", line 28, in <module>
precision = Decimal(raw_input(...))
File "/usr/lib/python2.7/decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'a'
Run Code Online (Sandbox Code Playgroud)
ValueError不会捕获InvalidOperation.我不希望程序停止因为这个,我希望它一直要求输入,直到它满足条件.我该怎么做?
最近我一直在玩 Python 以发现它的潜力,我刚刚偶然发现了 SimpleHTTPServer。
我在 Windows 10 上。
我跑:
python -m SimpleHTTPServer
Run Code Online (Sandbox Code Playgroud)
输出是:
Serving HTTP on 0.0.0.0 port 8000 ...
Run Code Online (Sandbox Code Playgroud)
我在智能手机和平板电脑上都打开了浏览器,但是当我输入“ http://127.0.0.1:8000 ”时,它们都无法连接到服务器。
(翻译自意大利语,可能不是准确的翻译)
iPad:“Safari 无法打开页面,因为服务器已停止响应”
Android:“网页没有响应。该网页可能暂时不可用,或者可能已移至其他地址”
为什么它不起作用?我该如何解决?
我有一个script.jl看起来或多或少像这样:
...
function main()
a::... = function1()
b::... = function2(...)
c::... = function3(...)
A::... = function4(...)
B::... = function5(...)
C::... = function6(...)
end
main()
Run Code Online (Sandbox Code Playgroud)
我不能,@time main()因为函数1-3是输入函数,因此它们的执行时间取决于用户的速度或速度.有没有办法让时间只有4-6功能?我不知道,这样的事情:
...
function main()
a::... = function1()
b::... = function2(...)
c::... = function3(...)
@time(
A::... = function4(...)
B::... = function5(...)
C::... = function6(...)
)
end
main()
Run Code Online (Sandbox Code Playgroud) 我的程序很耗费内存,所以我需要尽可能多地保存内存.为变量分配整数值时,值的类型将始终为Int64,无论是0还是+ 2 ^ 63-1或-2 ^ 63.我找不到一种有效分配内存的智能方法,所以我编写了一个看起来像这样的函数(在本例中为整数):
function right_int(n)
types = [Int8,Int16,Int32, Int64, Int128]
for t in reverse(types)
try
n = t(n)
catch InexactError
break
end
end
n
end
a = right_int(parse(Int,eval(readline(STDIN))))
Run Code Online (Sandbox Code Playgroud)
但我不认为这是一个很好的方法.
我还有一个相关的问题:什么是一种有效的数字操作方式而不用担心typemins和typemaxs?将每个操作数转换为BigInt然后应用right_int?
A = collect(reshape(1:16, 4, 4))
4×4 Array{Int64,2}:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Run Code Online (Sandbox Code Playgroud)
怎么有必要collect?不能reshape自动输出4x4 Array{Int64, 2}?在哪些情况下我需要一个Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}?