python数据类型之间的互相转换

2023-05-16

目录

1.将元组转为列表

(1)语法:list(tuple)

(2)实例

①简单的用法实例

②与for函数、if函数、split函数结合使用

2.将列表转为元组

(1)语法:tuple(list)

(2)实例

3.将数字转为字符串

(1)语法:string(number)

(2)实例


1.将元组转为列表

(1)语法:list(tuple)

(2)实例

①简单的用法实例

#list
tup1 = (1,4,6,7)
ls1 = list(tup1)
print('转换的列表ls1为:',ls1)


tup2 = ('hello','world','funny','window')
ls2 = list(tup2)
print('转换的列表ls2为:',ls2)

输出结果为:

转换的列表ls1为: [1, 4, 6, 7]
转换的列表ls2为: ['hello', 'world', 'funny', 'window']

②与for函数、if函数、split函数结合使用

ls = {('hello','home','funny','windows','water'), 'come on friends'}
for element in ls:
    if type(element) == tuple:
        ls1 = list(element)
        print('转换的列表ls为:',ls1)
    elif type(element) == str:
        ls2 = element.split()
        print('转换的列表ls为:',ls2)
    else:
        pass

输出结果:

转换的列表ls为: ['hello', 'home', 'funny', 'windows', 'water']
转换的列表ls为: ['come', 'on', 'friends']

说明:

①将字符串转为列表类型:list = string.split()

②type函数是用来判断对象是什么数据类型的。

2.将列表转为元组

(1)语法:tuple(list)

(2)实例

#tuple
#简单的用法实例
ls1  = [1,3,8,6,12,7]
tup1 = tuple(ls1)
print('转换的元组tup1为:',tup1)

ls2 = ['hello','world','funny','window']
tup2 = tuple(ls2)
print('转换的元组tup2为:',tup2)


#与if函数结合使用
ls3 = ['fine','happy','creative']
if type(ls3) == list:
    tup3 = tuple(ls3)
    print('转换的元组tup3为:',tup3)
else:
    pass

输出结果为:

转换的元组tup1为: (1, 3, 8, 6, 12, 7)
转换的元组tup2为: ('hello', 'world', 'funny', 'window')
转换的元组tup3为: ('fine', 'happy', 'creative')

3.将数字转为字符串

(1)语法:string(number)

(2)实例

#string
#简单的用法实例
num1 = 543563
string1 = str(num1)
print('转换的字符串string1为:',string1)
print('转换的字符串string1类型为:',type(string1))

num2 = 100086
string2 = str(num2)
print('转换的字符串string2为:',string2)
print('转换的字符串string2类型为:',type(string2))

#与if函数结合使用
ls = [56340,5983.25,'hello',10086,'happy']
n = 2
for i in ls :
    if type(i) == int or type(i) == float:
        n+=1
        string3 = str(i)
        print('转换的字符串string%d为:%s'%(n,string3))
        print('转换的字符串string%d类型为:%s'%(n,type(string3)))
    else:
        pass

输出结果:

转换的字符串string1为: 543563
转换的字符串string1类型为: <class 'str'>
转换的字符串string2为: 100086
转换的字符串string2类型为: <class 'str'>
转换的字符串string3为:56340
转换的字符串string3类型为:<class 'str'>
转换的字符串string4为:5983.25
转换的字符串string4类型为:<class 'str'>
转换的字符串string5为:10086
转换的字符串string5类型为:<class 'str'>

参考文章

具体可以参考split函数的用法实例:python如何将字符串进行拆分——split函数的用法及实例_小白修炼晋级中的博客-CSDN博客_python中str.split()的例子

 具体if判断语句用法可参考:python的if条件语句的用法及实例_小白修炼晋级中的博客-CSDN博客_python的if条件

具体for函数的用法可参考:

