简体   繁体   中英

How do I check if some elements of sublist exists in list (Python)?

I have a list, for example like this below:

list=[['a',0,1200],['b',1,900],['c',2,500],['a',1,200]]

and I need to check if these sub-lists contain the same element, for this example suppose to find the first element of each sub-list, so It will be found that ['a',1,200] was the same with ['a',0,1200] , because their first element are 'a'

So how do I find out all the duplicated sub-lists?

list[0] in list [1]

(in your case is False.)
for exemple you can check if first list in sec list.
if you want to check spcific (compere letter)
you can use to "extract" the list:

for x in list:
print(x)

and if you want to compare spcific the fisrt letter, but not all the sub-list.

counter = 0
for x in list:
  for i in list:
    if i != x and i[0] in x[0]:
        counter += 0.5

(sorry about my english)

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