作为def()中的参数的词数可变的方法
在当前代码中,我正在执行测试以处理数量可变的字典:我正在寻找一种方法来获取所有密钥及其值。在当前示例中,特别是在DictionnaryTest2功能中,我未能将 Tuple 更改为原始列表,以便获取密钥名称(并且错误即将出现):我想知道我是否使用了正确的方法(这是第一次),因此如何继续?#!/usr/bin/env python3# -*- coding: utf-8 -*-
import numpy as np
# A dictionnary is created
n = 10
m = 1
X = np.random.random( (n,m) )
Y = np.random.random( (n,m) )
Z = np.random.random( (n,m) )
MyDictionnary = {'Abcissa': X, 'Ordinate': Y, 'Altitude': Z}
MyDictionnary2 = {'Abcissa2': X, 'Ordinate2': Y, 'Altitude2': Z, 'Theta': (X+Y)}
del X, Y, Z
# Dictionnary keys are listed / the dictionnary is explicitly expressed
KeyList0 = list(MyDictionnary.keys())
print("Key list (explicitly) : {}".format(KeyList0))
# Dictionnary keys are listed / the dictionnary name is a variable
MyVar = 'MyDictionnary'
KeyList1 = list(locals().keys())
print("Key list (name=variable) : {}".format(KeyList1))
# Now inside a function with the dictionnary in argument
def DictionnaryTest1(MyDict):
NewVar = 'MyDict'
KeyListFunction = list(locals().keys())
print("Key list in a function : {}".format(KeyListFunction))
return
KeyList1 = DictionnaryTest1(MyDictionnary)
# A list a dictionnary names is now created
DictionnaryNamesTables = ['MyDictionnary', 'MyDictionnary2']
# just for printing the dictionnaries list
for i in range(len(DictionnaryNamesTables)):
print(DictionnaryNamesTables)
def DictionnaryTest2(*args):
# tests
print("Type args = {}".format(type(args)))
print("length args = {}".format(len(args)))
print("args = {}".format(args))
args = list(args)
print("length args (after list())= {}".format(len(args)))
NumberOfDictionnaries = len(args)
for i in range(len(args)):
NewVar = args#
print("NewVar = {}".format(NewVar))
KeyListFunction2 = list(locals().keys())
print("KeyListFunction2 = {}".format(KeyListFunction2))
return
KeyList2 = DictionnaryTest2(DictionnaryNamesTables)
你到底想达到什么目的?我关心所有这些动态代码创建你正在做 - 在这里使用。你真的需要这么做吗?这样的代码往往很难维护 我认为他们想解开功能调用DictionnaryNamesTablesKeyList2 = DictionnaryTest2(*DictionnaryNamesTables) 目前还不清楚你到底想实现什么目标?from itertools import chain
spam = {'foo':1, 'bar':2}
eggs = {'x':0, 'y':3}
dicts =
def get_keys(*args):
for some_dict in args:
print(list(some_dict.keys()))
def get_keys2(*args):
print(list(chain(*args)))
get_keys(*dicts)
get_keys2(*dicts) import numpy as np
from itertools import chain
# A dictionnary is created
n = 10
m = 1
X = np.random.random( (n,m) )
Y = np.random.random( (n,m) )
Z = np.random.random( (n,m) )
MyDictionnary = {'Abcissa': X, 'Ordinate': Y, 'Altitude': Z}
MyDictionnary2 = {'Abcissa2': X, 'Ordinate2': Y, 'Altitude2': Z, 'Theta': (X+Y)}
del X, Y, Z
# # Dictionnary keys are listed / the dictionnary is explicitly expressed
KeyList0 = list(MyDictionnary.keys())
print("Key list (explicitly) : {}".format(KeyList0))
## function get keys lists
def DictionnaryTest(*args):
return(list(chain(*args)))
# A list of dictionnary names is now created
DictionnaryNamesTable =
KeyList2 = DictionnaryTest(*DictionnaryNamesTable)
print("Key list2 : {}".format(KeyList2))
## list composed of a single dictionnary
DictionnaryNamesTable =
KeyList1 = DictionnaryTest(*DictionnaryNamesTable)
print("Key list1 : {}".format(KeyList1)) 同样,避免这样做,因为该代码更难读取,也更难维护。如果您有大量需要使用的东西,请将这些东西作为 buran 显示放入集合中。 我不熟悉数据库,但感谢有关Redis的信息,我会看看它。
页:
[1]