Python 2.7, restart loop - python-2.7

i am trying to loop though a series of numbers and assign a constant index to them with restarting a loop at a certain number.
input numbers
0
3
6
9
12
15
18
21
0
3
6
9
12
15
18
21
......
the expected output should be
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
my code looks like this. I works but never stops.
How could i iterate over the input data?
count = 1
for line in in_file:
should_restart = True
while should_restart:
should_restart = False
for i in xrange(0,22,3):
print count
if i == 21:
should_restart = True
count+=1
break
Thanks!

Supposing you have an in_file like this (that's just an example yours is different but I wanted to do it with your given input):
in_file = "0 3 6 9 12 15 18 21 0 3 6 9 12 15 18 21"
in_file = in_file.split(" ")
you setup an end-condition for your counter and a starting count:
end_condition = 21
count = 1
then you iterate through your list:
for line in in_file:
line = int(line) # This is only needed if you have string-data
within the loop print the current count:
print(count, end=' ')
and later increment the counter if you reach the final value:
if line == end_condition:
count += 1

Related

Keep first record when event occurrs

I have the following data in Stata:
clear
* Input data
input grade id exit time
1 1 . 10
2 1 . 20
3 1 2 30
4 1 0 40
5 1 . 50
1 2 0 10
2 2 0 20
3 2 0 30
4 2 0 40
5 2 0 50
1 3 1 10
2 3 1 20
3 3 0 30
4 3 . 40
5 3 . 50
1 4 . 10
2 4 . 20
3 4 . 30
4 4 . 40
5 4 . 50
1 5 1 10
2 5 2 20
3 5 1 30
4 5 1 40
5 5 1 50
end
The objective is to take the first row foreach id when a event occurs and if no event occur then take the last report foreach id. Here is a example for the data I hope to attain
* Input data
input grade id exit time
3 1 2 30
5 2 0 50
1 3 1 10
5 4 . 50
1 5 1 10
end
The definition of an event appears to be that exit is not zero or missing. If so, then all you need to do is tweak the code in my previous answer:
bysort id (time): egen when_first_e = min(cond(exit > 0 & exit < ., time, .))
by id: gen tokeep = cond(when_first_e == ., time == time[_N], time == when_first_e)
Previous thread was here.

What will be the output of the following pseudo code for input 7?

Please help me to understand the following code and what will be the possiable output.
What will be the output of the following pseudo code for input 7?
1.Input n
2.Set m = 1, T = 0
3.if (m > n)
Go to step 9
5.else
T = T + m
m = m + 1
8.Go to step 3
9.Print T
0
n is less than n so go to step 9 which is print T which is equal to 0 as set in step 2.
T should be 28. It will loop till m>7 (since n=7) and in each iteration T adds m to itself, since T is 0 initially it is only summing up m after incrementing it by 1 in each iteration.So if you add 1+2+3.....+7 you get 28 and that is when the loop breaks since m is now equal to 8.
for m = 1 2 3 4 5 6 7 and for 8 m>n will be true and it will go to step 9
T=(T+M)= 1 3 6 10 15 21 28 basically T is a series where next is added as 2,3,4,5,6,7 to prev number 2 3 4 5 6 7 if one look from other angle

Find RSTART for multiple matches in a line

I am writing a program to examine the string STRING to see where it matches SUBSTRING using gawk. One problem I have run into is that the match function only gives the left most match in the string. My current thought is to use gsub to find out how many times the SUBSTRING is present and then use match multiple times using the last substring(STRING,RSTART+1) to find the true start positions of each position, of course with some edits to the code. I am wondering if there is an easier way than this, or a built in function that gives all RSTARTS.
Example:
STRING=DDDADDCDFFDFGSDD
SUBSTRING=D
EDIT:
I looked at the array function for match (thanks for pointing me to more up to date documentation than I had been reading). This still doesn't work, as it allows you to search for multiple things in the same string, but still only gives the left most location of each of these strings.
For example:
$ echo DDDADDCDFFDFGSDD | gawk '{match($0,/D/,a); for (i in a) print i,a[i]}'
0start 1
0length 1
0 D
it works to find the left most of multiple things
echo gDDDADDCDFFDFGSDD | gawk '{match($0,/(D)(A)/,a); for (i in a) print i,a[i]}'
0start 4
0length 2
1start 4
2start 5
2length 1
1length 1
0 DA
1 D
2 A
So we are still finding the left most match (which is what the documentation say it will do)
There isn't a native way to deal with this that i have found, so I wrote this function to do it. This will only work with version of gawk that allow for multidimensional arrays, though making this work with older versions of awk would be simple as well, though parsing afterwards would be more difficult.
The function searches through the string for the regex and populates an array MM. It returns -1 if there was an error, 0 if there were no matches found, else it returns the number of matches found.
function multiMatch(string,subs){
split("",MM,"")
RLENGTH=0
RSTART=0
t=0
s=string
if (length(string) == 0 || length(subs) == 0){
print "Must have string and Regex to look for"
return -1
}
while (1) {
t=RSTART+t
s=substr(string,t+1)
if ( length(s) == 0 ){
break
}
match(s,subs)
if (RLENGTH == -1) {
break
}
found=substr(string,0,length(string)-(length(string)-t-RSTART+1))"-"substr(string,t+RSTART,RLENGTH)"-"substr(string,t+RSTART+RLENGTH);
MM[n]["RSTART"]=RSTART
MM[n]["RLENGTH"]=RLENGTH
MM[n]["STR"]=found
n++
}
return n
}
Example
echo doogggogogggggggooogggogggggooogoooggoooo g*o | awk '
BEGIN{PROCINFO["sorted_in"]="#ind_num_asc"}
{
print "Found "multiMatch($1,$2)" Matches"
for (x in MM) {
print x,MM[x]["RSTART"],MM[x]["RLENGTH"],MM[x]["STR"]
}
}'
OUTPUT
Found 40 Matches
2 1 d-o-ogggogogggggggooogggogggggooogoooggoooo
1 1 1 do-o-gggogogggggggooogggogggggooogoooggoooo
2 1 4 doo-gggo-gogggggggooogggogggggooogoooggoooo
3 1 3 doog-ggo-gogggggggooogggogggggooogoooggoooo
4 1 2 doogg-go-gogggggggooogggogggggooogoooggoooo
5 1 1 dooggg-o-gogggggggooogggogggggooogoooggoooo
6 1 2 doogggo-go-gggggggooogggogggggooogoooggoooo
7 1 1 doogggog-o-gggggggooogggogggggooogoooggoooo
8 1 8 doogggogo-gggggggo-oogggogggggooogoooggoooo
9 1 7 doogggogog-ggggggo-oogggogggggooogoooggoooo
10 1 6 doogggogogg-gggggo-oogggogggggooogoooggoooo
11 1 5 doogggogoggg-ggggo-oogggogggggooogoooggoooo
12 1 4 doogggogogggg-gggo-oogggogggggooogoooggoooo
13 1 3 doogggogoggggg-ggo-oogggogggggooogoooggoooo
14 1 2 doogggogogggggg-go-oogggogggggooogoooggoooo
15 1 1 doogggogoggggggg-o-oogggogggggooogoooggoooo
16 1 1 doogggogogggggggo-o-ogggogggggooogoooggoooo
17 1 1 doogggogogggggggoo-o-gggogggggooogoooggoooo
18 1 4 doogggogogggggggooo-gggo-gggggooogoooggoooo
19 1 3 doogggogogggggggooog-ggo-gggggooogoooggoooo
20 1 2 doogggogogggggggooogg-go-gggggooogoooggoooo
21 1 1 doogggogogggggggoooggg-o-gggggooogoooggoooo
22 1 6 doogggogogggggggooogggo-gggggo-oogoooggoooo
23 1 5 doogggogogggggggooogggog-ggggo-oogoooggoooo
24 1 4 doogggogogggggggooogggogg-gggo-oogoooggoooo
25 1 3 doogggogogggggggooogggoggg-ggo-oogoooggoooo
26 1 2 doogggogogggggggooogggogggg-go-oogoooggoooo
27 1 1 doogggogogggggggooogggoggggg-o-oogoooggoooo
28 1 1 doogggogogggggggooogggogggggo-o-ogoooggoooo
29 1 1 doogggogogggggggooogggogggggoo-o-goooggoooo
30 1 2 doogggogogggggggooogggogggggooo-go-ooggoooo
31 1 1 doogggogogggggggooogggogggggooog-o-ooggoooo
32 1 1 doogggogogggggggooogggogggggooogo-o-oggoooo
33 1 1 doogggogogggggggooogggogggggooogoo-o-ggoooo
34 1 3 doogggogogggggggooogggogggggooogooo-ggo-ooo
35 1 2 doogggogogggggggooogggogggggooogooog-go-ooo
36 1 1 doogggogogggggggooogggogggggooogooogg-o-ooo
37 1 1 doogggogogggggggooogggogggggooogoooggo-o-oo
38 1 1 doogggogogggggggooogggogggggooogoooggoo-o-o
39 1 1 doogggogogggggggooogggogggggooogoooggooo-o-

Python: Index in for loop is resetting without a reason

First of all, I would like to apologize for my english since it is not my native language.
I'm having a quite crazy problem with the following code:
linecounter = []
for i in range(20):
linecounter.append("Color "+str(i)+"\n")
for line in linecounter:
color_list = range(20)
for j in range(len(color_list)):
stri = "Color " + str(j+1)
if stri in line:
print j
The result I expect:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
The result I'm getting:
0
1
2
3
4
5
6
7
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
0
16
0
17
0
18
Can somebody tell me how I get this result or how I get the result I want?
I would like to thank everyone who answers.
Sincerely, Nikster
Those extra zeroes are being printed because of the way the in operator works for strings. When line is "Color10" and stri is "Color1", then if stri in line evaluates to True, and prints the value of j, which is zero at the time.
Try using an equality comparison instead of in. You would also need to add a newline to the end of stri so that they compare properly.
stri = "Color " + str(j+1) + "\n"
if stri == line:
print j
This will print the numbers from 0 through 18. I don't entirely understand what you're trying to do, but if you want 19 to get printed, you could try not adding 1 to j:
stri = "Color " + str(j) + "\n"
if stri == line:
print j
Strange logic but:
linecounter = []
for i in range(20):
linecounter.append("Color "+str(i))
for line in linecounter:
color_list = range(20)
for j in range(len(color_list)):
stri = "Color " + str(j+1)
if stri == line:
print j

adjusting the distance between numbers on console

I've made a program to create the pascal's triangle. the program takes number of rows as input and displays the triangle on the console. I've used the setw() function to set the distance between numbers. it's of for unit single digits but when the numbers get greater than 10,the width is not being adjusted properly,right now I've :
if(P<10){
std::cout << P ;
std::cout <<std::setw(2);
}
if(P>=10){
std::cout<<std::setw(3) << P ;
std::cout<<std::setw(2);
}
here's the ouput from the console:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84126126 84 36 9 110
I want it to appear like a proper triangle,Could someone help me out please???
If you read e.g. this reference of std::setw you will see
The width property of the stream will be reset to zero (meaning "unspecified") if any of the following functions are called
And then goes on to list basically all output operators.
This means that when you do
std::cout <<std::setw(2);
the width will only be set for the next output operation. If you do any kind of output after that the width will be reset to zero.