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 动态的创建变量 一.子符串的形式 这是在今天的一个项目中,发现需要动态的创建很多变量.每个变量对应的值的来源都相同.在网上看了些资料,研究出了这个动态创建变量的牛逼方法. 所用的方法就是 ...
随机推荐
- loadrunner--web_url函数用法
web_url语法: Int Web_url(const char *name, const char * url, <Lists of Attributes>, [EXTR ...
- chrome扩展安装图文教程
众所周知chrome的各类插件,扩展很丰富,也有很多经典的应用.但是谷歌经常被墙,无法访问,想要通过访问谷歌的应用市场来直接安装浏览器扩展会比较让人抓狂,好不容易无数次刷新后打开了页面,找到了想要的应 ...
- linux下的shell运算(加、减、乘、除
linux下的shell运算(加.减.乘.除 摘自:https://blog.csdn.net/hxpjava1/article/details/80719112 2018年06月17日 16:03: ...
- Error generating final archive: Unable to get debug signature key
在调试程序时,发生下面的错误: Error generating final archive: Unable to get debug signature key 解决办法: 删除下面的文件: C:\ ...
- oj 1002题 (大数题)
#include <stdio.h> #include <string.h> int main(void) { int q,j,h,k,l; int d; ],s2[];//题 ...
- Yii2 upload
http://webtips.krajee.com/advanced-upload-using-yii2-fileinput-widget/ http://webtips.krajee.com/upl ...
- HDU 6065 RXD, tree and sequence (LCA+DP)
题意:给定上一棵树和一个排列,然后问你把这个排列分成m个连续的部分,每个部分的大小的是两两相邻的LCA的最小深度,问你最小是多少. 析:首先这个肯定是DP,然后每个部分其实就是里面最小的那个LCA的深 ...
- 团体程序设计天梯赛L2-024 部落 2017-04-18 11:31 274人阅读 评论(0) 收藏
L2-024. 部落 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不 ...
- ZOJ2388 Beat the Spread! 2017-04-16 19:18 91人阅读 评论(0) 收藏
Beat the Spread! Time Limit: 2 Seconds Memory Limit: 65536 KB Superbowl Sunday is nearly here. ...
- java中null转换成其它类型
对null进行强转会不会抛错.测试结果是,如果把null强转给对象,是不会抛异常的,因为本身对象是可以为null的.但是如果是基本类型,比如 int i = (Integer)obj的强转,其实内部会 ...