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 ...
随机推荐
- mybatis中因为不理解$与#而出现的bug
最近项目中遇到一个bug,正常的流程是这样的:要上传一个应用,首先检查系统中是否已经存在这个应用的更高版本,如果存在,则上传操作将被取消. bug体现为当传入系统中存在的所有应用与新上传的应用的ver ...
- 微信小程序——e.target与e.currentTarget的区别
在小程序的点击事件中,我们经常使用这两个属性来传参,看起来效果一样,查了官方文档如下: target:事件源组件对象 currentTarget:当前组件对象 什么意思?我刚开始就有点不懂,那就直接上 ...
- js学习——1
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- luogu P1731 [NOI1999]生日蛋糕 |暴力枚举
题目背景 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层 生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M)层蛋糕是半径为Ri, 高度为Hi的圆柱 ...
- 趁热来一波,WWDC 2016 iMessage App开发
转自:http://www.jianshu.com/p/be79b8729bf8 WWDC 2016关于iMessage App的两个视频已经放出(iMessage Apps and Stickers ...
- js练习-两个栈实现队列
目录 题 解 题 现在有个Q队列和栈A,栈B,栈只有两个方法,push()和pop(), 队列也只有两个方法,push()和pull(),队列的进和出都只能通过A和B的push和pop实现. // 大 ...
- lodash.memoize
目录 _.memoize(func, [resolver]) 举例1: 获取J(1000000)的值 举例2: 斐波那契数列F(1000)的值 _.memoize(func, [resolver]) ...
- jQuery中的属性选择器
先看代码,后面详细解释: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- [TimLinux] Python __hash__ 可哈希集合
规则: __hash__ 应该返回一个整数,hash()函数计算基础类型的hash值 可哈希集合:set(), forzenset(), dict() 三种数据结构操作要求 key 值唯一,判断唯一的 ...
- [权限管理系统(四)]-spring boot +spring security短信认证+redis整合
[权限管理系统]spring boot +spring security短信认证+redis整合 现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...