更改一个列表改变了另一个列表(Changing one list changed the other list)
当我更改了一个列表时,另一个列表也发生了变化。
a = [[1, 1], [2, 2], [3, 3]] b = list(a) a[0][0] = 100 print(a) print(b)
当我这样做时,a是
[[100, 1], [2, 2], [3, 3]]
。 b也是[[100, 1], [2, 2], [3, 3]]
做
b = a[:]
也不能修复它。为什么会发生?
When I changed one list, the other one also changed.
a = [[1, 1], [2, 2], [3, 3]] b = list(a) a[0][0] = 100 print(a) print(b)
When I do this, a is
[[100, 1], [2, 2], [3, 3]]
. b is also[[100, 1], [2, 2], [3, 3]]
Doing
b = a[:]
doesn't fix it either.Why does this happen?
原文:https://stackoverflow.com/questions/41605086
最满意答案
(我不知道我是否应该得到答案,因为如果没有Harry Johnston和indiv的评论,我不会意识到问题是什么,但我会尝试将评论扩展为完整答案。)
正如
indiv
解释的那样,makedefs.exe
崩溃的原因是因为ctime
返回NULL。 通常你不会指望ctime
这样做,所以我们需要查看文档,找出它会在什么情况下返回错误。 由于MinGW用于编译,我们需要查看Microsoft的Visual C ++文档。 这是因为MinGW没有它自己的C运行时,它只使用微软的。查看
ctime
的Visual Studio C运行时库参考条目,我们发现:回报价值
指向字符串结果的指针。 如果出现以下情况,将返
- 时间表示1970年1月1日午夜之前的一个日期。
- 如果使用
_ctime32
或_wctime32
,则时间表示2038年1月19日03:14:07之后的日期。- 如果您使用
_ctime64
或_wctime64
,则time表示在UTC,12月31日23:59:59之后的日期。现在,假设原始海报没有将系统时钟设置为远期或过去很长时间,这是非常安全的。 那么为什么
ctime
会使用错误的时间呢? Harry Johnston指出代码使用long
而不是time_t
来存储时间值。这并不太令人惊讶。 Nethack实际上是旧代码,最初Unix以long
值存储它的时间,使用time_t
作为时间值后来出现。 Nethack将不得不处理那些没有时间的旧系统,以便在其活跃的开发周期中占据重要位置。这就解释了为什么Nethack源使用了错误的类型,但它并没有完全解释为什么它将错误的值传递给
ctime
。 事实上我们没有看到ctime
本身的返回值的描述,只是_ctime32
和_ctime64
给了我们一个线索。 如果time_t
是64位类型,那么使用long
代替将是一个问题。 在Windows上,long
只有32位,所以它意味着ctime
传递的是一个部分时间值的数字,一部分是随机位。 阅读文档证实了这种情况,并为我们提供了一个可能的解决方案:
ctime
是一个内联函数,其值为_ctime64
,time_t
等于__time64_t
。 如果需要强制编译器将time_t
解释为旧的32位time_t
,则可以定义_USE_32BIT_TIME_T
。 这样做会导致ctime
评估为_ctime32
。 建议不要这样做,因为您的应用程序可能在2038年1月18日之后失败,并且在64位平台上不允许现在定义
_USE_32BIT_TIME_T
只影响C头的编译方式,而且由于MinGW提供了自己的C头,MinGW可能不支持这个。 快速检查MinGW的time.h
发现它确实存在,因此使用-D_USE_32BIT_TIME_T
编译器选项来定义此宏的简单解决方案。(I don't know if I deserve credit for the answer, since I wouldn't have been aware of what the problem was without Harry Johnston's and indiv's comments, but I'll try to expand on the comments into a full answer.)
As
indiv
explained, the reason whymakedefs.exe
crashes is becausectime
returns NULL. Normally you wouldn't expectctime
to do this, so we need to check the documentation find out under what circumstances it will return an error. Since MinGW is being used to do the compiling we need to look at Microsoft's Visual C++ documentation. This is because MinGW doesn't have it's own C runtime, it just uses Microsoft's.Looking at at the Visual Studio C Run-Time Library Reference entry for
ctime
we find:Return Value
A pointer to the character string result.
NULL
will be returned if:
- time represents a date before midnight, January 1, 1970, UTC.
- If you use
_ctime32
or_wctime32
and time represents a date after 03:14:07 January 19, 2038.- If you use
_ctime64
or_wctime64
and time represents a date after 23:59:59, December 31, 3000, UTC.Now it's pretty safe to assume the original poster hasn't set his system clock to a time far into the future or long in the past. So why would
ctime
be using the wrong time? Harry Johnston pointed out that the code was usinglong
instead oftime_t
to store time values in. This isn't too surprising. Nethack is really old code, and originally Unix stored its time inlong
values, usingtime_t
for time values came later. Nethack would've had to be dealing with old systems that didn't havetime_t
for a significant chunl of its active development period.That explains why the Nethack source is using the wrong type, but it doesn't quite explain why it's passing the wrong value to
ctime
. The fact we don't see a description of the return value forctime
itself, just_ctime32
and_ctime64
gives us a clue. Iftime_t
is a 64-bit type, then usinglong
instead will be a problem. On Windowslong
is only 32-bits, so it would meanctime
is being passed a number that's one part time value, one part random bits. Reading on in the documentation confirms this is the case, and gives us a possible solution:
ctime
is an inline function which evaluates to_ctime64
andtime_t
is equivalent to__time64_t
. If you need to force the compiler to interprettime_t
as the old 32-bittime_t
, you can define_USE_32BIT_TIME_T
. Doing this will causectime
to evaluate to_ctime32
. This is not recommended because your application may fail after January 18, 2038, and it is not allowed on 64-bit platformsNow since defining
_USE_32BIT_TIME_T
only affects how the C headers are compiled, and since MinGW supplies it's own C headers, it's possible that MinGW doesn't support this. A quick check of MinGW'stime.h
reveals that it does, so the simple solution to use the-D_USE_32BIT_TIME_T
compiler option to define this macro.
相关问答
更多-
我做了一些检查...... NEARDATA在config1.h定义,仅用于AmigaOS平台。 在这种情况下,它意味着编译器相对于CPU寄存器引用了数据段(其中存储了全局变量和静态变量)。 STATIC_*定义也似乎是平台相关的。 所以这就是所有平台特定的事情,使用预处理器#define构造来定义,以确保源代码在不同的平台上构建。 I did a little checking... NEARDATA is defined in config1.h, and is only used on the Ami ...
-
我使用以下方法在Windows 7上为C ++编译clang,并且已经由Mysticial和其他人验证: 下载并安装MinGW (确保您安装C ++编译器)并将bin文件夹放入PATH(我有MinGW 4.6.1并在4.6.2的其他计算机上成功测试) 确保你的PATH中有Python ( 不是3 ,我有2.7) (可选:确保您的PATH中有Perl(我使用ActivePerl 5.14.2 64位)) 获取CMake并将其放在PATH中 转到LLVM下载页面,并下载LLVM 3.0源代码以及Clang源代码 ...
-
至于今天(2011-04-05),你将需要最新的版本: 柔性2.5.4a-的1.exe 野牛-2.4.1-SETUP.EXE 之后,完全安装在您的首选项目录中,名称中没有空格 。 我建议C:\GnuWin32 。 不要将它安装在默认值 (C:\ Program Files(x86)\ GnuWin32)中,因为bison在目录名称中有空格问题,而不是括号。 另外,考虑将Dev-CPP安装在默认目录( C:\Dev-Cpp ) 之后,将PATH变量设置为包含gcc (在C:\Dev-Cpp\bin )和fle ...
-
容易! 他们在Macintosh服务器上编译和运行代码,并在基于Web的应用程序中提供结果。 你没有运行代码,他们是。 你只需看到最终结果。 Easily! They compile and run code on a Macintosh Server on their end, and just feed you the result in a web-based app. You're not running the code, they are. You just get to see the end ...
-
我如何在Windows上编译和运行Atom中的Java类?(How can I compile and run Java classes in Atom on Windows?)[2022-04-20]
Atom不是问题。 您的JDK安装不正确。 JDK不在Windows PATH上,所以它找不到javac 。 请运行安装程序让它为您修复PATH,或手动将JDK自行添加到PATH 。 This isn't a problem with Atom. You have the JDK installed incorrectly. The JDK isn't on the Windows PATH, so it can't find javac. Either run the installer to let i ... -
在Windows中编译Go文件?(Compile Go file in Windows?)[2023-05-14]
我很笨,链接的文件是32位,我使用“6g”编译,因为我的系统是64位。 “8g”可以编译。 “8g myfile.go” “8l myfile.8” “myfile.out” 努力让它运行。 I'm dumb, the file linked is 32 bit and I was using "6g" to compile because my system is 64 bit. "8g" works to compile. "8g myfile.go" "8l myfile.8" "myfile.out ... -
如何在Windows上进行编译(How to compile this on Windows)[2023-08-22]
该行表示一个非常简单的编译。 它用一个标准参数编译文件(-g用于编译调试符号,在MSVC上是/ Zi)。 但它与很多库链接(这是所有的-l选项)。 我将其中的两个识别为标准压缩库(bz2和z),因此您需要首先构建这些库。 The line indicates a very simple compile. It's compiling the file with one standard argument (-g for compiling with debug symbols, on MSVC it's / ... -
.lib文件是 Windows二进制文件。 它是一个静态库文件,并由应用程序中的链接器用于静态链接(即在外行术语中使用)库。 由于libevent2不是一个程序,你不会得到一个.exe输出 - 它将是一个大的.dll +小.lib(用于动态链接)或一个大的.lib(用于静态链接)。 A .lib file is a Windows binary file. It's a static library file, and is used by the linker in your application to ...
-
(我不知道我是否应该得到答案,因为如果没有Harry Johnston和indiv的评论,我不会意识到问题是什么,但我会尝试将评论扩展为完整答案。) 正如indiv解释的那样, makedefs.exe崩溃的原因是因为ctime返回NULL。 通常你不会指望ctime这样做,所以我们需要查看文档,找出它会在什么情况下返回错误。 由于MinGW用于编译,我们需要查看Microsoft的Visual C ++文档。 这是因为MinGW没有它自己的C运行时,它只使用微软的。 查看ctime的Visual Stud ...
-
我不确定“下载”是什么意思,但你需要做的就是pkg install nethack (作为root角色)然后以任何用户身份运行它。 根据清单二进制文件应该在/usr/bin/nethack ,所以输入: /usr/bin/nethack 在你的终端窗口应该做。 /usr/bin很可能在你的PATH所以你可以输入 nethack 和游戏应该开始。 I am not sure what you mean by "downloaded" but everything you need to do is pkg ...