Python Stack Corruption?(Python Stack Corruption?)
我是python的新手(但不是编程),我无法解释以下行为。 似乎一个对象(“child”)中的变量(我的示例中的列表“children”)被一个完全不同的对象(“node”)中的该变量的值覆盖。 为了给出一些上下文,我正在尝试创建一个在树结构中使用的简单Node类。 该节点具有子节点和父节点(所有其他节点)。
我无法弄清楚为什么child.children获得与node.children相同的值。 他们以某种方式引用相同的数据吗? 为什么? 代码和输出如下:
class Node: children = [] parent = 0 visited = 0 cost = 0 position = (0, 0) leaf = 0 def __init__(self, parent, pos): self.parent = parent self.position = pos def addChild(self, node): self.children += [node] node = Node(0, (0,0)) child = Node(node, (3,2)) node.addChild(child) print "node: ", print node print "node.childen: ", print node.children print "child: ", print child print "child.children", print child.children
输出:
node: <__main__.Node instance at 0x414b20> node.childen: [<__main__.Node instance at 0x414b48>] child: <__main__.Node instance at 0x414b48> child.children [<__main__.Node instance at 0x414b48>]
如您所见,node.children和child.children都具有相同的值(包含child的列表),即使我只更新了node.children。 谢谢你的帮助!
I'm relatively new to python (but not to programming), and I can't explain the following behavior. It appears that a variable (the list "children" in my example) from one object ("child") is getting overwritten by the value for that variable in a completely different object ("node"). To give some context, I'm trying to create a simple Node class to use in a tree structure. The node has children and a parent (all other nodes).
I can't figure out why child.children gets the same value as node.children. Are they somehow referencing the same data? Why? Code and output follows:
class Node: children = [] parent = 0 visited = 0 cost = 0 position = (0, 0) leaf = 0 def __init__(self, parent, pos): self.parent = parent self.position = pos def addChild(self, node): self.children += [node] node = Node(0, (0,0)) child = Node(node, (3,2)) node.addChild(child) print "node: ", print node print "node.childen: ", print node.children print "child: ", print child print "child.children", print child.children
Output:
node: <__main__.Node instance at 0x414b20> node.childen: [<__main__.Node instance at 0x414b48>] child: <__main__.Node instance at 0x414b48> child.children [<__main__.Node instance at 0x414b48>]
As you can see, both node.children and child.children have the same value (a list containing child) even though I only updated node.children. Thanks for any help!
原文:https://stackoverflow.com/questions/8627628
最满意答案
第7行(指您的链接代码)。 这是一个简单的错误,在基于0的数组的语言中很常见,通常你必须从大小中减去一个来确定上限。
您声明“Board_Size”为10,但减去1得到9x9矩阵:
#define Board_Size 10 char P1_board [Board_Size - 1] [Board_Size - 1];
这会打印一个10x10矩阵:
printf(“1 |%c |%c |%c |%c |%c |%c |%c |%c |%c |%c | \ n”,P1_board [0] [0],P1_board [0 ] [1],P1_board [0] [2],P1_board [0] [3],P1_board [0] [4],P1_board [0] [5],P1_board [0] [6],P1_board [0] [ 7],P1_board [0] [8],P1_board [0] [9]);
C以行优先顺序存储数组(并且对于9x9矩阵元素A(0,10))与元素A(1,0)位于相同的存储位置,这就解释了为什么您在板上看到多个船只。
另外,C将字符常量视为整数,因此您可以编写(字母“a”)将字母转换为索引。
如果您不熟悉该术语,WIKIpedia对行排序的含义有很好的描述: https : //en.wikipedia.org/wiki/Row-_and_column-major_order
Line #7 (referring to your linked code). It's a simple mistake and a common one in languages with 0-based arrays where you often have to subtract one from the size to determine the upper bound.
You declare "Board_Size" as being 10 but subtract one to get a 9x9 matrix:
#define Board_Size 10 char P1_board[Board_Size - 1][Board_Size - 1];
This prints a 10x10 matrix:
printf(" 1 | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", P1_board[0][0], P1_board[0][1], P1_board[0][2], P1_board[0][3], P1_board[0][4], P1_board[0][5], P1_board[0][6], P1_board[0][7], P1_board[0][8], P1_board[0][9]);
C stores arrays in row-major order (and for a 9x9 matrix element A(0,10) is in the same memory location as element A(1,0), and that explains why you see multiple ships on the board.
As an aside, C treats character constants as integers, so you can write (letter - 'a') to convert a letter to an index.
WIkipedia has a nice description of what row-major order means if you're not familiar with the term: https://en.wikipedia.org/wiki/Row-_and_column-major_order
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在java和大多数语言中,Arrays的索引从零开始。 因此第一个元素是索引0等。 此外,一种可以改进代码的方法是用一个嵌套循环替换前两个循环。 for(int i=0;i第7行(指您的链接代码)。 这是一个简单的错误,在基于0的数组的语言中很常见,通常你必须从大小中减去一个来确定上限。 您声明“Board_Size”为10,但减去1得到9x9矩阵: #define Board_Size 10 char P1_board [Board_Size - 1] [Board_Size - 1]; 这会打印一个10x10矩阵: printf(“1 |%c |%c |%c |%c |%c |%c |%c |%c |%c |%c | \ n”,P1_board [0] [0],P1_bo ...如果您的数据在 char names[100][30]; 然后你不能对“指针”进行排序,因为该数据结构根本没有指针......一个接一个地只有100 * 30 = 3000个字符。 因此,要进行排序,您需要实际移动100行所有内容。 std::sort不能直接使用,因为数据结构是一个数组数组,而数组是C ++二等公民(例如,你不能将数组分配给另一个)。 If your data is in char names[100][30]; then you cannot sort "the pointers" ...
如何改变字符串中某个位置的字符...... Python(How to change characters in a certain position within a string… Python)[2023-01-08]
您可以使用下面代码片段中的逻辑将任何您想要的字符替换为一个随机数字。 from random import randint origin = "MYZR27EE7" origin = origin[:4] + str(randint(0,9)) + origin[5:] print(origin) 结果: # MYZR17EE7 # MYZR67EE7 You can use the logic in the snippet below to swap any character you want wi ...只是一个修正你的循环应该如下所示,因为在第二个循环内你需要循环在你在第一个循环中得到的数组。 for(int j = 0; j您将在声明的某个位置切换初始char的值(名为d1/2/3/4/5 ),而不是更改数组内的值。 tenByTen[d1C][d1R] = "."; 要么.. tenByTen[d2C][d2R] = "."; 等等... 祝你好运! You are switching the value of the initial char, (named d1/2/3/4/5), instead of changing the value inside the array, at one of the positi ...使用 char ch[n+1]; for(int i = 0;i>ch[i]; ch[n] = '\0'; cout< >ch[i]; ch[n] = '\0'; cout< first_character_array = myArray.map{ |e| e[0] }.map{ |e| e.nil? ? "" : e } => ["1", "1", "2", "5", "1", "5"] ... fifth_character_array = myArray.map{ |e| e[4] }.map{ |e| e.nil? ? "" : e } => ["", "5", "", "5", "", "1"] first_character_array = myArray.ma ...相关文章
更多- AI八数码问题 Stack Overflow
- python top project of 2013
- Stack Overflow Architecture Update - Now At 95 Million Page Views A Month
- 一天一道算法题--6.7--stack
- Python:渗透测试开源项目【源码值得精读】
- 探索 Python,第 1 部分: Python 的内置数值类型
- python2和python3的区别
- Python的文件类型
- python的下载与安装
- Python学习笔记
最新问答
更多- h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
- 创建一个功能(Create a function)
- 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
- PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
- Qt冻结循环的原因?(Qt freezing cause of the loop?)
- TableView重复youtube-api结果(TableView Repeating youtube-api result)
- 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
- SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
- 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
- 以编程方式创建UITableView(Creating a UITableView Programmatically)
- 如何打破按钮上的生命周期循环(How to break do-while loop on button)
- C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
- 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
- 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
- Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
- 设置kotlin扩展名(Setting a kotlin extension)
- 如何并排放置两个元件?(How to position two elements side by side?)
- 如何在vim中启用python3?(How to enable python3 in vim?)
- 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
- 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
- Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
- dedecms如何安装?
- 在哪儿学计算机最好?
- 学php哪个的书 最好,本人菜鸟
- 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
- 如何覆盖错误堆栈getter(How to override Error stack getter)
- 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
- USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
- 电脑高中毕业学习去哪里培训
- 正则表达式验证SMTP响应(Regex to validate SMTP Responses)