python 动态生成变量
locals() 函数会以字典类型返回当前位置的全部局部变量
createVar = locals()
listTemp = [1,2,3,4]
for i,s in enumerate(listTemp):
createVar['a'+str(i+1)] = s
print(createVar,type(createVar))
print(a1,a2,a3,a4)
>>> 'execfile': <function execfile at 0x105095950>, 'runfile': <function runfile at 0x1051f01e0>, 'breakpoint': <function ptvsd_breakpointhook at 0x102c010d0>}, 'createVar': {...}, 'listTemp': [1, 2, 3, 4], 'i': 3, 's': 4, 'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4} <class 'dict'>
1 2 3 4
locals()返回一个包含所有局部变量的字典,createVar指向这个字典。下面的操作就是往这个字典里添加数据了。
另一种方法:
word=[]
for i in range(4):
word.append('words_{}'.format(i))
print(word)
>>>['words_0', 'words_1', 'words_2', 'words_3']
listTemp = [1,2,3,4]
word={}
for i,s in enumerate(listTemp):
word['words_{}'.format(i)] = s
print(word)
>>> {'words_0': 1, 'words_1': 2, 'words_2': 3, 'words_3': 4}
https://www.cnblogs.com/technologylife/p/9211324.html
python 动态生成变量的更多相关文章
- python 动态生成变量名以及动态获取变量的变量名
前言需求: 必须现在需要动态创建16个list,每个list的名字不一样,但是是有规律可循,比如第一个list的名字叫: arriage_list_0=[],第二个叫arriage_list_1=[] ...
- 【转载】 Python动态生成变量
用Python循环创建多个变量, 如创建 a1= .a2= .a3= .a4= .a5= 或 self.a1= .self.a2= . self.a3= 一. 可以通 ...
- Python动态生成变量
Python 也可以像javascript那样动态生成变量.我们看javascript的动态生成变量. var obj = {}; for (var i =0, len = 10; i < le ...
- Python 动态生成多个变量
引用自:https://blog.csdn.net/u013061183/article/details/78015673 用Python循环创建多个变量, 如创建 a1= .a2= .a3= ...
- python 动态创建变量 获取变量名
参考链接:https://www.cnblogs.com/technologylife/p/9211324.html 参考链接(未)(使用inspect 获取变量名):https://blog.csd ...
- Ruby 动态生成变量
创建: 2018/03/21 更新: 2018/03/22 把标题ruby首字母大写 方法一: eval将字符串作为代码执行, 故写在里边 eval("@#{view_name.to_s} ...
- JS如何动态生成变量名[重点]
解决方案: function create_variable(num){ var name = "test_"+num; //生成函数名 ...
- Python使用动态的变量名
当我们在使用Python处理一些重复性很高的事情时,有时候需要很多的变量来存放一些暂行性的数据,由于这些变量的数量很大,所以这使我们就会想到能不能使用循环来像生成数据值一样生成变量名呢,当然是可以的 ...
- python 根据字符串动态的生成变量名并且赋值
Python 动态的创建变量 一.子符串的形式 这是在今天的一个项目中,发现需要动态的创建很多变量.每个变量对应的值的来源都相同.在网上看了些资料,研究出了这个动态创建变量的牛逼方法. 所用的方法就是 ...
随机推荐
- Java 中的类
public class Test{ public static void main(String[] args){ Animal a1 = new Dog(); a1.shout();//编译通过 ...
- Zookeeper基本使用(转)
一.Zookeeper架构 云计算越来越流行的今天,单一机器处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,这些信 ...
- 使用word 2010 发布csdn博客
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- [解决]--java_out: User.proto: User.proto: Cannot generate Java output because the file 's
在使用 protocol buffer 的时候,用.proto文件生成代码文件时报错 使用命令 protoc.exe --java_out c:\logs\ User.proto User.proto ...
- Codeforces768C Jon Snow and his Favourite Number 2017-02-21 22:24 130人阅读 评论(0) 收藏
C. Jon Snow and his Favourite Number time limit per test 4 seconds memory limit per test 256 megabyt ...
- 3D 相关
1. STL 2. AMF 3. X3D 网址: http://www.web3d.org/x3d-resources/content/examples/X3dResources.html
- Python WebDriver + Firefox 文件下载
firefox可以通过 在地址栏输入:about:config 或about:aupport 来查看或修改配置信息. 这里有两种解决方式, 1.设置自动保存下载 如下图勾选:以后自动采用相同的动作处理 ...
- ISE、vivado、QuartusII调用notepad++、UE汇总(整理)
我已经用惯了notepad++编写Verilog代码,很喜欢这款编辑器,功能真的非常强大.所以,当需要对vivado.ISE或quartus ii中的工程进行Verilog代码上的编写或修改时,只需双 ...
- 使用JDK实现动态代理
- 好用的下拉第三方——nicespinner
1.简介 GitHub地址:https://github.com/arcadefire/nice-spinner Gradle中添加: allprojects { repositories { ... ...