Jamesy Jamesy - 5 months ago 10
Python Question

How to find list which items are not in output list and add list to output list?

I have a list with id's called

total_list
. I want to add the list in
total_list
to an
accepted_list
if none of the ids are found in the
accepted_list
. Here is a data set and the code I used. Any help is appreciated. Thanks.


Sample of total_list =
[[u'19'], [u'34', u'36'], [u'34', u'36'], [u'50', u'51'], [u'46', u'47'], [u'48', u'49'], [u'38', u'39', u'40'], [u'41', u'42', u'44'], [u'46', u'47', u'48'], [u'47', u'48', u'49'], [u'37', u'50', u'51'], [u'294', u'295', u'296'], [u'296', u'297', u'298'], [u'37', u'38', u'51'], [u'41', u'42', u'43', u'44'], [u'37', u'38', u'39', u'40'], [u'294', u'295', u'296', u'297'], [u'40', u'41', u'43', u'44'], [u'295', u'296', u'297', u'298'], [u'784', u'785', u'786', u'793'], [u'26', u'787', u'788', u'789', u'808'], [u'38', u'39', u'40', u'41', u'43'], [u'40', u'41', u'42', u'43', u'44'], [u'778', u'779', u'780', u'781', u'782'], [u'294', u'295', u'296', u'297', u'298'], [u'783', u'784', u'785', u'786', u'791', u'792'], [u'794', u'795', u'796', u'798', u'799', u'800'], [u'778', u'779', u'780', u'781', u'782', u'783'], [u'778', u'779', u'780', u'781', u'782', u'787', u'788'], [u'783', u'784', u'785', u'786', u'791', u'792', u'793'], [u'26', u'780', u'781', u'782', u'787', u'788', u'789'], [u'792', u'793', u'794', u'795', u'796', u'798', u'799'], [u'793', u'794', u'795', u'796', u'798', u'799', u'800'], [u'21', u'22', u'23', u'24', u'25', u'816', u'817'], [u'21', u'22', u'23', u'24', u'25', u'815', u'816', u'817'], [u'26', u'780', u'781', u'782', u'787', u'788', u'789', u'790'], [u'21', u'22', u'23', u'24', u'25', u'815', u'816', u'817'], [u'778', u'779', u'780', u'781', u'782', u'783', u'787', u'788', u'789'], [u'21', u'22', u'23', u'24', u'25', u'814', u'815', u'816', u'817'], [u'778', u'779', u'780', u'781', u'782', u'783', u'787', u'788', u'789'], [u'779', u'780', u'781', u'783', u'784', u'785', u'789', u'790', u'791'], [u'783', u'788', u'789', u'790', u'791', u'792', u'797', u'804', u'805'], [u'26', u'780', u'781', u'783', u'787', u'788', u'789', u'790', u'804'], [u'21', u'22', u'23', u'24', u'25', u'814', u'815', u'816', u'817'], [u'26', u'808', u'809', u'810', u'814', u'815', u'816', u'817', u'818'], [u'808', u'809', u'810', u'813', u'814', u'815', u'816', u'817', u'818'], [u'795', u'796', u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'811'], [u'785', u'786', u'791', u'792', u'793', u'794', u'795', u'797', u'798', u'799'], [u'801', u'806', u'809', u'810', u'811', u'812', u'813', u'814', u'815', u'817', u'818'], [u'801', u'802', u'803', u'806', u'809', u'810', u'811', u'812', u'813', u'814', u'815'], [u'784', u'785', u'790', u'791', u'792', u'793', u'794', u'797', u'798', u'799', u'803', u'804'], [u'21', u'22', u'23', u'24', u'25', u'808', u'809', u'814', u'815', u'816', u'817', u'818'], [u'792', u'793', u'794', u'795', u'796', u'797', u'798', u'799', u'800', u'801', u'802', u'803'], [u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'806', u'810', u'811', u'812', u'813'], [u'789', u'790', u'791', u'792', u'797', u'802', u'803', u'804', u'805', u'806', u'810', u'811'], [u'783', u'784', u'785', u'790', u'791', u'792', u'793', u'797', u'798', u'803', u'804', u'805'], [u'790', u'791', u'797', u'798', u'802', u'803', u'804', u'805', u'806', u'809', u'810', u'811'], [u'797', u'798', u'801', u'802', u'803', u'804', u'805', u'806', u'809', u'810', u'811', u'812', u'813'], [u'24', u'25', u'808', u'809', u'810', u'811', u'812', u'813', u'814', u'815', u'816', u'817', u'818'], [u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'804', u'805', u'806', u'810', u'811', u'812'], [u'805', u'806', u'808', u'809', u'810', u'811', u'812', u'813', u'814', u'815', u'816', u'817', u'818'], [u'797', u'800', u'801', u'802', u'803', u'804', u'805', u'806', u'809', u'810', u'811', u'812', u'813', u'814'], [u'22', u'23', u'24', u'25', u'808', u'809', u'810', u'812', u'813', u'814', u'815', u'816', u'817', u'818'], [u'21', u'22', u'23', u'24', u'25', u'808', u'809', u'810', u'813', u'814', u'815', u'816', u'817', u'818'], [u'791', u'792', u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'804', u'805', u'806', u'811', u'812'], [u'791', u'792', u'793', u'794', u'795', u'796', u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'805', u'806'], [u'790', u'791', u'792', u'793', u'797', u'798', u'799', u'800', u'801', u'802', u'803', u'804', u'805', u'806', u'811'], [u'801', u'802', u'804', u'805', u'806', u'808', u'809', u'810', u'811', u'812', u'813', u'814', u'815', u'817', u'818']]


So when I go through my
total_list
the first item is 19, which is not found in a list in my accepted_list(empty). As soon as it find an item in a list in
total_list
, which is in a list of my
accepted_list
, it won't add it to
accepted_list
.

accept_intersect = []
inaccept = False

for i in totallist:
if not accept_intersect:
accept_intersect.append(i)
continue
for p in i:
for a in accept_intersect:
if p in a:
inaccept = True
break
if inaccept is False:
accept_intersect.append(i)
elif inaccept is True:
pass

for a in accept_intersect:'
print a

Answer

It's probably quicker to use a dictionary to store if you have seen an id or not.

accept_intersect=[]
seen = {}

for item in totallist:
    for sub_item in item:
        if sub_item not in seen:
            accept_intersect.append(sub_item)
            seen[sub_item] = True;

print accept_intersect

If this answer helped you out, press the green tick on the left.

EDIT, UPDATED ON REQUEST

accept_intersect=[]
seen = {}

for item in totallist: 

    # Track if a subitem has been seen
    seen_flag = False

    for sub_item in item:

        # If it has been seen before
        # stop looking in this list
        if sub_item in seen:
            seen_flag = True
            break

        # Add seen item
        seen[sub_item] = True

    # If a subitem was seen, skip and continue
    # with the next loop.
    if seen_flag == True:
        continue

    # Only append if no items were seen before.
    accept_intersect.append(item)

#totallist = [['19'], ['19', '22']]
#accept_intersect -> [['19']]

#totallist = [['19'], ['19', '22'], ['43'], ['44', '45'], ['65', '19']]
#accept_intersect -> [['19'], ['43'], ['44', '45']]

Where you want to sort by highest to lowest doesn't concern this solution. When you construct your total_list input, just arrange it how you want.