-2

我想返回一个作为frozenset类型的列表,它的类型将通过返回函数来检查,如下所示

frozentSet = functionName(list1, list2)

if type(frozentSet) == frozenset:
    print("Return value is a frozenset")
else:
    print("Retrun value is not a frozenset")

每当我将任何列表作为“frozenset”返回时,它都会给出如下结果,而我想返回上面给出的 if 条件。

return frozenset(['a','b','c','d','e','f'])

Output:
['f']
['b']
['a']
['d']
['e']
Returned value is not a frozenset

谢谢你的及时回复。请看下面的完整代码。

def returnFrozenset(listA):
    frsA = frozenset(listA)
    return frsA
 
if __name__ == '__main__':
   lst1_count = int(input().strip())
   lst1 = []

for _ in range(lst1_count):
    lst1_item = input()
    lst1.append(lst1_item)

   isFrozenSet = returnFrozenset(lst1)
   print("Returned value is {1} frozenset".format(isFrozenSet, "a" if type(frset) == frozenset else "not a"))
4

1 回答 1

0

也许用更多的代码我们可以帮助你,我和你做的一样,一切都很好。

示例代码:

def func(list1):
    return frozenset(list1)

if __name__ == "__main__":
    frozentSet = func(['a','b','c','d','e','f'])
    if type(frozentSet) == frozenset:
        print("Return value is a frozenset")
    else:
        print("Retrun value is not a frozenset")

输出:

>> frozenset({'a', 'd', 'b', 'f', 'e', 'c'})
>> Return value is a frozenset
于 2020-10-12T20:29:12.303 回答