小白教程

 找回密码
 立即注册
查看: 11203|回复: 6

作为def()中的参数的词数可变的方法

[复制链接]

1

主题

1

帖子

3

积分

新手上路

Rank: 1

积分
3
发表于 2021-5-21 04:17:13 | 显示全部楼层 |阅读模式
在当前代码中,我正在执行测试以处理数量可变的字典:我正在寻找一种方法来获取所有密钥及其值。在当前示例中,特别是在DictionnaryTest2功能中,我未能将 Tuple 更改为原始列表,以便获取密钥名称(并且错误即将出现):我想知道我是否使用了正确的方法(这是第一次),因此如何继续?
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import numpy as np

  4. # A dictionnary is created
  5. n = 10
  6. m = 1
  7. X = np.random.random( (n,m) )
  8. Y = np.random.random( (n,m) )
  9. Z = np.random.random( (n,m) )
  10. MyDictionnary = {'Abcissa': X, 'Ordinate': Y, 'Altitude': Z}
  11. MyDictionnary2 = {'Abcissa2': X, 'Ordinate2': Y, 'Altitude2': Z, 'Theta': (X+Y)}
  12. del X, Y, Z

  13. # Dictionnary keys are listed / the dictionnary is explicitly expressed
  14. KeyList0 = list(MyDictionnary.keys())
  15. print("Key list (explicitly) : {}".format(KeyList0))


  16. # Dictionnary keys are listed / the dictionnary name is a variable
  17. MyVar = 'MyDictionnary'
  18. KeyList1 = list(locals()[MyVar].keys())
  19. print("Key list (name=variable) : {}".format(KeyList1))


  20. # Now inside a function with the dictionnary in argument
  21. def DictionnaryTest1(MyDict):
  22.   NewVar = 'MyDict'
  23.   KeyListFunction = list(locals()[NewVar].keys())
  24.   print("Key list in a function : {}".format(KeyListFunction))
  25.   return
  26.    
  27. KeyList1 = DictionnaryTest1(MyDictionnary)



  28. # A list a dictionnary names is now created
  29. DictionnaryNamesTables = ['MyDictionnary', 'MyDictionnary2']

  30. # just for printing the dictionnaries list
  31. for i in range(len(DictionnaryNamesTables)):
  32.   print(DictionnaryNamesTables[i])
  33.    

  34. def DictionnaryTest2(*args):

  35.   # tests
  36.   print("Type args = {}".format(type(args)))
  37.   print("length args = {}".format(len(args)))
  38.   print("args = {}".format(args))
  39.   args = list(args)
  40.   print("length args (after list())= {}".format(len(args)))
  41.   NumberOfDictionnaries = len(args)
  42.    
  43.   for i in range(len(args)):
  44.     NewVar = args[i]  #
  45.     print("NewVar = {}".format(NewVar))
  46.     KeyListFunction2 = list(locals()[NewVar].keys())
  47.     print("KeyListFunction2 = {}".format(KeyListFunction2))

  48.   return

  49. KeyList2 = DictionnaryTest2(DictionnaryNamesTables)
复制代码


回复

使用道具 举报

0

主题

5

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2021-5-22 01:21:18 | 显示全部楼层
你到底想达到什么目的?我关心所有这些动态代码创建你正在做 - 在这里使用。你真的需要这么做吗?这样的代码往往很难维护
回复

使用道具 举报

0

主题

1

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2021-5-24 07:43:44 | 显示全部楼层
我认为他们想解开功能调用DictionnaryNamesTables
  1. KeyList2 = DictionnaryTest2(*DictionnaryNamesTables)
复制代码
回复

使用道具 举报

1

主题

2

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2021-5-26 09:41:01 | 显示全部楼层
目前还不清楚你到底想实现什么目标?
  1. from itertools import chain
  2. spam = {'foo':1, 'bar':2}
  3. eggs = {'x':0, 'y':3}

  4. dicts = [spam, eggs]

  5. def get_keys(*args):
  6.     for some_dict in args:
  7.         print(list(some_dict.keys()))

  8. def get_keys2(*args):
  9.     print(list(chain(*args)))

  10. get_keys(*dicts)
  11. get_keys2(*dicts)
复制代码
回复

使用道具 举报

0

主题

5

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2021-5-28 15:25:45 | 显示全部楼层
  1. import numpy as np
  2. from itertools import chain

  3. # A dictionnary is created
  4. n = 10
  5. m = 1
  6. X = np.random.random( (n,m) )
  7. Y = np.random.random( (n,m) )
  8. Z = np.random.random( (n,m) )
  9. MyDictionnary = {'Abcissa': X, 'Ordinate': Y, 'Altitude': Z}
  10. MyDictionnary2 = {'Abcissa2': X, 'Ordinate2': Y, 'Altitude2': Z, 'Theta': (X+Y)}
  11. del X, Y, Z

  12. # # Dictionnary keys are listed / the dictionnary is explicitly expressed
  13. KeyList0 = list(MyDictionnary.keys())
  14. print("Key list (explicitly) : {}".format(KeyList0))


  15. ## function get keys lists
  16. def DictionnaryTest(*args):
  17.     return(list(chain(*args)))

  18. # A list of dictionnary names is now created
  19. DictionnaryNamesTable = [MyDictionnary, MyDictionnary2]

  20. KeyList2 = DictionnaryTest(*DictionnaryNamesTable)
  21. print("Key list2 : {}".format(KeyList2))


  22. ## list composed of a single dictionnary
  23. DictionnaryNamesTable = [MyDictionnary]
  24. KeyList1 = DictionnaryTest(*DictionnaryNamesTable)
  25. print("Key list1 : {}".format(KeyList1))
复制代码
回复

使用道具 举报

1

主题

3

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2021-5-30 06:39:05 | 显示全部楼层
同样,避免这样做,因为该代码更难读取,也更难维护。如果您有大量需要使用的东西,请将这些东西作为 buran 显示放入集合中。
回复

使用道具 举报

4

主题

6

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2021-5-30 22:01:00 | 显示全部楼层
我不熟悉数据库,但感谢有关Redis的信息,我会看看它。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 12:28 , Processed in 0.030047 second(s), 29 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表