List Comprehension Nested Loop, Multiple Operations - python
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
Related
How I can get the loop result as list or string
I write this code for i in range(1,6): for m in range(i): print(i) And I get the output like this: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 How I can get the output as an iterator (list or tuple) like this 1[1] 2[2,2] or simple string like this (In each category of numbers, we return to the line) 1 22 333
You can do this for iterable format output: for i in range(1,6): print(i, [i]*i) output -> 1 [1] 2 [2, 2] 3 [3, 3, 3] 4 [4, 4, 4, 4] 5 [5, 5, 5, 5, 5] or this for string format output: for i in range(1,6): print(str(i)*i) output -> 1 22 333 4444 55555
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
Slicing lists containing lists [duplicate]
This question already has answers here: Slicing list of lists in Python (6 answers) Closed 2 years ago. I have a list containing 3 lists: V1 = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3] V2 = [4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7] V3 = [8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11] V = [V1,V2,V3] I would like to slice V so that the indexes 4:7 of V1,V2 and V3 are selected and put into list Z. I tried doing it like this: Z = V[:,4:7] But I get the error: TypeError: list indices must be integers or slices, not tuple How do I correct this?
You need to use numpy to get an array that is sliceable in multi-dimension V = np.array([V1, V2, V3]) Z = V[:, 4:7] # V [[ 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3] [ 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7] [ 8 8 8 8 9 9 9 9 10 10 10 10 11 11 11 11]] # Z [[1 1 1] [5 5 5] [9 9 9]]
Your code will work if it was numpy.array V1 = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3] V2 = [4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7] V3 = [8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11] V = [V1,V2,V3] import numpy as np spam = np.array(V) print(spam[:,4:7]) output [[1 1 1] [5 5 5] [9 9 9]] without numpy you can use list comprehension: eggs = [item[4:7] for item in V] print(eggs) output [[1, 1, 1], [5, 5, 5], [9, 9, 9]]
You cannot simply slice a sublist, but you can iterate over each sublist and slice them one by one: V1 = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3] V2 = [4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7] V3 = [8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11] V = [V1, V2, V3] Z = [] for sublist in V: Z.append(sublist[4:7]) Or use numpy as suggested in other answers.
Split string with double enter in 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))
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])