所以我有这样一个问题:
array_of_things = [[is_shiny=False, is_bumpy=True]
[is_shiny=False, is_bumpy=False]
[is_shiny=True, is_bumpy=True]]
要访问项目,我将执行以下操作:
if array_of_things[1][1] == True: #If the second item in list is bumpy
print("The second item is bumpy")
但是,为了使我的代码更清晰,我希望数组中的每个项都可以像这样访问:
if array_of_things[1].is_bumpy == True: #If the second item in the list is bumpy
print("The second item is bumpy")
我该怎么做?任何帮助都将不胜感激
一个选项是使用} ,可以使用:
dict
,但如果您想确切地使用问题中给出的语法^{namedtuple
创建一个新的tuple
子类,其类型名为第一个参数。可以使用与普通tuple
一样的索引访问字段,也可以使用在第二个参数中传递的字段名访问字段如果这些“东西”对您的程序有重要意义,我建议您定义一个类,否则,请使用dicts:
相关问题 更多 >
编程相关推荐