Split string with double enter in Python - python

I have something like that:
1
2
3
4
5
6
And I want it to have :
[[1,2,3],[4,5,6]]
How can I do it? Thanks a lot

With list comprehension
import os
src = """1
2
3
4
5
6"""
print [[int(x) for x in sub.split(os.linesep)] for sub in src.split(os.linesep*2)]
#special case if this is for windows and src is a string
print [[int(x) for x in sub.split('\n')] for sub in src.split('\n\n')]
would return
[[1, 2, 3], [4, 5, 6]]

Try this:
input = '''1
2
3
4
5
6'''
def parse(string):
out = []
groups = string.split('\n\n') # Split by empty line
for group in groups:
out.append([item.strip() for item in group.split('\n')])
return out
print(parse(input))

Related

How to take in both input split by space(s) and by line(s)

for example I have this 2 inputs:
2 5 2
and:
2
5
2
How do I take all of them using the same code?
Read lines of input and split them until you get 3 total values.
inputs = []
while len(inputs) < 3:
values = input().split()
inputs.extend(values)
print(inputs) # this will print [2, 5, 2]

How to change a list [1,2,3] into 1 2 3 with spaces in between?

I created a list
x = list(range(0,5))
which gives the result [0,1,2,3,4] but I want to convert it as 0 1 2 3 4
How do I do that?
myList = [1, 2, 3]
print(' '.join(myList))
Try using * to unpack the list
print(*[1,2,3]) # -> 1 2 3

Python: print without overwriting printed lines

I have
for i in range(0, 11): print i, "\n", i
I'd like my python program to print this way for each for loop
1st loop:
1
1
2nd loop:
1
2
2
1
3rd loop:
1
2
3
3
2
1
I've tried using \r\n or \033[1A but they just overwrite the previous line. Is there a way I can "push" the outputted line down so I don't overwrite it?
One way to do this,
def foo(x, limit):
if x < limit :
print x
foo(x + 1, limit)
print x
foo(1, 11)
It's not possible to do it in 1 for loop as you're currently trying.
As I suggested, you can do it like this by using lists
>>> l1 = []
>>> l2 = []
>>> for i in range(0, 11):
... l1.append(i)
... l2 = [i] + l2
>>> l1.extend(l2)
>>> for i in l1:
... print i
0
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1
0
Concatenation of two list generators.
>>> ladder = [x for x in range(5)] + [x for x in range(5,-1,-1)]
>>> ladder
[0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
>>> for x in ladder:
... print x
...
0
1
2
3
4
5
4
3
2
1
0
>>>
one of ways to solve this
def two_side_print(start, end):
text = [str(i) for i in range(start,end)]
print '\n'.join(text), '\n', '\n'.join(reversed(text))
two_side_print(1, 11)
Another option is to save the printed values in a list and print that list in reverse order in each loop iteration.
My suggestion:
l = []
for i in range(1, 5):
print 'Loop '+str(i) # This is to ease seeing the printing results
for val in l:
print val
print i
l.insert(len(l),i)
for val in reversed(l):
print val
The output for a loop iterating from 0 to 5:
Loop 1
1
1
Loop 2
1
2
2
1
Loop 3
1
2
3
3
2
1
Loop 4
1
2
3
4
4
3
2
1
I hope this is what you are looking for.
You can use a recursive function to help here:
def print_both_ways(start, end):
print(start)
if start != end:
print_both_ways(start+1, end)
print(start)
Usage:
print_both_ways(1,3) # prints 1,2,3,3,2,1 on separate lines

List Comprehension Nested Loop, Multiple Operations

Given a list like so:
[[1,2,3],[4,5,6],[7,8,9]]
I'm trying to get my output to look like this, with using only a list comprehension:
1 2 3
4 5 6
7 8 9
Right now I have this:
[print(x,end="") for row in myList for x in row]
This only outputs:
1 2 3 4 5 6 7 8 9
Is there a way to have it break to a new line after it processes each inner list?
You could do like the below.
>>> l = [[1,2,3],[4,5,6],[7,8,9]]
>>> for i in l:
print(' '.join(map(str, i)))
1 2 3
4 5 6
7 8 9
You can do as follows:
print("\n".join(" ".join(map(str, x)) for x in mylist))
Gives:
1 2 3
4 5 6
7 8 9
>>> l = [[1,2,3],[4,5,6],[7,8,9]]
>>> for line in l:
print(*line)
1 2 3
4 5 6
7 8 9
A good explanation of the star in this answer. tl;dr version: if line is [1, 2, 3], print(*line) is equivalent to print(1, 2, 3), which is distinct from print(line) which is print([1, 2, 3]).
I agree with Ashwini Chaudhary that using list comprehension to print is probably never the "right thing to do".
Marcin's answer is probably the best one-liner and Andrew's wins for readability.
For the sake of answering the question that was asked...
>>> from __future__ import print_function # python2.x only
>>> list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [print(*x, sep=' ') for x in list]
1 2 3
4 5 6
7 8 9
[None, None, None]
Try this:
print("\n".join([' '.join([str(val) for val in list]) for list in my_list]))
In [1]: my_list = [[1,2,3],[4,5,6],[7,8,9]]
In [2]: print("\n".join([' '.join([str(val) for val in list]) for list in my_list]))
1 2 3
4 5 6
7 8 9

get rid of commas in a list python

I have a list [1,2,4,7,5,2] where I want to get rid of the commas to make it [1 2 4 7 5 2]
how would I go about this?
np.random.randint(0,4,12) will print out like [0 3 4 1 3 4 2 1 2 4 3 4] and thats the kind of thing I want :)
You could do this:
out = '[' + ' '.join(str(item) for item in numbers)+ ']'
print out
In [7]: data = [1,2,4,7,5,2]
In [11]: '[{}]'.format(' '.join(map(str, data)))
Out[11]: '[1 2 4 7 5 2]'
or,
In [14]: str(data).replace(',','')
Out[14]: '[1 2 4 7 5 2]'
If you want a numpy ndarray, use:
np.array([1, 2, 4, 7, 5, 2])

Categories

Resources