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

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

    本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的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. plsql 连接oracle数据库的2种方式

      plsql 连接oracle数据库的2种方式 CreationTime--2018年8月10日09点50分 Author:Marydon 方式一:配置tnsnames.ora 该文件在instan ...

  5. 连接远程服务器的几种方式/Vscode + Remote

    连接远程服务器的几种方式 前言 最近在尝试做网盘,使用的技术栈大概是 .net core + MVC + Mysql + Layui,主要目的是通过这个具体的项目,熟悉熟悉 .net core 开发, ...

  6. python核心高级学习总结3-------python实现进程的三种方式及其区别

    python实现进程的三种方式及其区别 在python中有三种方式用于实现进程 多进程中, 每个进程中所有数据( 包括全局变量) 都各有拥有⼀份, 互不影响 1.fork()方法 ret = os.f ...

  7. SQLPlus在连接时通常有四种方式

    SQLPlus在连接时通常有四种方式 1. ? 1 sqlplus / as sysdba 操作系统认证,不需要数据库服务器启动listener,也不需要数据库服务器处于可用状态.比如我们想要启动数据 ...

  8. js中 json对象与json字符串相互转换的几种方式

    以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...

  9. python中序列类型

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

随机推荐

  1. 使用PaintCode便捷地实现动画效果

    // // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import " ...

  2. Python批量检测服务器端口可用性与Socket函数使用

    socket函数 简述 socket又称套间字或者插口,是网络通信中必不可少的工具.有道是:"无socket,不网络".由于socket最早在BSD Unix上使用,而Unix/L ...

  3. 虚拟环境创建的另外一种方法:anaconda

    anaconda基本使用- 主要是一个虚拟环境管理器-还是一个安装包管理器- conda list: 显示anaconda安装的包- conda env list: 显示anaconda你的虚拟环境列 ...

  4. 在一个数组中,除了两个数外,其余数都是两两成对出现,找出这两个数,要求时间复杂度O(n),空间复杂度O(1)

    题目:在一个数组中,除了两个数外,其余数都是两两成对出现,找出这两个数,要求时间复杂度O(n),空间复杂度O(1) 分析:这道题考察位操作:异或(^),按位与(&),移位操作(>> ...

  5. componentWillMount VS componentDidMount

    前言 这与React组件的生命周期有关,组件挂载时有关的生命周期有以下几个: constructor(){} componentWillMount(){} render(){} componentDi ...

  6. iOS libsqlite3.0.tbd和libsqlite3.tbd的区别

    ibsqlite3.0.tbd 只是一个快捷方式,其实也是指向libsqlite3.tbd的,如果libsqlite3.0.tbd指向的就是最新的libsqlite3.tbd,就不用更新了.

  7. 数据挖掘算法(一)--K近邻算法 (KNN)

    数据挖掘算法学习笔记汇总 数据挖掘算法(一)–K近邻算法 (KNN) 数据挖掘算法(二)–决策树 数据挖掘算法(三)–logistic回归 算法简介 KNN算法的训练样本是多维特征空间向量,其中每个训 ...

  8. 2019牛客全国多校训练四 I题 string (SAM+PAM)

    链接:https://ac.nowcoder.com/acm/contest/884/I来源:牛客网 题目描述 We call a,ba,ba,b non-equivalent if and only ...

  9. nginx的一些知识(一)

    第8章 web网站的搭建 curl -Lv 网站地址:查看网站的请求信息和响应信息,并且会将结果输出出来 8.1 web网站的的传输原理过程 会进行DNS的解析 进行客户端和服务端进行三次握手协议 客 ...

  10. JS-变量、作用域、垃圾回收机制总结

    预解析时变量和函数同名的话,保留函数