简体   繁体   中英

Python program to check if a specified element presents in a tuple of tuples

I have a tuple which contains integers,strings and tuples(nested tuple). I want to check whether an element present in the tuple( in nested tuple also). this is what i have tried so far

tu=(1,'priya',2,(1,2,3,4),5,6,(7,'hi'),8,9)
if any('hi' in i for i in c for c in tu):
    print("exists")
else:
    print("does not exist")

it shows an error like this

if any('hi' in i for i in c for c in tu): NameError: name 'c' is not defined

kindly help

searched = 'hi'
for i in tu:
    if isinstance(i, tuple):
        if searched in i:
            print('exists')
            break
    elif searched == i:
        print('exists')
        break
else:
    print('does not exist')

this should work

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-2025 STACKOOM.COM