简体   繁体   中英

Find item in combined list

I want to combine list and do some tasks with its items. Then I want to know to which initial list belongs current item. How can I do it in less ugly way than now:

 combined_list = sorted(max_peaks_eph + min_peaks_eph) # number of lists is about 10
        for item in combined_list:
            #do some tasks....
            if something ...........
                if item in max_peaks_eph:
                    label = 'max_peaks_eph'
                elif item in min_peaks_eph:
                    label = 'min_peaks_eph'
                print (label)

#If you don't have duplicate ones then this will work better than yours

 combined_list = A+B+C
 sorted_list = sort(combined_list)
 item = something # that you need to find
 index = combined_list(item)
 if index< len(A):
   print("Item in A")
 elif index < len(A) +len(B):
  print("Item in B")
 else:
  print("Item in C)

Why this method is efficient is we don't need to iterate each time to check the element in the list we can check with length of the list.

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