0026-python实践笔记:批量定义全局变量

0026-python实践笔记:批量定义全局变量,第1张

目录

1.遍历列表的每个元素,使之成为全局变量,并生成元素指向的字典

2. 生成全局变量字典,字典值列表储存

3.生成一个全局变量字典

4.受列表 t 元素个数生成3个‘day’字典

5.生成键为'day'的全局变量字典

6.遍历列表生成全局变量列表字典


1.遍历列表的每个元素,使之成为全局变量,并生成元素指向的字典
t = ['one','two','three']
for i in t:
    globals() [i] = {}

print(i)
print(globals())

2. 生成全局变量字典,字典值列表储存
t = ['one']
for i in t:
    globals() [i] = {}
    globals() [i]['day'] = []

print(i)
print(globals())

3.生成一个全局变量字典
t = ['one']
for i in t:
    globals() [i] = {}
    globals() [i]['day'] = []
    for k in range(3):
        globals()[i]['day'].append(k)
        print(i)

print(i)
print(t)
print(globals())

4.受列表 t 元素个数生成3个‘day’字典
t = ['one','two','three']
for i in t:
    globals() [i] = {}
    globals() [i]['day'] = []
    for k in range(3):
        globals()[i]['day'].append(k)
        print(i)

print(i)
print(t)
print(globals())

5.生成键为'day'的全局变量字典
def fun():
    s = ['SH']
    for i in s:
        globals() [i] = {}
        globals() [i]['day'] = []
        for k in range(7):
            globals()[i]['day'].append(k)
    return SH

print(fun())
print(globals())

6.遍历列表生成全局变量列表字典
s = ['one','two','three']
def fun(a):
    s = ['SH']
    for i in s:
        globals() [i] = {}
        globals() [i][a] = []
        for k in range(3):
            globals()[i][a].append(0)
    return SH

list = []
for i in s:
    list.append(fun(i))
print(list)

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/717244.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存