How can I print a range of results with a space between every three results?
When I print something like the following:
for i in range(1, 11):
print(i)
Output I get:
1
2
3
4
5
6
7
8
9
10
Expected output:
1
2
3
4
5
6
7
8
9
10
for i in range(1, 11):
print(i)
if i % 3 == 0:
print('')
Should do it!
Basically, it just checks that i is modulo 3. If it is, it will just print an empty line.
Example with a more complex loop
In the case that you have a loop that goes from a to b and that you want to print an empty line every c print, you can do:
for i in range(a, b):
print(i)
if (i - a + 1) % c == 0:
print("")
I want to iterate through multiple lists, but begin with index 0 for each list. For example, the code gives output
lists = [[1,2,3],[2,3,4],[1,6,5]]
p=0
for i in lists:
for li in i:
p += li
print(p)
1
3
6
8
11
15
16
22
27
but I want the output to be
1
3
6
2
5
9
1
7
12
I'm new to Python, is there other ways to implement this?
Set the value of p to 0 before the inner loop.
lists = [[1,2,3],[2,3,4],[1,6,5]]
for i in lists:
p = 0
for li in i:
p += li
print(p)
Just set p = 0 on every item of lists
lists = [[1,2,3],[2,3,4],[1,6,5]]
p=0
for i in lists:
p = 0
for li in i:
p += li
print(p)
The output is
1
3
6
2
5
9
1
7
12
Process finished with exit code 0
How do I make a list or column in pandas which number in this column increase itself by step of 1 and when it hits an certain number (let's say 5) it return to 1 and repeat this process?
My scripts like below and it won't work as expected:
i = 0
lista = []
for i in range(50):
i += 1
if i == 5:
continue
lista.append(i)
print(i)
# what I wanted from this code is like :
1
2
3
4
5
.
.
.
# repeat printing 1-5 for 10 times
First using loops in pandas is antipattern, mainly if exist vectorized solutions, here numpy.tile:
df = pd.DataFrame({'col': np.tile(np.arange(1, 6), 10)})
print (df.head(12))
col
0 1
1 2
2 3
3 4
4 5
5 1
6 2
7 3
8 4
9 5
10 1
11 2
In the simplest way that occurs to me:
lista = []
for i in range(50):
lista.append(i%5+1)
print(lista)
This could also be written as a list comprehension, which is only one line of code and way cooler ;)
lista = [ i%5+1 for i in range(50) ]
I am trying to print all the values of x not just 9 outside the for loop. What change do I need to make to my code?
for x in range(10):
x
print x
in:
vars = []
for i in range(10):
vars.append(i)
for i in vars:
print i
out:
0
1
2
3
4
5
6
7
8
9
It would be easier just to do print(range(10)), but you could also do:
aux = []
for x in range(10):
aux.append(x)
print(aux)
Use this
for x in range(10):
print(x+"\n")
the output will be
0
1
...
9
or
for x in range(10):
print(x+end=' ')
out
0 1 2 3 4 5 6 7 8 9
:)
(yes, I've searched all around for a solution, and, if did I see it, I wasn't able to relate to my issue. I'm new to Python, sorry!)
I've got a work to do, and it says to me:
"User will input X and Y. Show a sequence from 1 to Y, with only X elements each line."
e.g
2 4 as entrance
1 2
3 4
e.g 2 6
1 2
3 4
5 6
Okay... So, I thought on doing this:
line, final = input().split()
line = int(line)
final = int(final)
List = []
i = 0
total = (final // line)
spot = 0
correction = 0
k = 1
if i != final:
List = list(range(1, final + 1, 1))
i += 1
while k != total:
spot = line * k + correction
correction += 1
k += 1
list.insert(List, spot, '\n')
print(*List)
Ok. So I managed to build my List from 1 to the "final" var.
Also managed to find on which spots (therefore, var "spot") my new line would be created. (Had to use a correction var and some math to reach it, but it's 10/10)
So far, so good.
The only problem is this work is supposed to be delivered on URI Online Judge, and it DEMANDS that my result shows like this:
2 10 as entrance
1 2
3 4
5 6
7 8
9 10
And, using the code I just posted, I get this as a result:
1 2
3 4
5 6
7 8
9 10
Thus, it says my code is wrong. I've tried everything to remove those spaces (I think). Using sys won't work since it only prints one argument. Tried using join (but I could have done it wrong, as I'm new anyway)
Well, I've tried pretty much anything. Hope anyone can help me.
Thanks in advance :)
You have built a list that includes each necessary character, including the linefeed. Therefore, you have a list like this:
[1, 2, '\n', 3, 4, '\n'...]
When you unpack arguments to print(), it puts a separator between each argument, defaulting to a space. So, it prints 1, then a space, then 2, then a space, then a linefeed, then a space... And that is why you have a space at the beginning of each line.
Instead of inserting linefeeds into a list, chunk that list with iter and next:
>>> def chunks(x, y):
... i = iter(range(1, y+1))
... for row in range(y//x):
... print(*(next(i) for _ in range(x)))
... t = tuple(i)
... if t:
... print(*t)
...
>>> chunks(2, 6)
1 2
3 4
5 6
>>> chunks(2, 7)
1 2
3 4
5 6
7
The problem with the approach you're using is a result of a space being printed after each "\n" character in the series. While the idea was quite clever, unfortunately, I think this means you will have to take a different approach from inserting the newline character into the list.
Try this approach: (EDITED)
x, y = input().split()
x, y = int(x), int(y)
for i in range(1, y+1):
if i % x == 0 or i == y:
print(i)
else:
print(i, end=" ")
Output for 3 11
1 2 3
4 5 6
7 8 9
10 11
Output for 2 10
1 2
3 4
5 6
7 8
9 10
Use itertools to take from an iterable in chunks:
>>> import itertools
>>> def print_stuff(x,y):
... it = iter(range(1, y + 1))
... chunk = list(itertools.islice(it,X))
... while chunk:
... print(*chunk)
... chunk = list(itertools.islice(it,X))
...
>>> print_stuff(2,4)
1 2
3 4
>>>
And here:
>>> print_stuff(2,10)
1 2
3 4
5 6
7 8
9 10
>>>
I split user input into two string then convert them into int and comapre if y greater than x by 2 because this is minimum for drawing your sequence
Then i make a list from 1 to y
And iterate over it 2 element for each iteration printing them
x,y=input().split()
if int(y)>int(x)+2:
s=range(1,int(y)+1)
for i in range(0,len(s),2):
print(' '.join(str(d) for d in s[i:i+2]))
result:
1 2
3 4
5 6
7 8
9 10