简体   繁体   中英

How to return a list as frozenset type?

I wants to return a list as frozenset type and its type will be checked by returning function as given below

frozentSet = functionName(list1, list2)

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

Whenever I am returning any list as "frozenset" it gives the result as below, while I wants to return as given in above if condition.

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

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

Thanks for your prompt response. Please see the whole code below.

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"))

Maybe with a little more code we can help you out, I'm doing the same as you and everything is working fine.

Example code:

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")

output:

>> frozenset({'a', 'd', 'b', 'f', 'e', 'c'})
>> Return value is a frozenset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM