Python中的内存高效变量类型(Memory efficient variable types in Python)
我是python中的新手,我有两个关于内存的问题。
第一:
我可以为每个变量准确存储2位还是比int更少的内存? 我的程序只适用于值0,1,2,3,我想节省内存。
getsizeof(1) //12 bytes getsizeof('A') //22 bytes, but I expected 2 bytes as char :(
由于2位,每个变量需要12或22个字节。 有可能在pyhton中解决这个问题吗?
第二个类似问题:
我尝试实现树数据结构,其中每个节点有4个正确顺序的子节点。 例如理想像dict:
node = {1: None, 2: None, 3: None, 4: None} getsizeof(node) //57 bytes
或者在数组中:
node = [None, None, None, None] getsizeof(node) //52 bytes, where getsizeof([]) = 36 bytes
实际上,每个节点都将无用地包含36个字节。 它是~3.35Gb,有1 000 000 000个节点。 有什么方法可以节省内存吗?
I am completely new in python and I have two questions about memory.
First:
Can I store exactly 2 bits per variable or with less memory than int? My program works only with values 0, 1, 2, 3 and I would like to save memory.
getsizeof(1) //12 bytes getsizeof('A') //22 bytes, but I expected 2 bytes as char :(
Due to 2 bits, I need 12 or 22 bytes for each variable. Is possible solve this problem in pyhton?
Second similar problem:
I try to implement tree data structure, where each node have 4 children in correct order. For example ideal like dict:
node = {1: None, 2: None, 3: None, 4: None} getsizeof(node) //57 bytes
or in array:
node = [None, None, None, None] getsizeof(node) //52 bytes, where getsizeof([]) = 36 bytes
In fact, each node will contain 36 bytes uselessly. It is ~3.35Gb with 1 000 000 000 nodes. Is there any way to save memory?
原文:https://stackoverflow.com/questions/33248490
最满意答案
您正在添加一个对象的属性 -
newArr
到另一个 -game[0]
,您可以使用Object.assign()
。var game = [{ 'userType': 'VIP', 'data': [{ 'name': 'John' }] }, { 'userType': 'VIP', 'data': [{ 'name': 'Michelle' }] }]; var newArr = {isActive: true}; Object.assign(game[0], newArr); console.log(game)
You are adding properties of one object -
newArr
to another -game[0]
and for that you can useObject.assign()
.var game = [{ 'userType': 'VIP', 'data': [{ 'name': 'John' }] }, { 'userType': 'VIP', 'data': [{ 'name': 'Michelle' }] }]; var newArr = {isActive: true}; Object.assign(game[0], newArr); console.log(game)
相关问答
更多-
你有一个包含一个元素的数组: var tabelle : [[Actions]] = [[]]这就是tabelle [0]工作的原因。 在使用tabelle[1]之前,需要将另一个数组附加到tabelle You have an array with one element: var tabelle : [[Actions]] = [[]] That is why tabelle[0] is working. You need to append another array to tabelle befor ...
-
您正在添加一个对象的属性 - newArr到另一个 - game[0] ,您可以使用Object.assign() 。 var game = [{ 'userType': 'VIP', 'data': [{ 'name': 'John' }] }, { 'userType': 'VIP', 'data': [{ 'name': 'Michelle' }] }]; var newArr = {isActive: true}; Object.a ...
-
javascript:object属性是数组,但是append不起作用(javascript: object property is array, but append isn't working)[2023-08-07]
Array.push : var objs = { 'prop': [] } objs['prop'].append('q'); 应该: var objs = { 'prop': [] } objs['prop'].push('q'); Array.push: var objs = { 'prop': [] } objs['prop'].append('q'); should be: var objs = { 'prop': [] } objs['prop'].push('q' ... -
与您访问它的方式类似 var table = {"abc":{0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}}; console.log(table["abc"][1]); table["xyz"] = {0:0, 1:5, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0}; console.log(table); Similar with the way you access it var table = {"abc":{0: ...
-
你可以用许多不同的方式来做到这一点,例如: 使用concat timeSeries.values = timeSeries.values.concat(plotArray); 使用lodash的 concat函数 timeSeries.values = _.concat(timeSeries.values, plotArray); 使用ES6 扩展算子 : timeSeries.values = [...timeSeries.values, ...plotArray]; 要么 timeSeries.valu ...
-
尝试这个: ... ], "rows": dataJson.map(function(row) {return {c:row};}) }; Try this: ... ], "rows": dataJson.map(function(row) {return {c:row};}) };
-
根据您当前的数据结构,它应该是: $scope.todayChartConfig.series[0][0].data.push( { name: "Internet Explorer", y: 56.33 }, { name: "Chrome", y: 30.12 } ); 哪个应该有效,因为: $scope.chartConfig.series.push( [{ name: "Bran ...
-
如果你仔细看一下这个操作,这很简单。 你有一个对象,假设$scope.newsList = {} $scope.newsList = { "status": "ok", "count": 2, "count_total": 4231, "pages": 2116, "posts": [ { "id": 62296 }, { "id": 62297 }, ...
-
看起来你没有在任何地方初学化学生。 一定要这样做。 对于记录,如果数组为空,则array.count应为0,而不是nil。 如果你看到nil意味着数组可能是nil(也就是你没有初始化它)。 Doesn't look like you are initializing studentsArray anywhere. Make sure to do that. For the record, array.count should be 0 if the array is empty, not nil. If y ...
-
在对象数组的开头添加数组 - Javascript(Append array at the beginning of the array of objects - Javascript)[2022-10-29]
我认为你想要的可能是: this.setState({ lists: [result].concat(this.state.lists), }); 但如果您使用的是ES6,那么还有更好的方法: this.setState({ lists: [result, ...this.state.lists], }); I think that what you want is probably: this.setState({ lists: [result].concat(this.sta ...