首页 \ 问答 \ 更改一个列表改变了另一个列表(Changing one list changed the other list)

更改一个列表改变了另一个列表(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
更新时间:2023-02-19 20:02

最满意答案

(我不知道我是否应该得到答案,因为如果没有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是一个内联函数,其值为_ctime64time_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 why makedefs.exe crashes is because ctime returns NULL. Normally you wouldn't expect ctime 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 using long instead of time_t to store time values in. This isn't too surprising. Nethack is really old code, and originally Unix stored its time in long values, using time_t for time values came later. Nethack would've had to be dealing with old systems that didn't have time_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 for ctime itself, just _ctime32 and _ctime64 gives us a clue. If time_t is a 64-bit type, then using long instead will be a problem. On Windows long is only 32-bits, so it would mean ctime 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 and time_t is equivalent to __time64_t. If you need to force the compiler to interpret time_t as the old 32-bit time_t, you can define _USE_32BIT_TIME_T. Doing this will cause ctime 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 platforms

Now 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's time.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 ...
  • 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 ...
  • 我很笨,链接的文件是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 ...
  • 该行表示一个非常简单的编译。 它用一个标准参数编译文件(-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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)