可以使用以下代码创建列表:
>>> [i+1 for i in(0,1,2)]
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
元组可以做类似的事情吗?
>>> (i+1 for i in(0,1,2)),
(<generator object <genexpr> at 0x03A53CF0>,)
Run Code Online (Sandbox Code Playgroud)
我会期望(1, 2, 3)作为输出。
我知道您可以做tuple(i+1 for i in(0,1,2)),但是既然您可以做[i+1 for i in(0,1,2)],我希望元组也可以做类似的事情。
我正在用python编写程序,并希望能够写入二进制文件中的特定字节.我尝试在shell中使用包含数字0到15的小二进制文件来执行此操作,但我无法弄清楚如何执行此操作.下面是我刚刚在shell中输入的代码,其中包含用于演示我要执行的操作的注释:
>>> File=open("TEST","wb") # Opens the file for writing.
>>> File.write(bytes(range(16))) # Writes the numbers 0 through 15 to the file.
16
>>> File.close() # Closes the file.
>>> File=open("TEST","rb") # Opens the file for reading, so that we can test that its contents are correct.
>>> tuple(File.read()) # Expected output: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, …Run Code Online (Sandbox Code Playgroud) 我一直在使用 tkinter 在 python 中编码,但遇到了 tinter 库中的异常。
由于我不知道错误的确切含义,因此我很难调试我的程序。我想知道是否有人可以概述错误并提出可能导致它的原因,以便我知道在调试我的程序时要查找什么。_tkinter.TclError: invalid command name ".entry#"(散列代表一个数字)。
我有一张图像显示通过我的程序的导航,如果有帮助会导致错误。

基本上,该程序只会让我在“记录”部分中查看一次子菜单,每次运行它时,它应该让我根据需要多次输入它们。