简体   繁体   中英

How to check whether an item is in a 2d list or not?

recLst=\[\["10:20","DDSFE","Dyson Digital Slim Fluffy Extra",2599.0,"Iron"\],\["10:25","DV8SF","Dyson V8 Slim Fluffy Plus",1799.0,"Standard"\],\["13:30","DDSFE","Dyson Digital Slim Fluffy Extra",2599.0,"Purple"\],\["13:30","DDSFE","Dyson Digital Slim Fluffy Extra",2599.0,"Iron"\],\["16:15","DV11F","Dyson V11 Fluffy",2899.0,"Red"\],\["17:25","DV8SF","Dyson V8 Slim Fluffy Plus",1799.0,"Standard"\]\]

code ='DDDD'

for i in recLst:
if code not in i\[1\]:
print('No record found')

#Output
No record found
No record found
No record found
No record found
No record found
No record found

The output that i want was the python can go through the list and check whether the code exist or not, if not the system will print out 'No record found' but just for one time.

How to let the system go through the 2 dimensional list and check whether the code is at there or not, if not it will print an output of 'No record found'

you could try something like this

if any([code in i for i in recLst]):
    print("FOUND!")
else:
    print("NOT FOUND")

Good luck:)

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