1

I have two lists:

A = [476, 1440, 3060, 3060, 500,500]
B = [0,4,10,15]

NOTE: length of list B will always be equal to individual plus number of paired values in list A. Updated list B will match the length of the list A.

I want to check for duplicate values in the list A and use its indexed position to duplicate corresponding value in the list B. For example, list A has A[2]=A[3] and A[4]=A[5] (3060 and 500), so based on this I want to duplicate B[2] and B[3] and add these at B[3] and B[5] respectively. So updated list B would look like:

B = [0,4,10,10,15,15] 

I tried following to get start index of duplicate values:

C = [x==A[i-1] for i,x in enumerate(A)][1:]

for idx,l in enumerate(C):
    if l == True:
        print "idx"

But running short to incorporate this change into list B. Any suggestions would be appreciative.

5
  • 1
    List B would be unique, always? and what if the same number occurs more than twice in A Commented Jan 22, 2014 at 7:51
  • Both lists will continuously change. Same number will only occur twice in list A, not more than that. Commented Jan 22, 2014 at 7:52
  • If A = [200, 500, 3060, 3060, 500] and B = [0,4,10,15] what would be your answer? Commented Jan 22, 2014 at 7:55
  • This will not be the case. Duplicate values in A will be adjacent to each other. Commented Jan 22, 2014 at 7:59
  • You should add a better example, one that contains multiple repeated items. Commented Jan 22, 2014 at 8:11

1 Answer 1

2

Here is a naive implementation (not sure if I got your requirement correct):

>>> from itertools import groupby
>>> A = [476, 1440, 3060, 3060, 500, 500]
>>> B = [0, 4, 10, 15]
>>> Result = []
>>> for i, g in enumerate(groupby(A)):
        Result += [B[i]] * len(list(g[1]))


>>> Result
[0, 4, 10, 10, 15, 15]
Sign up to request clarification or add additional context in comments.

1 Comment

an additional thing: if I have more than one list to adjust, how to include them as part of the single for loop you have provided in answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.