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.

目录
相关文章
|
3月前
|
JSON API 数据格式
Python采集京东商品评论API接口示例,json数据返回
下面是一个使用Python采集京东商品评论的完整示例,包括API请求、JSON数据解析
|
3月前
|
存储 JSON API
Python与JSON:结构化数据的存储艺术
Python字典与JSON格式结合,为数据持久化提供了便捷方式。通过json模块,可轻松实现数据序列化与反序列化,支持跨平台数据交换。适用于配置管理、API通信等场景,兼具可读性与高效性,是Python开发中不可或缺的数据处理工具。
132 0
|
11天前
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南
|
2月前
|
JSON 安全 API
Python处理JSON数据的最佳实践:从基础到进阶的实用指南
JSON作为数据交换通用格式,广泛应用于Web开发与API交互。本文详解Python处理JSON的10个关键实践,涵盖序列化、复杂结构处理、性能优化与安全编程,助开发者高效应对各类JSON数据挑战。
137 1
|
7月前
|
XML JSON API
淘宝商品详情API的调用流程(python请求示例以及json数据示例返回参考)
JSON数据示例:需要提供一个结构化的示例,展示商品详情可能包含的字段,如商品标题、价格、库存、描述、图片链接、卖家信息等。考虑到稳定性,示例应基于淘宝开放平台的标准响应格式。
|
6月前
|
数据采集 存储 JSON
用Python爬虫抓取数据并保存为JSON的完整指南
用Python爬虫抓取数据并保存为JSON的完整指南
|
7月前
|
JSON 监控 API
python语言采集淘宝商品详情数据,json数据示例返回
通过淘宝开放平台的API接口,开发者可以轻松获取商品详情数据,并利用这些数据进行商品分析、价格监控、库存管理等操作。本文提供的示例代码和JSON数据解析方法,可以帮助您快速上手淘宝商品数据的采集与处理。
|
存储 Python
一文掌握python数组字典dict()的全部用法(零基础学python(三))
一文掌握python数组字典dict()的全部用法(零基础学python(三))
|
存储 索引 Python
Python教程:深入了解 Python 中 Dict、List、Tuple、Set 的高级用法
Python 中的 Dict(字典)、List(列表)、Tuple(元组)和 Set(集合)是常用的数据结构,它们各自有着不同的特性和用途。在本文中,我们将深入了解这些数据结构的高级用法,并提供详细的说明和代码示例。
810 2
|
Python 容器
python中dict的详细用法以及set集合使用
python中dict的详细用法以及set集合使用
315 0

推荐镜像

更多