本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的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'>

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}
#
dict1 = {}
for i in tuple1:
dict1[i] = tuple2[tuple1.index(i)]
print(dict1) # {1: 4, 2: 5, 3: 6} #
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中基本类型的连接组合和互相转换的更多相关文章

  1. python中基本类型的连接组合和互相转换13种方式

    本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的extend()方法和字典中的 update方法非常的常用. 1.连接两个字符串 a = "hello ...

  2. 全面理解Python中的类型提示(Type Hints)

    众所周知,Python 是动态类型语言,运行时不需要指定变量类型.这一点是不会改变的,但是2015年9月创始人 Guido van Rossum 在 Python 3.5 引入了一个类型系统,允许开发 ...

  3. python中的类型

    python中的类型分为四种 1.整形 2.浮点型 3.字符串 4.对象(除了前三种,其他的都是对象) 比如函数也是对象 def fun(): print(123) type(fun) // < ...

  4. python中序列类型

    Python中的序列类型使用 元组类型 一旦被创建,就无法被修改. 创建 使用()或者tuple()创建 creater1=('cat', 'dog', 'tiger', 'human') creat ...

  5. Python中布尔类型

    我们已经了解了Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算:与运算:只有两个布尔值都为 True 时,计算结果才为 True.True and T ...

  6. python中数字类型与处理工具

    python中的数字类型工具 python中为更高级的工作提供很多高级数字编程支持和对象,其中数字类型的完整工具包括: 1.整数与浮点型, 2.复数, 3.固定精度十进制数, 4.有理分数, 5.集合 ...

  7. python中如何将字符串连接在一起,多倍的字符串如何输出

    说明: 在python中,如果有多个字符串,想要连接在一起,或者说想要拼接在一起该如何操作,在此记录下. 操作过程: 1.通过 + 这个加号操作符,将字符串拼接在一起 >>> &qu ...

  8. python中可变类型和不可变类型

    1.python中的可变类型和不可变类型 python中的数据类型大致可分为6类:1.Number(数字) 2. String(字符串) 3. Tuple (元组) 4. List(列表) 5. Di ...

  9. java和python中的string和int数据类型的转换

    未经允许,禁止转载!!! 在平时写代码的时候经常会用到string和int数据类型的转换 由于java和python在string和int数据类型转换的时候是不一样的 下面总结了java和python ...

随机推荐

  1. UE4入门(二)建立和打开项目

    1.双击电脑桌面上的Unreal Engine 2.见下图 建立c++或者蓝图项目: 蓝图是什么? 蓝图种类: 接口:

  2. IE内核浏览器的404页面问题和IE自动缓存引发的问题

    本站404页面被IE替换成IE自己的404页面 在权限设置正确的情况下,自定义的404页面文件大小如果小于512字节,那么IE内核的浏览器会认为你自定义的404页面不够权威,从而使用其自带的404页面 ...

  3. 使用PrerenderSpaPlugin预渲染插件没有成功渲染

    问题 在已有vue项目里使用prerender-spa-plugin插件时,遇到了build出来的页面是白屏或者出现{"statusCode":404,"error&qu ...

  4. 9、js扩展

    作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理. 本片导航: js的作用域 作用域链(Scope Chain) 一. ...

  5. Docker技术快速精通指南

    doctor专业网站:http://www.dockerinfo.net/ Docker中文文档 csdn 的docker专栏: Docker技术快速精通指南

  6. 何谓sdk,何谓api

    狭义上的 SDK 指 Windows SDK,包括在 Windows 平台进行开发的一系列头文件和库文件以及命令行工具等. API 是 SDK 提供给用户的函数,即接口就是这个 SDK 提供给你用于应 ...

  7. mysql select into 不支持

    不支持的 select * into order_new from orders 改为 Create table order_new(select * from orders)  

  8. Centos7离线安装redis

    1.1 下载.上传.解压redis4.0.11 下载地址:http://download.redis.io/releases/redis-4.0.11.tar.gz下载后上传到服务器上,并解压:备注: ...

  9. [Linux] - Windows与Linux网络共享文件夹挂载方法

    Windows与Linux网络SMB方式文件夹共享挂载 本示例系统: Windows 2003+ Linux-Centos/Ubuntu 本示例全为命令行操作,如何使用Windows.Linux命令行 ...

  10. 一步步教你轻松学K-means聚类算法

    一步步教你轻松学K-means聚类算法(白宁超  2018年9月13日09:10:33) 导读:k-均值算法(英文:k-means clustering),属于比较常用的算法之一,文本首先介绍聚类的理 ...