last day (16 days later) » 

7:41 PM
2
A: How to compare two unequal lists and append matching elements value back to the first List

Padraic CunninghamIf you values are immutable use a collections.Counter dict to count the occurrences of elements in List2 and add occurrence * : List1 = [['A'],['b']] List2 = ['a','A','c','A','b','b','A','b' ] from collections import Counter # gets the frequency count of each element in List2 c = Counter(List...

 
Woah!!! your code gives me the exact result i want, but one more thing i don't really understand how it works can you please explain because in my real program this values in the lists are all objects
 
@danidee, first what are the potential values in List1, are they always single strings in lists?
@danidee, are they immutable? If not we need to make a little change. How are they created?
 
they're created dynamically based on parameters gotten from the user
 
So they are class objects? What do you want to do with the objects after?
 
i'm actually trying to get two properties of the objects in the two lists and compare them against one another then if there is a match append the object itself back to the original list i.e if List1object.attribute == List2object.attribute: List1.append(List2object)
 
7:41 PM
If you actually want the same object then the first code is what you want, if you want copies of objects then you need to deepcopy.
 
i still don't know how to compare the object attributes against one another can you still help me out in that (like what i said in my previous comment)
 
can you add a minimal example of your actual input?
What do you actually want to append if you have different objects, is it the object itself if the object has the same attribute value? In List2 are the repeated elements actaually the same objects as in your example input?
 
it is the object i want to append if it has the same attribute value
 
But are the objects in List2 repeated objects, can they be identified by object1 is object2 or do we just consider objects to be equal if they have the same attribute value?
 
yes some are the same but i want to test the equality of the objects based on their attribute value for example if object1.id = object5.pred: do_something() and yes they should only be equal if their attributes are the same
 
7:41 PM
how many objects are in each sublist in List1?
 
just one object for each sublist
 
8:39 PM
ok I will add an answer in the morning
 
8:51 PM
ok
 

  last day (16 days later) »