简体   繁体   中英

How to extract a sublist from another list based on value with indices

I need to get a sublist called invalids from another list called opened_list that pulls the records containing the value 12 along with their index in the list. I have tried the below code

x = opened_list
print (x.index(-1))

I want the output to be something like

  • 25 12
  • 32 12
  • 74 12
a=[1,12,3,4,12,5,12]                                                    
b=[]                                                                    
for idx,item in enumerate(a): 
    if item==12: b.append((idx,item)) 
print(b)                                                                

prints: [(1, 12), (4, 12), (6, 12)]

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