python中可以改变的数据类型(Python常见数据类型转换操作示例)
类别:脚本大全 浏览量:2896
时间:2021-10-04 01:35:14 python中可以改变的数据类型
Python常见数据类型转换操作示例本文实例讲述了python常见数据类型转换操作。分享给大家供大家参考,具体如下:
类型转换
主要针对几种存储工具:list、tuple、dict、set
特殊之处:dict是用来存储键值对的。
1、list 转换为set
|
l1 = [ 1 , 2 , 4 , 5 ] s1 = set (l1) print ( type (s1)) print (s1) |
输出:
<class 'set'>
{1, 2, 4, 5}
2、set转换为list
|
s1 = set ([ 1 , 2 , 3 , 4 ]) l1 = list (s1) print ( type (l1)) print (l1) |
输出:
<class 'list'>
[1, 2, 3, 4]
3、tuple 转换为set
|
t1 = ( 1 , 2 , 3 , 4 ) s1 = set (t1) print ( type (s1)) print (s1) |
输出:
<class 'set'>
{1, 2, 3, 4}
4、set转换为tuple
|
s1 = set ([ 1 , 2 , 3 , 4 ]) t1 = tuple (s1) print ( type (t1)) print (t1) |
输出:
<class 'tuple'>
(1, 2, 3, 4)
5、list转tuple
|
l1 = [ 1 , 2 , 4 , 5 ] t1 = tuple (l1) print ( type (t1)) print (t1) |
输出:
<class 'tuple'>
(1, 2, 4, 5)
6、tuple转list
|
t1 = ( 1 , 2 , 3 , 4 ) l1 = list (t1) print ( type (l1)) print (l1) |
输出:
<class 'list'>
[1, 2, 3, 4]
7、list转dict
|
list1 = [( 'a' , 1 ),( 'b' , 2 ),( 'c' , 3 )] dict1 = {k:v for k,v in list1} dict2 = {v:k for k,v in list1} print (dict1) print (dict2) |
输出:
{'a': 1, 'b': 2, 'c': 3}
{1: 'a', 2: 'b', 3: 'c'}
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/lm_is_dc/article/details/80077304
您可能感兴趣
- python排序方法简单(快速排序的四种python实现推荐)
- python创建文件的方法(Python3.5文件读与写操作经典实例详解)
- pythondjango搭建web(PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上)
- python利用空列表进行数字排序(python实现计数排序与桶排序实例代码)
- python电脑自动发送邮件(python学习--使用QQ邮箱发送邮件代码实例)
- python语言案例教程单元测试(Python单元和文档测试实例详解)
- opencv人脸识别实战(Python opencv实现人眼/人脸识别以及实时打码处理)
- python实现的数据结构(Python嵌套式数据结构实例浅析)
- python注册码实现(python实现Virginia无密钥解密)
- python带权限复制文件夹(python定时复制远程文件夹中所有文件)
- python3 for循环怎么用(Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解)
- python解析excel例子(Python玩转Excel的读写改实例)
- python加密与解密(python实现简单加密解密机制)
- python的静态方法和类方法(深入解析python中的实例方法、类方法和静态方法)
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- python对mysql数据分析(python使用adbapi实现MySQL数据库的异步存储)
- 换代在即,现在是抄底 7.5代 高尔夫的最佳时机吗(换代在即现在是抄底)
- 2020年大众7.5代高尔夫R终结特别版 最后的呐喊(2020年大众7.5代高尔夫R终结特别版)
- 七年前的这部剧有毒,全剧只有女主红到发紫,男主至今无人认识(七年前的这部剧有毒)
- 宋轶除了演过于曼丽,原来还演过一个青楼女子(宋轶除了演过于曼丽)
- 赵丽颖第一部当女主的戏,主角配角个个都是实力演员(赵丽颖第一部当女主的戏)
- 乾隆为何这么喜爱白塔原因是什么(乾隆为何这么喜爱白塔原因是什么)
热门推荐
- mybatis为什么还用mysql(关于MyBatis连接MySql8.0版本的配置问题)
- python操作mysql连接池(详解Python连接MySQL数据库的多种方式)
- idea docker部署微服务(idea连接docker实现一键部署的方法)
- 定时删除linux日志(Linux使用shell脚本定时删除历史日志文件)
- docker镜像创建教程(Docker镜像加载原理)
- dedecms的简单说明(dedecms 后台假死问题解决方法)
- nginx的请求处理(nginx处理http请求实现过程解析)
- html支付功能怎么实现(Div+CSS仿支付宝登录页面)
- web项目部署到阿里云服务器上(阿里云的WindowsServer2016上部署php+apache)
- uni app 小程序全局样式没法用(uniapp封装小程序雷达图组件的完整代码)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9