简体   繁体   中英

How to print new line in nested list comprehension?

Am the new one to python. I want to display the possibilities of two dices using list comprehension. I got the output. But is it possible to print new line when y reaches 6(y==6)?

My coding:

d1=(1,2,3,4,5,6) 
d2=(1,2,3,4,5,6) 
print([(x,y) for x in d1 for y in d2])

My Output:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

My Expectation output:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), 
(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), 
(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), 
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), 
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), 
(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

Print has an optional parameter end This is normally a new line but you can modify it so that it is a space or whatever you want. You can use this to recreate your needed output:

d1=(1,2,3,4,5,6)
d2=(1,2,3,4,5,6)
# this is bad style and should be avoided because of side effects (see comments)
([print((x,y), end='\n' if y==len(d2) else ' ') for x in d1 for y in d2]) 

Better without side effects:

for tup in [(x,y) for x in d1 for y in d2]:
    print(tup, end='\n' if tup[1]==len(d2) else ' ')

Output:

(1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (1, 6)
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6)
(3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (3, 6)
(4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6)
(5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6)
(6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6)

And in case you really want a authentic list output (with all the special character (",", "[", "]") then you can do this as well with a bit more work:

def print_nice_list(d1,d2):
    print("[",end="")
    for tup in [(x,y) for x in d1 for y in d2]:
        end = ", "
        if tup[1] == len(d2) and tup[0]==len(d1):
            end = "]"
        elif tup[1] == len(d2):
            end = ",\n"
        print(tup, end=end )

print_nice_list(d1,d2) 

Output:

[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

Edit: Reworked the answer based on the comments.

If this is just for visualization, you should use pprint :

>>> import pprint
>>> lst = [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
>>> pprint.pprint(lst, compact=True, width=50)
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
 (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
 (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
 (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),
 (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
 (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM