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.

目录
相关文章
|
8月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
462 2
|
8月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
8月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
11月前
|
JSON API 数据格式
Python采集京东商品评论API接口示例,json数据返回
下面是一个使用Python采集京东商品评论的完整示例,包括API请求、JSON数据解析
|
8月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
652 4
|
9月前
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南
|
8月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
1063 0
|
10月前
|
JSON 安全 API
Python处理JSON数据的最佳实践:从基础到进阶的实用指南
JSON作为数据交换通用格式,广泛应用于Web开发与API交互。本文详解Python处理JSON的10个关键实践,涵盖序列化、复杂结构处理、性能优化与安全编程,助开发者高效应对各类JSON数据挑战。
468 1
|
10月前
|
存储 数据处理 Python
python dict的所有基础知识
python dict的所有基础知识
584 0
|
9月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1433 102

推荐镜像

更多