python的for循环语句的用法及实例_小白修炼晋级中的博客-CSDN博客

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python数据类型之间的互相转换 的相关文章

  • 如何在python中读取多个文件中的文本

    我的文件夹中有许多文本文件 大约有 3000 个文件 每个文件中第 193 行是唯一包含重要信息的行 我如何使用 python 将所有这些文件读入 1 个文本文件 os 模块中有一个名为 list dir 的函数 该函数返回给定目录中所有文
  • Python、Tkinter、更改标签颜色

    有没有一种简单的方法来更改按钮中文本的颜色 I use button text input text here 更改按下后按钮文本的内容 是否存在类似的颜色变化 button color red Use the foreground设置按钮
  • Python PAM 模块的安全问题?

    我有兴趣编写一个 PAM 模块 该模块将利用流行的 Unix 登录身份验证机制 我过去的大部分编程经验都是使用 Python 进行的 并且我正在交互的系统已经有一个 Python API 我用谷歌搜索发现pam python http pa
  • 如何在android上的python kivy中关闭应用程序后使服务继续工作

    我希望我的服务在关闭应用程序后继续工作 但我做不到 我听说我应该使用startForeground 但如何在Python中做到这一点呢 应用程序代码 from kivy app import App from kivy uix floatl
  • DreamPie 不适用于 Python 3.2

    我最喜欢的 Python shell 是DreamPie http dreampie sourceforge net 我想将它与 Python 3 2 一起使用 我使用了 添加解释器 DreamPie 应用程序并添加了 Python 3 2
  • Python 多处理示例不起作用

    我正在尝试学习如何使用multiprocessing但我无法让它发挥作用 这是代码文档 http docs python org 2 library multiprocessing html from multiprocessing imp
  • 如何在Windows上模拟socket.socketpair

    标准Python函数套接字 套接字对 https docs python org 3 library socket html socket socketpair不幸的是 它在 Windows 上不可用 从 Python 3 4 1 开始 我
  • Python tcl 未正确安装

    我刚刚为 python 安装了graphics py 但是当我尝试运行以下代码时 from graphics import def main win GraphWin My Circle 100 100 c Circle Point 50
  • python pandas 中的双端队列

    我正在使用Python的deque 实现一个简单的循环缓冲区 from collections import deque import numpy as np test sequence np array range 100 2 resha
  • Python:字符串不会转换为浮点数[重复]

    这个问题在这里已经有答案了 我几个小时前写了这个程序 while True print What would you like me to double line raw input gt if line done break else f
  • HTTPS 代理不适用于 Python 的 requests 模块

    我对 Python 还很陌生 我一直在使用他们的 requests 模块作为 PHP 的 cURL 库的替代品 我的代码如下 import requests import json import os import urllib impor
  • Python - 按月对日期进行分组

    这是一个简单的问题 起初我认为很简单而忽略了它 一个小时过去了 我不太确定 所以 我有一个Python列表datetime对象 我想用图表来表示它们 x 值是年份和月份 y 值是此列表中本月发生的日期对象的数量 也许一个例子可以更好地证明这
  • 设置 torch.gather(...) 调用的结果

    我有一个形状为 n x m 的 2D pytorch 张量 我想使用索引列表来索引第二个维度 可以使用 torch gather 完成 然后然后还设置新值到索引的结果 Example data torch tensor 0 1 2 3 4
  • glpk.LPX 向后兼容性?

    较新版本的glpk没有LPXapi 旧包需要它 我如何使用旧包 例如COBRA http opencobra sourceforge net openCOBRA Welcome html 与较新版本的glpk 注意COBRA适用于 MATL
  • 用于运行可执行文件的python多线程进程

    我正在尝试将一个在 Windows 上运行可执行文件并管理文本输出文件的 python 脚本升级到使用多线程进程的版本 以便我可以利用多个核心 我有四个独立版本的可执行文件 每个线程都知道要访问它们 这部分工作正常 我遇到问题的地方是当它们
  • 从 Python 中的类元信息对 __init__ 函数进行类型提示

    我想做的是复制什么SQLAlchemy确实 以其DeclarativeMeta班级 有了这段代码 from sqlalchemy import Column Integer String from sqlalchemy ext declar
  • 循环标记时出现“ValueError:无法识别的标记样式 -d”

    我正在尝试编码pyplot允许不同标记样式的绘图 这些图是循环生成的 标记是从列表中选取的 为了演示目的 我还提供了一个颜色列表 版本是Python 2 7 9 IPython 3 0 0 matplotlib 1 4 3 这是一个简单的代
  • Python - 字典和列表相交

    给定以下数据结构 找出这两种数据结构共有的交集键的最有效方法是什么 dict1 2A 3A 4B list1 2A 4B Expected output 2A 4B 如果这也能产生更快的输出 我可以将列表 不是 dict1 组织到任何其他数
  • Python 分析:“‘select.poll’对象的‘poll’方法”是什么?

    我已经使用 python 分析了我的 python 代码cProfile模块并得到以下结果 ncalls tottime percall cumtime percall filename lineno function 13937860 9
  • Pandas 与 Numpy 数据帧

    看这几行代码 df2 df copy df2 1 df 1 df 1 values 1 df2 ix 0 0 我们的教练说我们需要使用 values属性来访问底层的 numpy 数组 否则我们的代码将无法工作 我知道 pandas Data

随机推荐