python中基本类型的连接组合和互相转换13种方式
off999 2024-09-13 13:35 24 浏览 0 评论
本篇总结了一下字符串,列表,字典,元组的连接组合使用和类型的互相转换小例子,尤其列表中的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} # 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开发工程师,整理了一套最新的python系统学习教程,想要这些资料的可以关注私信小编“01”即可,希望能对你有所帮助。
相关推荐
- Python中的两个内置函数id()和type()
-
id()>>>id(3)2531362761072>>>id(3.222222)2531397393680>>>id(3.0)25313...
- python 函数中,如何将另一个函数作为参数传递
-
python函数中,如何将另一个函数作为参数传递,类似C#委托defadd(a,b):"""这是一个简单的加法函数,接受两个参数并返回它们的和。""...
- Python性能暴涨10倍的终极指南:7个核心技巧+代码压缩秘籍
-
提升Python程序运行性能,使代码运行更流畅更快,以及压缩代码,减小代码大小,下面的方法仅供大家参考,有什么更好的方法在评论区说说。1.使用NumPy/SciPy替代纯Python循环...
- Python 匿名函数(Lambda 函数)详解
-
匿名函数(AnonymousFunction),在Python中称为lambda函数,是一种不需要使用def关键字定义的小型函数。它主要用于简化代码,特别适合需要函数对象的地方。1.基...
- Python学习笔记 | 匿名函数lambda、映射函数map和过滤函数filter
-
什么是匿名函数?定义:没有函数名的自定义函数场景:函数体非常简单,使用次数很少,没有必要声明函数,通常搭配高阶函数使用。高阶函数是能够把函数当成参数进行传递的函数,如:映射函数map和过滤函数fil...
- python练习:自定义函数调用:商品购物实例
-
1、商品录入dict_myshanpin_iof={101:{"商品名称":"毛毛熊","单价":25},102:{"商品名称":...
- Python中如何使用Lambda函数(lambda在python中的用法)
-
Python和其他编程语言一样,都在其语法中添加了lambda函数,Pythonlambda是匿名函数,比常规Python自定义函数有更简洁的语法。虽然Lambda语法在开始时可能会觉得有点混乱,...
- 8-Python内置函数(python内置函数代码)
-
Python提供了丰富的内置函数,这些函数可以直接使用而无需导入任何模块。以下是一些常用的内置函数及其示例:1-print()1-1-说明输出指定的信息到控制台。1-2-例子2-len()2-1-说...
- 用Python进行函数式编程(python函数程序)
-
什么是函数式编程?函数式程序设计是一种编程范例,它把计算当作数学函数的评价,避免状态和可变数据。换句话说,函数编程(FunctionalProgramming,FP)促进没有副作用和不变变量的代码。它...
- python 函数进阶(python如何进阶)
-
1.有名函数和匿名函数#该函数有名称,名称是adddefadd(x,y):returnx+y#改函数没有名称属于匿名函数,也叫lambda表达式lambda_add...
- python自学者的分享:自定义函数、参数作用域、匿名函数、装饰器
-
#自定义新函数函数名newhsdefnewhs(a,b=1):#b的默认值为1,在没有传入b值时,采用默认值,,默认值参数不能放前边returna-bprint(newh...
- Python 函数式编程的 8 大核心技巧,不允许你还不会
-
函数式编程是一种强调使用纯函数、避免共享状态和可变数据的编程范式。Python虽然不是纯函数式语言,但提供了丰富的函数式编程特性。以下是Python函数式编程的8个核心技巧:1.纯函数(...
- 零基础到发布:手把手教你创建并分发 Python 自定义库
-
作为程序员,我们经常依赖各种外部库来解决不同的问题。这些库由技术娴熟的开发者创建,为我们提供了节省时间和精力的解决方案。但你是否曾想过:“我也能创建属于自己的自定义库吗?”答案是肯定的!本文将为你详细...
- 打工人学Python:(七)自定义函数,打造自己的武器库
-
从一个简单的函数开始#!/usr/bin/envpython#-*-encoding:utf-8-*-'''@Purpose:Wordcount@...
- 肖sir_python自定义函数format、zip函数
-
python自定义函数一、常见的自定义函数已经学过的函数:list、print、set、str、type、tuple、dict、range、input等今天学的函数:format二、实战讲解(一)f...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (93)
- python自定义函数 (53)
- python进度条 (54)
- python的for循环 (56)
- python串口编程 (60)
- python写入txt (51)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python qt (52)
- python人脸识别 (54)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- centos7安装python (53)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)