假设我有一个list[str]包含时间戳"HH:mm"格式的对象,例如
timestamps = ["22:58", "03:11", "12:21"]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为list[int]每个时间戳的“自午夜以来的分钟数”值的对象:
converted = [22*60+58, 3*60+11, 12*60+21]
Run Code Online (Sandbox Code Playgroud)
...但我想以风格来完成它并使用单个列表理解来完成它。我天真的构建的(语法不正确的)实现类似于:
def timestamps_to_minutes(timestamps: list[str]) -> list[int]:
return [int(hh) * 60 + int(mm) for ts in timestamps for hh, mm = ts.split(":")]
Run Code Online (Sandbox Code Playgroud)
...但这不起作用,因为这for hh, mm = ts.split(":")不是有效的语法。
写同样的东西的有效方式是什么?
澄清一下:我可以看到一个形式上令人满意的解决方案,其形式为:
def timestamps_to_minutes(timestamps: list[str]) -> list[int]:
return [int(ts.split(":")[0]) * 60 + int(ts.split(":")[1]) for ts in timestamps]
Run Code Online (Sandbox Code Playgroud)
...但是这是非常低效的,我不想将字符串分割两次。
我正在调试 FastAPI 应用程序,并且遇到了与本文中提到的问题类似的问题:asyncio.wait_for对应该超时的调用却没有超时:
try:
await wait_for(completion_event.wait(), 1.0)
except TimeoutError:
logging.info("timeout")
return SubmissionResult(post_id=post_id, language_check_pending=True)
Run Code Online (Sandbox Code Playgroud)
此代码片段是 FastAPI 的 POST 请求处理程序的一部分。这里,completion_event是一个asyncio.Event对象。我可以在带有 的行上放置一个断点wait_for,观察它卡住超过 1 秒的时间,然后直接移过该except块。毫无疑问,这wait_for并没有达到预期的效果。
我不知道为什么它会这样。此时,我开始怀疑 FastAPI 的内部结构,因为它使用uvloop作为“更快的异步替代品”。但我不知道如何检验这个假设,更不知道如何解决这个问题(如果确实如此)。
有什么建议么?
我正在使用标记为x86_64-8.1.0-posix-seh-rt_v6-rev0. 运行时g++ --version,我看到:
g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这个版本的 g++ 应该默认生成 64 位二进制文件。
编译命令是这样的:
g++.exe -std=c++17 -g main.cpp -o main.exe
Run Code Online (Sandbox Code Playgroud)
但是,如果main.cpp看起来像这样:
#include <iostream>
int main() {
std::cout << sizeof(long);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印4而不是8. 我尝试使用-m64编译器标志,但没有任何改变。
我做错了什么,如何解决这个问题?