python中基本类型的连接组合和互相转换13种方式
本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的extend()方法和字典中的
update方法非常的常用。
1.连接两个字符串
a = "hello "
b = "world"
a += b
print(a) # hello world
2.字典的连接
dict1 = {1: "a", 2: "b"}
dict2 = {3: "c", 4: "d"}
dict1.update(dict2)
print(dict1) # {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
3.列表的连接
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2) # [1, 2, 3, 4, 5, 6]
print(list1)
4.元组的连接
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple1 += tuple2
print(tuple1) # (1, 2, 3, 4)
5.字典转换为字符串
dict1 = {1: "a", 2: "b"}
str1 = str(dict1)
print(str1) # {1: 'a', 2: 'b'}
print(type(str1)) # <class 'str'>
PS:遇到问题没人解答?需要Python学习资料?可以加点击下方链接自行获取
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76
6.字典转换为列表
dict1 = {1: "a", 2: "b"}
list1 = list(dict1.keys())
list2 = list(dict1.values())
list3 = list(dict1)
print(list1) # [1, 2]
print(list2) # ['a', 'b']
print(list3) # [1,2]
7.字典转换为元组
dict1 = {1: "a", 2: "b"}
tuple1 = tuple(dict1.keys())
tuple2 = tuple(dict1.values())
tuple3 = tuple(dict1)
print(tuple1) # (1, 2)
print(tuple2) # ('a', 'b')
print(tuple3) # (1, 2)
8.列表转换为字符串
list1 = [1, 2, 3]
str1 = str(list1)
print(str1) # [1, 2, 3]
print(type(str1)) # <class 'str'>
9.列表转换为字典
# 1.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
dict1 = dict(zip(list1, list2))
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
# 2.
dict1 = {}
for i in list1:
dict1[i] = list2[list1.index(i)]
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
# 3.
list1 = [[1, 'a'], [2, 'b'], [3, 'c']]
dict1 = dict(list1)
print(dict1) # {1: 'a', 2: 'b', 3: 'c'}
10.列表转换为元组
list1 = [1, 2, 3]
tuple1 = tuple(list1)
print(tuple1) # (1, 2, 3)
11.元组转换为字符串
tuple1 = (1, 2, 3)
str1 = tuple(tuple1)
print(str1) # (1, 2, 3)
print(type(str1)) # <class 'tuple'>
12.元组转换为字典
# 1.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
dict1 = dict(zip(tuple1, tuple2))
print(dict1) # {1: 4, 2: 5, 3: 6}
# 2
dict1 = {}
for i in tuple1:
dict1[i] = tuple2[tuple1.index(i)]
print(dict1) # {1: 4, 2: 5, 3: 6}
# 3
tuple1 = (1, 2)
tuple2 = (4, 5)
tuple3 = (tuple1, tuple2)
dict1 = dict(tuple3)
print(dict1) # {1: 2, 4: 5}
13.元组转换为列表
tuple1 = (1, 2)
list1 = list(tuple1)
print(list1) # [1, 2]
python中基本类型的连接组合和互相转换13种方式的更多相关文章
- python中基本类型的连接组合和互相转换
本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的extend()方法和字典中的 update方法非常的常用. 1.连接两个字符串 a = "hello ...
- 全面理解Python中的类型提示(Type Hints)
众所周知,Python 是动态类型语言,运行时不需要指定变量类型.这一点是不会改变的,但是2015年9月创始人 Guido van Rossum 在 Python 3.5 引入了一个类型系统,允许开发 ...
- python中的类型
python中的类型分为四种 1.整形 2.浮点型 3.字符串 4.对象(除了前三种,其他的都是对象) 比如函数也是对象 def fun(): print(123) type(fun) // < ...
- plsql 连接oracle数据库的2种方式
plsql 连接oracle数据库的2种方式 CreationTime--2018年8月10日09点50分 Author:Marydon 方式一:配置tnsnames.ora 该文件在instan ...
- 连接远程服务器的几种方式/Vscode + Remote
连接远程服务器的几种方式 前言 最近在尝试做网盘,使用的技术栈大概是 .net core + MVC + Mysql + Layui,主要目的是通过这个具体的项目,熟悉熟悉 .net core 开发, ...
- python核心高级学习总结3-------python实现进程的三种方式及其区别
python实现进程的三种方式及其区别 在python中有三种方式用于实现进程 多进程中, 每个进程中所有数据( 包括全局变量) 都各有拥有⼀份, 互不影响 1.fork()方法 ret = os.f ...
- SQLPlus在连接时通常有四种方式
SQLPlus在连接时通常有四种方式 1. ? 1 sqlplus / as sysdba 操作系统认证,不需要数据库服务器启动listener,也不需要数据库服务器处于可用状态.比如我们想要启动数据 ...
- js中 json对象与json字符串相互转换的几种方式
以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...
- python中序列类型
Python中的序列类型使用 元组类型 一旦被创建,就无法被修改. 创建 使用()或者tuple()创建 creater1=('cat', 'dog', 'tiger', 'human') creat ...
随机推荐
- 链表-C语言实现
链式存储线性表的结构体: typedef int ElemType; //元素类型 typedef struct Node //链表结构体 { ElemType date; //链表结点数据域 str ...
- centos使用yum存储快速安装MySQL
RHEL/CentOS 7.x MySQL yum库 https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm RHEL ...
- git 使用详解(4)—— commit -a -m/diff --staged/rm/mv
查看已暂存和未暂存的更新 实际上 git status的显示比较简单,仅仅是 列出了(修改过的.新创建的.已经暂存但未提交的)文件,如果要查看具体修改了什么地方,可以用git diff 命令.稍后我们 ...
- 数据库Oracle函数之单行函数的介绍
函数介绍: 函数:是数据库产品中提供的能够处理查询结果的方法. 函数能够用于下面的目的: • 执行数据计算 • 修改单个数据项 • 格式化显示的日期和数字 • 转换列数据类型 • 函数有输入参数,并且 ...
- 洛谷 题解 P1287 【盒子与球】
题解:P1287 盒子与球 不了解的:stirling数(斯特林数) - 百度百科 分析如下: 设有n个不同的球,分别用b1,b2,--bn表示.从中取出一个球bn,bn的放法有以下两种: 1) bn ...
- Mysql基础03-函数
函数 字符串函数 函数 用法 CONCAT(S1,S2,......,Sn) 连接S1,S2,......,Sn为一个字符串 CONCAT_WS(s, S1,S2,......,Sn) 同CONCAT ...
- Dubbo学习系列之七(分布式订单ID方案)
既然选择,就注定风雨兼程! 开始吧! 准备:Idea201902/JDK11/ZK3.5.5/Gradle5.4.1/RabbitMQ3.7.13/Mysql8.0.11/Lombok0.26/Erl ...
- 建议2:注意Javascript数据类型的特殊性---(2)慎用JavaScript类型自动转换
在JavaScript中能够自动转换变量的数据类型,这种转换是一种隐性行为.在自动转换数据类型时,JavaScript一般遵循:如果某个类型的值被用于需要其它类型的值的环境中,JavaScript就自 ...
- 服务器端Mysql常用操作
原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/f7463513-5 ...
- vue 中 px转vw的用法
下面介绍最简单的用法 1 下载依赖 npm install postcss-import postcss-loader postcss-px-to-viewport --save-dev 2 在项目根 ...