Nested Loops Python - python

Hi I am working with nested loops but can not figure out a way to start a new row for my nested loop below:
for i in range(0,10):
for j in range(10):
print(i,end =" ")
Output
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
The output I want is:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
and so on

Thank you but I found the answer!
for i in range(0,10):
for j in range(10):
print(i, end=" ")
print()

This (and minor variations) also work:
for i in range(10):
print((('%s '%i)*10)[:-1])

Related

How to get a uniqueId for the following colum in pandas dataframe?

I have the following column in my pandas dataframe named as FailureLabel
ID FailureLabel
0 1 1
1 2 1
2 3 1
3 4 0
4 5 0
5 6 0
6 7 0
7 8 1
8 9 1
9 10 0
10 11 0
11 12 1
12 13 1
I would like to assign a unique_id to this column such that eachs 1's have a unique id whereas all zeros + the next one have a common "unique id".
I tried using the following code ,
df['unique_id'] = (df['FailureLabel'] | (df['FailureLabel']!=df['FailureLabel'].shift())).cumsum()
which gives me the following output,
ID FailureLabel unique_id
0 1 1 1
1 2 1 2
2 3 1 3
3 4 0 4
4 5 0 4
5 6 0 4
6 7 0 4
7 8 1 5
8 9 1 6
9 10 0 7
10 11 0 7
11 12 1 8
12 13 1 9
But what I desire is,
ID FailureLabel unique_id
0 1 1 1
1 2 1 2
2 3 1 3
3 4 0 4
4 5 0 4
5 6 0 4
6 7 0 4
7 8 1 4
8 9 1 5
9 10 0 6
10 11 0 6
11 12 1 6
12 13 1 7
Use Series.shift with backfilling first value, compare by 1 and add cumulative sum:
df['unique_id'] = df['FailureLabel'].shift().bfill().eq(1).cumsum()
print (df)
ID FailureLabel unique_id
0 1 1 1
1 2 1 2
2 3 1 3
3 4 0 4
4 5 0 4
5 6 0 4
6 7 0 4
7 8 1 4
8 9 1 5
9 10 0 6
10 11 0 6
11 12 1 6
12 13 1 7

how to calculate the given factorial equation of huge numbers?

How can I find the last digit of X^(N!)?
X can go up to 10^9
N can go up to 10^18
I know how to do it when only one of them is large but not both.
ps: execution time is 1 sec
I have no proof for this but...
Let's assume our target function is:
import math
def pow_fact_mod_last_digit_exact(x, y):
return pow(x, math.factorial(y), 10)
but for large values of x and y this would just take too long.
This is actually equivalent to:
import math
def pow_fact_mod_last_digit(x, y):
return pow(x, math.factorial(min(y, 4)), 10)
To test it for the first few hundred numbers:
print(all(
pow_fact_mod_last_digit(x, y) == pow_fact_mod_last_digit_exact(x, y)
for x in range(-300, 300) for y in range(300)))
# True
How did I went for it (empirically)
Let us just observe how pow(x, y, 10) behaves for some values of x and y:
n = 20 # x
m = 24 # y
print(f'{"":2s}', end=' ')
for y in range(m):
print(f'{y:2d}', end=' ')
print()
for x in range(n):
print(f'{x:2d}', end=' ')
for y in range(m):
print(f'{pow(x, y, 10):2d}', end=' ')
print()
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8
3 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7
4 1 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4
5 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3
8 1 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2
9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
10 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
12 1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8
13 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7 1 3 9 7
14 1 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4 6 4
15 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
16 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3 1 7 9 3
18 1 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2
19 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9 1 9
So, it looks like to get pow(x, y, 10) you only need you only need to know x % 10 (of course) and (y - 1) % 4.
Now, the factorial of a number factorial(n) % k is 0 for n > k and we only need to take care, at most, for the case of n <= k.
For the case of k = 4, we have:
import math
print([(i, math.factorial(i) % 4) for i in range(10)])
# [(0, 1), (1, 1), (2, 2), (3, 2), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0)]
So we do not need to worry for values of y above 4 as they will behave like 4.
EDIT: Apparently this is obvious from Fermat's Little Theorem (but it was not obvious to me O:-) ) and #OneLyner's answer contain essentially the same observations as above, as well as the reference to the theorem in the comments.
Use the fact that
pow(x, k, 10) = pow(x % 10, 1 + (k-1) % 4, 10)
You just need to know the factorial modulo 4.
And obviously, N > 3 => N! % 4 == 0, which should make your life easy.

