假设我有一个Python对象的id,我通过这个来检索它id(thing).如何thing通过我给出的身份证号码再次找到?
所以我使用的是Python 2.7,使用该json模块对以下数据结构进行编码:
'layer1': {
'layer2': {
'layer3_1': [ long_list_of_stuff ],
'layer3_2': 'string'
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我使用漂亮的打印方式打印出来,如下所示:
json.dumps(data_structure, indent=2)
Run Code Online (Sandbox Code Playgroud)
哪个好,除了我要缩进所有内容,除了内容"layer3_1"- 这是一个列出坐标的大量字典,因此,在每个上面设置一个值使得漂亮的打印创建一个包含数千行的文件,示例如下:
{
"layer1": {
"layer2": {
"layer3_1": [
{
"x": 1,
"y": 7
},
{
"x": 0,
"y": 4
},
{
"x": 5,
"y": 3
},
{
"x": 6,
"y": 9
}
],
"layer3_2": "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想要的是类似于以下内容:
{
"layer1": {
"layer2": {
"layer3_1": [{"x":1,"y":7},{"x":0,"y":4},{"x":5,"y":3},{"x":6,"y":9}],
"layer3_2": "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我听说可以扩展json模块:是否可以将其设置为仅在"layer3_1"对象内部时关闭缩进?如果是这样,有人请告诉我怎么样?
这是我的代码:
from memory_profiler import profile
@profile
def mess_with_memory():
huge_list = range(20000000)
del huge_list
print "why this kolaveri di?"
Run Code Online (Sandbox Code Playgroud)
当我从解释器运行时,这就是输出:
3 7.0 MiB 0.0 MiB @profile
4 def mess_with_memory():
5
6 628.5 MiB 621.5 MiB huge_list = range(20000000)
7 476.0 MiB -152.6 MiB del huge_list
8 476.0 MiB 0.0 MiB print "why this kolaveri di"
Run Code Online (Sandbox Code Playgroud)
如果你注意到输出,创建巨大的列表消耗621.5 MB,而删除它只需释放152.6 MB.当我检查文档时,我发现了以下声明:
the statement del x removes the binding of x from the namespace referenced by the local scope
Run Code Online (Sandbox Code Playgroud)
所以我猜,它并没有删除对象本身,而只是取消绑定它.但是,它在解除绑定方面做了什么呢?它释放了如此多的空间(152.6 MB) …
如何将python字典列表保存到文件中,每个字典dict将保存在一行中?我知道我可以json.dump用来保存字典列表。但是我只能将列表以紧凑形式保存(一行中的完整列表)或缩进形式,其中为所有字典键添加一个换行符。
编辑:
我希望我的最终json文件如下所示:
[{key1:value,key2:value},
{key1:value,key2:value},
...
{key1:value,key2:value}]
Run Code Online (Sandbox Code Playgroud) 可能重复:
通过id()获取对象?
>>> var = 'I need to be accessed by id!'
>>> address = id(var)
>>> print(address)
33003240
Run Code Online (Sandbox Code Playgroud)
有没有办法使用的地址var在内存中,所提供的id(),用于访问的价值var?
UPD:
我还想说,如果这不能在标准Python中完成,那么它也会很有趣,如果这可以通过黑客攻击C++内部的Python来实现.
UPD2:
知道如何改变价值也会很有趣var.
我最近了解到,Python不仅有一个名为的模块ctypes,该模块具有一个docs页面,还有一个名为的模块_ctypes,该模块没有(但在docs中已多次提及)。互联网上的某些代码(例如此Stack Overflow答案中的代码段)使用了这个神秘的未记录_ctypes模块。
进行一些实验表明,这两个模块具有相似但不相同的文档字符串以及重叠但不相同的属性列表:
Python 3.7.4 (default, Sep 7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes, _ctypes
>>> print(ctypes.__doc__)
create and manipulate C data types in Python
>>> print(_ctypes.__doc__)
Create and manipulate C compatible data types in Python.
>>> dir(ctypes)
['ARRAY', 'ArgumentError', 'Array', 'BigEndianStructure', 'CDLL', 'CFUNCTYPE', 'DEFAULT_MODE', 'LibraryLoader', 'LittleEndianStructure', 'POINTER', 'PYFUNCTYPE', 'PyDLL', 'RTLD_GLOBAL', 'RTLD_LOCAL', 'SetPointerType', …Run Code Online (Sandbox Code Playgroud) 我有一个二维列表,如:
data = [[1,2,3], [2,3,4], [4,5,6]]
Run Code Online (Sandbox Code Playgroud)
我想像这样将它写入 JSON 文件:
{
'data':[
[1,2,3],
[2,3,4],
[4,5,6]
]
}
Run Code Online (Sandbox Code Playgroud)
我得到这个json.dumps(data, indent=4, sort_keys=True):
{
'data':[
[
1,
2,
3
],
[
2,
3,
4
],
[
4,
5,
6]
]
}
Run Code Online (Sandbox Code Playgroud)
这是另一个问题如何在使用 JSON 模块进行漂亮打印时实现自定义缩进?,但那是字典。
我正在尝试从 Python 中的地址读取值。
假设我在变量中有一个地址: address_vble= 0x900045A1
在C中,我们可以得到 value = *address_vble
我如何在 Python 中做同样的事情?
感谢您的帮助。
我有一个Python dict对象d.d = {'a': 1, 'b': 2, 'c': 3}.我的问题很简单.我想引用一个变量到元素d.例如,类似于:
In[1]: p = d['a']
>>> p = 1
In[2]: p = 2
In[3]: d
>>> d = {'a': 2, 'b': 2, 'c': 3}
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?但据我所知,dict对象是可变的.
python ×9
json ×3
reference ×2
ctypes ×1
dictionary ×1
indentation ×1
memory ×1
pointers ×1
variables ×1