class ModeBool():
def __init__(self, mode):
self.mode = mode
class IndexOfBool():
def __init__(self, lst):
'''
Creates a list of True's as it's elements based on the given number.
>>> i1 = IndexOfBool(5)
Should create -> [True, True, True, True, True]
'''
self.lst = [ModeBool(True) for i in range(lst)]
def bool_true(self):
new_lst = []
for index, element in enumerate(self.lst):
if element is True:
new_lst.append(index)
return new_lst
但是,当我调用 bool_true
时,它不会返回正确的结果:
i1 = IndexOfBool(10)
i1.bool_true()
[]
它应该返回什么:
i1 = IndexOfBool(10)
i1.bool_true()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
不太确定为什么它没有返回预期的结果。另外,如果我用 ModeBool(False)
替换 ModeBool(True)
,那么它应该创建一个 False
列表。如果我在 False
列表上调用 bool_true
,它应该返回 []
。
最佳答案
这一行
self.lst = [ModeBool(True) for i in range(lst)]
应该是
self.lst = [ModeBool(True).mode for i in range(lst)]
关于python - 返回列表中元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21338603/