Im wondering how I'm available to print the first 4 words from a list[:4] on the same line and then the next 4 [4:8] on the line under and so on. I have a dictionary with over 1000 words and I don't want 1 word to printed on 1 line because then the vertical output goes on forever. I would like to split them up by 4 on one line.
Any tips?
you can try using pprint module of python
Refer https://docs.python.org/3/library/pprint.html
for the dict:
#let your dict be dict1
val = list(dict1.values())
key = list(dict1.keys())
print(val)
print(key)
for i in range(len(val)):
if i // 4 == (i+1)// 4:
print(dict1[key[i]], end=" ")
else:
print(dict1[key[i]], end="\n")
Given dictionary
d = {i: i * i for i in range(200)}
do
from pprint import pprint
pprint(list(d.items()), compact=True)
which prints
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64),
(9, 81), (10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225),
(16, 256), (17, 289), (18, 324), (19, 361), (20, 400), (21, 441), (22, 484),
(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841),
(30, 900), (31, 961), (32, 1024), (33, 1089), (34, 1156), (35, 1225),
(36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681),
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209),
(48, 2304), (49, 2401), (50, 2500), (51, 2601), (52, 2704), (53, 2809),
(54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481),
(60, 3600), (61, 3721), (62, 3844), (63, 3969), (64, 4096), (65, 4225),
(66, 4356), (67, 4489), (68, 4624), (69, 4761), (70, 4900), (71, 5041),
(72, 5184), (73, 5329), (74, 5476), (75, 5625), (76, 5776), (77, 5929),
(78, 6084), (79, 6241), (80, 6400), (81, 6561), (82, 6724), (83, 6889),
(84, 7056), (85, 7225), (86, 7396), (87, 7569), (88, 7744), (89, 7921),
(90, 8100), (91, 8281), (92, 8464), (93, 8649), (94, 8836), (95, 9025),
(96, 9216), (97, 9409), (98, 9604), (99, 9801), (100, 10000), (101, 10201),
(102, 10404), (103, 10609), (104, 10816), (105, 11025), (106, 11236),
(107, 11449), (108, 11664), (109, 11881), (110, 12100), (111, 12321),
(112, 12544), (113, 12769), (114, 12996), (115, 13225), (116, 13456),
(117, 13689), (118, 13924), (119, 14161), (120, 14400), (121, 14641),
(122, 14884), (123, 15129), (124, 15376), (125, 15625), (126, 15876),
(127, 16129), (128, 16384), (129, 16641), (130, 16900), (131, 17161),
(132, 17424), (133, 17689), (134, 17956), (135, 18225), (136, 18496),
(137, 18769), (138, 19044), (139, 19321), (140, 19600), (141, 19881),
(142, 20164), (143, 20449), (144, 20736), (145, 21025), (146, 21316),
(147, 21609), (148, 21904), (149, 22201), (150, 22500), (151, 22801),
(152, 23104), (153, 23409), (154, 23716), (155, 24025), (156, 24336),
(157, 24649), (158, 24964), (159, 25281), (160, 25600), (161, 25921),
(162, 26244), (163, 26569), (164, 26896), (165, 27225), (166, 27556),
(167, 27889), (168, 28224), (169, 28561), (170, 28900), (171, 29241),
(172, 29584), (173, 29929), (174, 30276), (175, 30625), (176, 30976),
(177, 31329), (178, 31684), (179, 32041), (180, 32400), (181, 32761),
(182, 33124), (183, 33489), (184, 33856), (185, 34225), (186, 34596),
(187, 34969), (188, 35344), (189, 35721), (190, 36100), (191, 36481),
(192, 36864), (193, 37249), (194, 37636), (195, 38025), (196, 38416),
(197, 38809), (198, 39204), (199, 39601)]
I okay so i need the max of this list:
variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]
I´m using this line of code to do it but it doesn´t really work
print(max(variable,key=lambda x:x[1]))
The print says [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')] but the one i need are one of the other two ones and i dont know what to do. Thanks in advance and sorry if i messed something up,this is my first time using stack overflow for asking a question. Im using python btw
I believe you need sum of element 0 in your sub-list.
Ex:
variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]
print(max(variable, key=lambda x: sum(i for i,_ in x)))
# --> [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')]
I have a list [1,2,3,...,13] and a list ['clubs','hearts,'spades','diamonds']. How do I make a list of tuples looking like...
[(1,hearts),(1,clubs),(1,spades),(1,diamonds),(2,hearts)... and so on?
I have tried using a list comprehension but can't figure it out.
From the Python docs,
rank = [1, 2, 3]
suit = ['clubs', 'hearts', 'spades']
card = [(a, b) for a in rank for b in suit]
Another solution using product from itertools
from itertools import product
rank = range(1,14)
suit = ['clubs','hearts','spades','diamonds']
print( list(product(rank, suit)) )
you get:
[(1, 'clubs'), (1, 'hearts'), (1, 'spades'), (1, 'diamonds'), (2, 'clubs'), (2, 'hearts'), (2, 'spades'), (2, 'diamonds'), (3, 'clubs'), (3, 'hearts'), (3, 'spades'), (3, 'diamonds'), (4, 'clubs'), (4, 'hearts'), (4, 'spades'), (4, 'diamonds'), (5, 'clubs'), (5, 'hearts'), (5, 'spades'), (5, 'diamonds'), (6, 'clubs'), (6, 'hearts'), (6, 'spades'), (6, 'diamonds'), (7, 'clubs'), (7, 'hearts'), (7, 'spades'), (7, 'diamonds'), (8, 'clubs'), (8, 'hearts'), (8, 'spades'), (8, 'diamonds'), (9, 'clubs'), (9, 'hearts'), (9, 'spades'), (9, 'diamonds'), (10, 'clubs'), (10, 'hearts'), (10, 'spades'), (10, 'diamonds'), (11, 'clubs'), (11, 'hearts'), (11, 'spades'), (11, 'diamonds'), (12, 'clubs'), (12, 'hearts'), (12, 'spades'), (12, 'diamonds'), (13, 'clubs'), (13, 'hearts'), (13, 'spades'), (13, 'diamonds')]