python
Python:怎么把字符串转换成字典(key:value)的形式?
发布时间:2024-05-26
访问量:0
来源:未知
贴一个简单的方法。
(前面有位老兄提到过,我这里细化一下)
思路: 1.字典化 2.提取key名
Demo:
既然有题主这个需求,说明这个变量
目前已被赋值
,这个值可以是任意值
,可以是int,string,甚至是一个类实例,这里以int举例。来看一下具体发生了什么:
这里最骚的一步就是第一步
字典化:
粗体
abc
交给字典做key名,第二个abc(即376)是key的value。这就保证了变量在提取过程中值不会发生任何改变。这个方法巧妙地利用了python字典化操作中将变量名变成key名的特点,根本无需调用复杂的底层命名空间等等这种鬼操作。
个人感觉,这个需求太太太太小众了,但极偶尔在有些奇奇怪怪的地方就能派上用场了。
上一篇:1None代表什么意思?
热点信息
-
在Python中,要查看函数的用法,可以使用以下方法: 1. 使用内置函数help():在Python交互式环境中,可以直接输入help(函数名)来获取函数的帮助文档。例如,...
-
一、java 连接数据库 在当今信息时代,Java 是一种广泛应用的编程语言,尤其在与数据库进行交互的过程中发挥着重要作用。无论是在企业级应用开发还是...
-
一、idea连接mysql数据库 php connect_error) { die("连接失败: " . $conn->connect_error);}echo "成功连接到MySQL数据库!";// 关闭连接$conn->close();?> 二、idea连接mysql数据库连...
-
要在Python中安装modbus-tk库,您可以按照以下步骤进行操作: 1. 确保您已经安装了Python解释器。您可以从Python官方网站(https://www.python.org)下载和安装最新版本...