I have two python lists as follow:
ListA = [a1,a2,a1,a3,a2,a4,a5,a4]
ListB = [b1,b2,b1,b3,b2,b4,b5,b4]
What I want is to find the equal elements in the two lists and print them in a file. I have found the equal elements in lists and add them to two new lists as follow:
[a1,a2,a4]
[b1,b2,b4]
I want to compare some parameters with elements in ListA and if an element in ListA and the parameter is equal print the corresponding element in ListB. I do this as follow.
for i,j in enumerate(ListA):
if j == paramname:
filelines.append('%sTransferSourceName = "%s"\n'%(indent,ListB[i]))
My problem is that the element are not in the order for ListB. It is added like below:
b2,b4,b1
So the whole order get mixed up.
Note that the number of letters in each element in the lists may differ.
Here is the code I have done so far:
def ProcessLinks():
duplicates = [x for x in linkparamArray if linkparamArray.count(x) > 1]
linkstemp = list(set(duplicates))
for i in linkstemp:
links.append(i)
def ProcessLinks2():
duplicates2 = [x for x in linkparameterArray if linkparameterArray.count(x) > 1]
linkstemp2 = list(set(duplicates2))
for j in linkstemp2:
linkparameters.append(j)
And here is the comparing code:
paramname = a1
for i,j in zip(linkparameters,links):
if i == paramname:
filelines.append('%s TransferSourceName = "%s"\n(indent,j))
You can use collections.Counter to determine duplicate elements.
As per the Comment of OP as the order is important than the following solution might work using (OrderedDict)[http://docs.python.org/library/collections.html#collections.OrderedDict]