0

I have a file with two columns, lets say A and B

A B
1 10
0 11
0 12 
0 15
1 90
0 41

I want to create a new column (a list), lets call the empty list C = []

I would like to loop through A, find if A == 1, and if it is I want to append the value of B[A==1] (10 in the first case) to C until the next A == 1 arrives.

So my final result would be:

A B C
1 10 10
0 11 10
0 12 10
0 15 10
1 90 90
0 41 90

I have tried using the for loop, but only to my dismay:

for a in A:
 if a == 1:
 C.append(B[a==1])
 elif a == 0:
 C.append(B[a==1])
asked Mar 6, 2015 at 13:17
7
  • It shouldn't be a is equal to one. That is the wrong condition for the desired result. Commented Mar 6, 2015 at 13:20
  • Are you multiplying 10 to every value with 1? Commented Mar 6, 2015 at 13:21
  • @MalikBrahimi I am not sure how to implement it using the for loop!! Commented Mar 6, 2015 at 13:22
  • What are you trying to do? I don't understand this algorithmically. Commented Mar 6, 2015 at 13:23
  • @MalikBrahimi I have two lists, given by A and B. I want a new list C as mentioned!! I am not sure what you don't understand. It has to loop through A, find if A == 0, if so append the value of B for which A == 0 to C Commented Mar 6, 2015 at 13:27

2 Answers 2

4

You could use another variable to keep the value of the last index in A that had a value of 1, and update it when the condition is met:

temp = 0
for index, value in enumerate(A):
 if value == 1:
 C.append(B[index])
 temp = index
 else:
 C.append(B[temp])

enumerate() gives you a list of tuples with index and values from an utterable. For A, it will be [(0, 1), (1, 0), (2, 0), (3, 0), (4, 1), (5, 0)].

P.S: When you try to address a list using a boolean (B[a == 1]) it will return the item in the first place if the condition is false (B[a != 1] => B[False] => B[0]) or the item in the second place if it's true (B[a == 1] => B[True] => B[1]).

answered Mar 6, 2015 at 13:23
Sign up to request clarification or add additional context in comments.

3 Comments

But what if A[0] == 0 ? Still use B[0] or a default value? The OP was not clear about that case.
@DonQuestion A[0] is always 1.. the list proceeds that way!
@DonQuestion I guess you could add a conditional that will only start appending to C on the first occurrence of 1, but as we don't know what is the purpose of this code I went with the simplest approach.
1

You may also try using groupby.

Though solution I have come up with looks a bit convoluted to me:

>>> from itertools import izip, groupby, count
>>> from operator import itemgetter
>>> def gen_group(L):
 acc = 0
 for item in L:
 acc += item
 yield acc
>>> [number_out for number,length in ((next(items)[1], 1 + sum(1 for _ in items)) for group,items in groupby(izip(gen_group(A), B), itemgetter(0))) for number_out in repeat(number, length)]
[10, 10, 10, 10, 90, 90]

The idea is to prepare groups and then use them to group your input:

>>> list(gen_group(A))
[1, 1, 1, 1, 2, 2]
answered Mar 6, 2015 at 13:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.