How to merge two dataframes and get the resulting dataframe be indexed properly (starting from zero up to the number of rows-1)

Let's say I have two dataframes:
df1:
0 1 2 3
0 2 2 2 2
1 2 2 2 2
2 3 1 1 1
3 3 1 1 1
4 3 1 1 1
5 3 1 1 1
df2:
0 1 2 3
0 9 9 9 9
1 9 9 9 9
2 9 9 9 9
3 9 9 9 9
4 9 9 9 9
I'd like to get this as their combination:
0 1 2 3
0 2 2 2 2
1 2 2 2 2
2 3 1 1 1
3 3 1 1 1
4 3 1 1 1
5 3 1 1 1
6 9 9 9 9
7 9 9 9 9
8 9 9 9 9
9 9 9 9 9
10 9 9 9 9
I have tried df = df1.append(df2) but i get this as the result:
0 1 2 3
0 2 2 2 2
1 2 2 2 2
2 3 1 1 1
3 3 1 1 1
4 3 1 1 1
5 3 1 1 1
0 9 9 9 9
1 9 9 9 9
2 9 9 9 9
3 9 9 9 9
4 9 9 9 9
How can I get the version I want?
If I understand your sample data frames correctly, you need to basically concat both frames ?
try
df = pd.concat([df1,df2],ignore_index=True)
print(df)
0 1 2 3
0 2 2 2 2
1 2 2 2 2
2 3 1 1 1
3 3 1 1 1
4 3 1 1 1
5 3 1 1 1
6 9 9 9 9
7 9 9 9 9
8 9 9 9 9
9 9 9 9 9
10 9 9 9 9
if regards to your own code, you are close but you need to add the ignore_index clause which will create a new index for you
df = df1.append(df2,ignore_index=True)
print(df)
0 1 2 3
0 2 2 2 2
1 2 2 2 2
2 3 1 1 1
3 3 1 1 1
4 3 1 1 1
5 3 1 1 1
6 9 9 9 9
7 9 9 9 9
8 9 9 9 9
9 9 9 9 9
10 9 9 9 9

Simple Python number generation

I'm missing something very simple. I'm a new Python student, so bear with me.
for i in range(10):
print("\n")
for x in range(10):
print(x, end = " ")
Sample output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
...
The "i" variable is setting the size of what I want to output, and in the above example I am showing the range of 0-9, 10 times. How would I alter this to display the following without using the print command by way of a string or an array? I'm trying to use two nested "for" statements, but I'm drawing a blank on what to use in place of the range command.
Desired output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
...
You are printing the variable from your inner loop (x) instead of the variable from the outer loop (i). Do it this way:
for i in range(10):
print("\n")
for x in range(10):
print(i, end = " ")
why to loop twice, we know str*n give n times str
>>> for x in range(10):
... print((str(x) + ' ')*10)
...
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
demo:
>>> 'a'*5
'aaaaa'
>>> 'a'*10
'aaaaaaaaaa'
>>> 'hello**'*10
'hello**hello**hello**hello**hello**hello**hello**hello**hello**hello**'
>>> '9'*10
'9999999999'
This also works:
for i in range(10):
print ' '.join(str(i) * 10)

How can I print this?

I am trying to teach myself python using interactivepython.org. I have come across a problem that I can not figure out. I have the slope and the spacing correct. I need it to print one less number every time. Could anybody help a newbie out?...
The code I have written:
numLines = 10
for i in range(numLines):
for k in range(i):
print(' ', end = ' ')
for j in range(1, numLines):
print(j, end = ' ')
print()
print(" ")
Prints:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Want to Print:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Well, The above answers are perfectly fine. But this is my way of doing things... :)
Code:
l = map(str,range(0,10))
for i in range(10):
print ' '.join(l[:len(l)-i]).rjust(20)
Output:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Hope this helps :)
Try this:
numLines = 10
for i in range(numLines, 0, -1):
for j in range(0, numLines - i):
print " ",
for k in range(0, i):
print k,
print
How about this
numLines = 10
for i in range(numLines):
print "".join (" " for j in range(i)) + " ".join (str(j) for j in range(numLines - i))
Output
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Never mind I figured it out. I had to decrement numLines by one inside the first for loop.

Categories