python dict type like json

简介:
python的dictionary类型和JSON类似, 
定义dict类型的方法: 使用{}构造符或dict()
>>> new_dict={}
>>> type(new_dict)
<class 'dict'>
>>> new_dict2=dict()
>>> type(new_dict2)
<class 'dict'>
如下 : 
>>> new_dict1={"a":"hello", "b":[1,2,3,4,5]}
>>> print(new_dict1)
{'a': 'hello', 'b': [1, 2, 3, 4, 5]}
甚至还可以嵌套
>>> new_dict3={"hello":new_dict2,"a":"b"}
>>> print(new_dict3)
{'hello': {}, 'a': 'b'}
>>> new_dict2=new_dict3
>>> print(new_dict3)
{'hello': {}, 'a': 'b'}
>>> new_dict2={"www":"sky-mobi"}
>>> print(new_dict3)
{'hello': {}, 'a': 'b'}

[其他]
list的用法举例, pop用于取出最后一个值, FILO的方式取数据.
>>> new_l1=[1,2,3,4,5,6]
>>> new_l1.pop()
6
>>> new_l1.pop()
5
>>> new_l1.pop()
4
>>> new_l1
[1, 2, 3]
扩展单个元素使用append
>>> new_l1.append(4)
>>> new_l1.pop()
4
扩展多个元素使用extend
>>> new_l1.extend([4,5,6,7])
>>> new_l1.pop()
7
>>> new_l1
[1, 2, 3, 4, 5, 6]
按位置插入使用insert
>>> new_l1.insert(1,10)
>>> new_l1
[1, 10, 2, 3, 4, 5, 6]
直接倒序, 使用reverse
>>> new_l1.reverse()
>>> new_l1
[6, 5, 4, 3, 2, 10, 1]
按index位置取数据
>>> new_l1.pop(1)
5
清除使用remove(x)或clear()

Operation Result Notes
s[i] = x item i of s is replaced by x  
s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t  
del s[i:j] same as s[i:j] = []  
s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1)
del s[i:j:k] removes the elements of s[i:j:k] from the list  
s.append(x) appends x to the end of the sequence (same ass[len(s):len(s)] = [x])  
s.clear() removes all items from s (same as del s[:]) (5)
s.copy() creates a shallow copy of s (same as s[:]) (5)
s.extend(t) extends s with the contents of t (same ass[len(s):len(s)] = t)  
s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] =[x])  
s.pop([i]) retrieves the item at i and also removes it from s (2)
s.remove(x) remove the first item from s where s[i] == x (3)
s.reverse() reverses the items of s in place (4)

Notes:

  1. t must have the same length as the slice it is replacing.

  2. The optional argument i defaults to -1, so that by default the last item is removed and returned.

  3. remove raises ValueError when x is not found in s.

  4. The reverse() method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence.

  5. clear() and copy() are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as dict andset)

    New in version 3.3: clear() and copy() methods.

相关文章
|
6天前
|
存储 JSON 编解码
python之simplejson:JSON 编/解码器示例详解
python之simplejson:JSON 编/解码器示例详解
7 0
|
6天前
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
14 0
|
14天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
18 1
|
14天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
23 4
|
16天前
|
JSON 数据格式 Python
python把json转换为execl
确保替换 `'your_data.json'` 为你实际的JSON文件路径,并将 `'output.xlsx'` 替换为你希望保存的Excel文件路径。以上步骤将帮助你将JSON数据转换为Excel文件,以便进一步处理或分享。
23 0
|
18天前
|
存储 JSON 数据处理
|
19天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
19 0
|
1月前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
2月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
5天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用