This question already has answers here:
Scope of python variable in for loop
(10 answers)
Closed 3 years ago.
I want to print i and k till i is less than or equal to k.
in C++ the code can be given as:
for(i=0;i<k;i++){
cout<<i<<k;
k--;
}
I am not getting the correct output.
this is my code
k=5
for i in range(k):
print(i,k)
k-=1
the output i get is:
0 5
1 4
2 3
3 2
4 1
but i want to get:
0 5
1 4
2 3
is there someway to use the range() function for this?
For loops in Python are really for-each and suboptimal for your needs. Use while instead:
i = 0; k = 5
while i < k:
print(i,k)
i += 1
k -= 1
k=5
for i in range(k):
print(i,k)
if k<=i:
break
k-=1
This question already has answers here:
Determine whether integer is between two other integers
(16 answers)
Use two or more relational operators in one sentence in python
(2 answers)
Closed 4 years ago.
Running python v3.6.5 PyCharm
I have a simple for loop with an if and else statement with it:
for i in range(10):
if i > 3 < 5:
print(i, "first")
else:
print(i, "second")
The output I get is:
0 second
1 second
2 second
3 second
4 first
5 first
6 first
7 first
8 first
9 first
but shouldn't the output be:
0 second
1 second
2 second
3 second
4 first
5 second
6 second
7 second
8 second
9 second
Try it yourself. It doesn't make sense to me. Why is it doing this?
Don't really need to know
I know you might be thinking, why didn't you just say
if i == 4
but this is just a simplified problem in my program.
Thanks in advance
Hugo
A chain of operators like i > 3 < 5 is interpreted as
i > 3 and 3 < 5
where the "middle" operand(s) are repeated for the left and right operator. You want
3 < i and i < 5
, which can be abbreviated (using the reverse of the previous interpretation) as 3 < i < 5.
The correct syntax is:
if 3 < i < 5:
Be aware that Python is special here, and this construct won't work in most other languages (where you'd have to say something like 3 < i and i < 5 instead).
Please close if this is a duplicate, but this answer does not answer my question as I would like to print a list, not elements from a list.
For example, the below does not work:
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(%3s % mylist)
Desired output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Basically, if all items in the list are n digits or less, equal spacing would give each item n+1 spots in the printout. Like setw in c++. Assume n is known.
If I have missed a similar SO question, feel free to vote to close.
You can exploit formatting as in the example below. If you really need the square braces then you will have to fiddle a bit
lst = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
frmt = "{:>3}"*len(lst)
print(frmt.format(*lst))
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
items=range(10)
''.join(f'{x:3}' for x in items)
' 0 1 2 3 4 5 6 7 8 9'
If none of the other answers work, try this code:
output = ''
space = ''
output += str(list[0])
for spacecount in range(spacing):
space += spacecharacter
for listnum in range(1, len(list)):
output += space
output += str(list[listnum])
print(output)
I think this is the best yet, as it allows you to manipulate list as you wish. even numerically.
mylist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(*map(lambda x: str(x)+" ",a))
This question already has answers here:
Count all values in a matrix less than a value
(6 answers)
Closed 5 years ago.
Data = matrix R
First I wanted to count elements of each row
countR = np.count_nonzero(R, axis=1)
Then, I could get matrix countR.
[25 2 1 2 2 55 1 2 1 2 1 1 2 2 1 1 1 1 2 2 1 2 14 1 3 ..
Second, I want to count elements in matrix
"if element>1 "
So what I did is here
countR1 = pd.value_counts(countR.values, sort>1)
But there was an error.
How can i count elements?
you can do it easily like this:
y=np.array(countR)
len(y[y>1])
if I understand correctly you want to count all the elements that are larger than 1 in the matrix R.
you can filter the data-frame by doing so(to dispose of the elements that are larger than 1):
biggerThanOne = R[R<1]
you can then get the size of the array and get the number of elements:
biggerThanOne.size
if you mean you want to count the elements of countR you can practically do the same thing
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 8 years ago.
if I have a simple for loop like:
for i in range(10):
total = 0
total = total + i
print(total)
How can I print the loops once/together like:
1 2 3 4 5 6 7 8 9
Instead of every loop by itself:
1
2
3 etc
for i in range(10):
total = 0
total = total + i
print(total,end=" ")
The ability of print function, end of the each line changing from \n to " "