python - Appending a value to an empty list according to conditions -


I have a table with many columns. I am interested in only two columns, call them A and B. I want to create an empty column, say c = [] and add it according to some situations.

  AB11011 12 12 14 14 13 12 12   

Take the above table as an example. If the value in column A == 1, then add the value of B to which for A == 1, column C (empty list) appears in column A. until the next A == 1. The next value of 1 in column A, above.

Then I would like to see my column C like this:

  ABC 1 10 10 11 11 10 0 12 10 1 09 09 0 14 09 0 13 09 1 12 12   

As you can see, for the first A == 1, B is the value of B in which for A == 1 next A == 1 appears it happens.

I was trying something like this, but it is not working:

  i in: if i == 1: C .append (B [A == i])   

But lose it through my entire column A and connects several lists

I'm about to do it How can i go

  a = [1,0,0,1,0,0,1] b = [10,11,12, 9, 14, 13, 12] C = [] temp = B [0] # For the boundary, start a floating variable in the first item (LAN (B)) in B. If A [I] == 1: temp = B [i] # Temporarily update only if 1 was in column C.append (temp) & gt; & Gt; & Gt; C [10, 10, 10, 9, 9, 9, 12]   

Walk through some iterations for the loop: < P> i = 0 , a [0] is 1 , becomes temporary B [ 0] which is 10
i = 1 , a [1] is 0 temporary resides B [0] which is still 10
i = 2 , a [2] is 0 , is temporary remains B [0] which still is 10 < I = 3 , a [3] is 1 , becomes temporary B [ 3] which 9
... is

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -