Python 实现list,tuple,str和dict之间的相互转换

作者:Violet-Guo 时间:2021-02-28 12:35:42 

1、字典(dict)


dict = {‘name': ‘Zara', ‘age': 7, ‘class': ‘First'}

1.1 字典——字符串

返回:


print type(str(dict)), str(dict)

1.2 字典——元组

返回:(‘age', ‘name', ‘class')


print tuple(dict)

1.3 字典——元组

返回:(7, ‘Zara', ‘First')


print tuple(dict.values())

1.4 字典——列表

返回:[‘age', ‘name', ‘class']


print list(dict)

1.5 字典——列表


print dict.values

2、元组


tup=(1, 2, 3, 4, 5)

2.1 元组——字符串

返回:(1, 2, 3, 4, 5)


print tup.__str__()

2.2 元组——列表

返回:[1, 2, 3, 4, 5]


print list(tup)

2.3 元组不可以转为字典

3、列表


nums=[1, 3, 5, 7, 8, 13, 20];

3.1 列表——字符串

返回:[1, 3, 5, 7, 8, 13, 20]


print str(nums)

3.2 列表——元组

返回:(1, 3, 5, 7, 8, 13, 20)


print tuple(nums)

3.3 列表不可以转为字典

4、字符串

4.1 字符串——元组

返回:(1, 2, 3)


print tuple(eval("(1,2,3)"))

4.2 字符串——列表

返回:[1, 2, 3]


print list(eval("(1,2,3)"))

4.3 字符串——字典

返回:


print type(eval("{'name':'ljq', 'age':24}"))

补充:python入门之路:一个小错误,str变tuple

笔者在编程的时候发现,原先定义的str字符串在传递和引用的时候会莫名其妙改变类型,变成tuple。


import random
class get_Veri(object):
 def random_color(self):
   random_color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
   return random_color

def random_num(self):
   random_num = str(random.randint(0, 9))
   return random_num

def random_lowerchr(self):
   random_lowerchar=chr(random.randint(97, 122))
   return random_lowerchar

def random_upperchr(self):
   random_upperchr = chr(random.randint(65, 90))
   return random_upperchr

def random_char(self):
   random_char = random.choice([get_Veri.random_num(self), get_Veri.random_upperchr(self), get_Veri.random_lowerchr(self)])
   print(random_char)
   print(type(random_char))
   return random_char

这里random_char函数输出一个随机字符串,可以看到type类型为:


<class 'str'>

在另一个文件中进行引用:


from random_data.py import get_Veri

get_veri=get_Veri()
random_char = get_veri.random_char(),
print(random_char)
print(type(random_char))

发现random_char的type类型已经发生改变:


<class 'tuple'>

只是一个简单的赋值,为什么会发生改变?

原因是在赋值的时候多加了一个逗号。

这个逗号让编译器执行的时候理解为("str",)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/violet_echo_0908/article/details/52486689

标签:Python,list,tuple,str,dict
0
投稿

猜你喜欢

  • python双向链表原理与实现方法详解

    2021-03-06 05:29:39
  • Window 7/XP 安装Apache 2.4与PHP 5.4 的过程详解

    2023-11-24 09:28:09
  • pytest解读fixtures之Teardown处理yield和addfinalizer方案

    2023-06-18 22:13:01
  • pandas 如何将字符串映射为数字

    2021-04-15 02:47:33
  • anaconda jupyter不能导入安装的lightgbm解决方案

    2021-09-15 19:24:45
  • 讲解MaxDB数据库和MySQL数据库的主要差别

    2009-01-04 13:04:00
  • Python还能这么玩之用Python修改了班花的开机密码

    2023-11-23 17:38:40
  • 为什么python比较流行

    2023-06-26 02:24:22
  • python基础知识之字典(Dict)

    2023-08-25 20:01:44
  • python3 dict ndarray 存成json,并保留原数据精度的实例

    2021-03-04 13:25:31
  • python人工智能tensorflow函数tf.assign使用方法

    2023-04-08 00:59:03
  • Python进程间通讯与进程池超详细讲解

    2023-09-05 16:50:41
  • dataframe设置两个条件取值的实例

    2021-09-15 05:56:50
  • python实现的多任务版udp聊天器功能案例

    2021-07-31 21:49:29
  • 由浅入深讲解MySQL数据库索引的选择性

    2008-12-17 15:06:00
  • python批量修改交换机密码的示例

    2023-06-29 07:52:42
  • Python编写电话薄实现增删改查功能

    2021-07-14 21:28:18
  • Python Tornado框架轻松写一个Web应用的全过程

    2022-05-10 10:38:36
  • python安装whl文件的实战步骤

    2022-04-06 19:59:00
  • oracle 数据库连接分析

    2009-07-28 10:42:00
  • asp之家 网络编程 m.aspxhome.com