How to print out a dict with nice output - python-3.x

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)]

Related

How to change two-dimension label to one-dimension label?

I have a two-dimension (10,2) coordinate which indicates each points label, like
coord_list = [(19, 17), (19, 17), (5, 26), (19, 17), (5, 26), (5, 26), (15, 17), (19, 5), (18, 6), (5, 26)]
I want to change it to a label list that only have one dimension (10,1),(assign a "label" to every unique item and replace each item by its label),like
label_list = [1,1,0....2,3]
I just want to classified points that have same coordinate in a same label, is there some more simple way can achieve it?
I tried to use this code,
label_list = []
for idx, coord in enumerate(coord_list):
if coord == (19,17):
label = 1
label_list.append(label)
if ...
But the problem is I don't know how many different coordinate in my coord_list, so I cannot write all if sentence in my code
Here's what I think you're after. I convert the list to a set, which eliminates duplicates. Then back to a list, and I sort it. Then I map each element of the original list to its index in that sorted list. There are only 5 unique points here, so the indexes will be from 0 to 4:
coord_list = [(19, 17), (19, 17), (5, 26), (19, 17), (5, 26), (5, 26), (15, 17), (19, 5), (18, 6), (5, 26)]
a = sorted(list(set(coord_list)))
print(a)
b = [a.index(i) for i in coord_list]
print(b)
Output:
[(5, 26), (15, 17), (18, 6), (19, 5), (19, 17)]
[4, 4, 0, 4, 0, 0, 1, 3, 2, 0]

matplotlib.pyplot heatmap with strange range of values

I am plotting a heatmap that have many values set to zero, a few that are very low (0.001 to 0.05) and a few that are high (>0.9 up to 1). Is it possible to set a non-uniform colorbar spacing so the data can be better understood? How can that be done?
data = {(0, 33): 0.002, (0, 34): 0.007, (0, 35): 0.015, (0, 36): 0.014, (0, 37): 0.943, (0, 38): 0.01, (0, 51): 0.007, (1, 33): 0.002, (1, 34): 0.002, (1, 35): 0.005, (1, 36): 0.005, (1, 37): 0.977, (1, 38): 0.006, (1, 51): 0.003, (2, 34): 0.002, (2, 35): 0.003, (2, 36): 0.003, (2, 37): 0.99, (2, 38): 0.002, (3, 33): 0.002, (3, 34): 0.003, (3, 35): 0.005, (3, 36): 0.007, (3, 37): 0.978, (3, 38): 0.005, (3, 51): 0.001, (4, 37): 1.0, (5, 37): 1.0, (12, 33): 0.003, (12, 34): 0.004, (12, 35): 0.018, (12, 36): 0.017, (12, 37): 0.931, (12, 38): 0.013, (12, 49): 0.001, (12, 51): 0.012, (13, 33): 0.003, (13, 34): 0.004, (13, 35): 0.009, (13, 36): 0.013, (13, 37): 0.944, (13, 38): 0.014, (13, 51): 0.012, (14, 34): 0.001, (14, 35): 0.002, (14, 36): 0.003, (14, 37): 0.993, (14, 38): 0.001, (15, 34): 0.001, (15, 35): 0.002, (15, 36): 0.003, (15, 37): 0.992, (15, 38): 0.001, (16, 37): 0.996, (16, 38): 0.004, (17, 37): 1.0, (18, 37): 1.0, (19, 37): 1.0, (20, 37): 1.0, (21, 37): 1.0, (22, 37): 1.0, (23, 37): 1.0, (24, 33): 0.004, (24, 34): 0.007, (24, 35): 0.032, (24, 36): 0.035, (24, 37): 0.903, (24, 38): 0.016, (24, 51): 0.003, (25, 33): 0.004, (25, 34): 0.006, (25, 35): 0.029, (25, 36): 0.034, (25, 37): 0.907, (25, 38): 0.016, (25, 51): 0.003, (26, 35): 0.004, (26, 36): 0.007, (26, 37): 0.972, (26, 38): 0.017, (27, 33): 0.004, (27, 34): 0.005, (27, 35): 0.007, (27, 36): 0.008, (27, 37): 0.961, (27, 38): 0.016, (28, 33): 0.005, (28, 34): 0.006, (28, 35): 0.008, (28, 36): 0.01, (28, 37): 0.961, (28, 38): 0.01, (29, 33): 0.002, (29, 34): 0.003, (29, 35): 0.023, (29, 36): 0.016, (29, 37): 0.943, (29, 38): 0.012, (30, 33): 0.001, (30, 35): 0.01, (30, 36): 0.001, (30, 37): 0.985, (30, 38): 0.004, (35, 37): 1.0, (36, 37): 1.0, (37, 37): 1.0, (38, 38): 1.0, (39, 49): 0.013, (39, 50): 0.012, (39, 51): 0.975, (40, 47): 0.18, (40, 49): 0.02, (40, 50): 0.126, (40, 51): 0.674, (41, 49): 0.012, (41, 50): 0.022, (41, 51): 0.966}
i_set, j_set = set(), set()
for k in data.keys():
i_set.add(k[0])
j_set.add(k[1])
i_set = list(i_set)
i_set.sort()
j_set = list(j_set)
j_set.sort()
# Make array
array = []
for i in i_set:
row = []
for j in j_set:
row.append( data.get((i,j), 0) )
array.append(row)
array = np.array(array)
# plot
fig, ax = plt.subplots()
im = ax.imshow(array)
# We want to show all ticks...
ax.set_xticks(np.arange(len(j_set)))
ax.set_yticks(np.arange(len(i_set)))
# ... and label them with the respective list entries
ax.set_xticklabels(j_set)
ax.set_yticklabels(i_set)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(i_set)):
for j in range(len(j_set)):
if array[i,j] != 0:
text = ax.text(j, i, array[i, j],
ha="center", va="center", color="w", fontsize='xx-small')
fig.tight_layout()
plt.show()
Furthermore, the top and bottom rows are "cut in half" instead of shown in full. How can I fix that?
EDIT
The last question, of how to fix the cut in half squares was solved. Still, the question on how to make the colorbar vary non-uniformly is open!

Log plots in matplotlib is creating two lines

I was wondering why my loglog plots are creating two lines when it's just the orange one I want and why the labels won't show? Any insight would be greatly appreciated.
import matplotlib.pyplot as plt
data1 = [(3, 5034), (2, 4596), (4, 1469), (5, 1209), (6, 540), (7, 380), (8, 196), (9, 136), (10, 71), (11, 47), (12, 39), (13, 20), (14, 16), (15, 12), (16, 6), (18, 5), (17, 2), (19, 2), (22, 2), (1, 1), (24, 1), (20, 1)]
plt.loglog(data1, basex=2, basey=2, label='N1')
plt.show()

Type Conversion in Python using ord()

s = '4'
c = ord(s)
print ("After converting character to integer : ",end="")
print (c)
output: After converting character to integer : 52
I don't understand the value of output. Can someone please explain why 52 is printed?
If you want become a string into integer you need use the int() function, ord() function returns an integer representing Unicode code point for the given Unicode character.
Example
s = '4'
c = int(s)
print ("After converting character to integer : ",end="")
print (c)
Python’s built-in function chr() is used for converting an Integer to a Character, while the function ord() is used to do the reverse, i.e, convert a Character to an Integer.
For example :
print([(chr(i),i) for i in range(49, 123)])
output is :
[('1', 49), ('2', 50), ('3', 51), ('4', 52), ('5', 53), ('6', 54), ('7', 55), ('8', 56), ('9', 57), (':', 58), (';', 59), ('<', 60), ('=', 61), ('>', 62), ('?', 63), ('#', 64), ('A', 65), ('B', 66), ('C', 67), ('D', 68), ('E', 69), ('F', 70), ('G', 71), ('H', 72), ('I', 73), ('J', 74), ('K', 75), ('L', 76), ('M', 77), ('N', 78), ('O', 79), ('P', 80), ('Q', 81), ('R', 82), ('S', 83), ('T', 84), ('U', 85), ('V', 86), ('W', 87), ('X', 88), ('Y', 89), ('Z', 90), ('[', 91), ('\', 92), (']', 93), ('^', 94), ('_', 95), ('`', 96), ('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101), ('f', 102), ('g', 103), ('h', 104), ('i', 105), ('j', 106), ('k', 107), ('l', 108), ('m', 109), ('n', 110), ('o', 111), ('p', 112), ('q', 113), ('r', 114), ('s', 115), ('t', 116), ('u', 117), ('v', 118), ('w', 119), ('x', 120), ('y', 121), ('z', 122)]
The ord() function takes a string argument of a single Unicode character and returns its integer Unicode code point value. It does the reverse of chr().

python sqlite3 List

I have a question about sqlite3 in python, it is about the list problem.
Here is the question:
Write a function getMay(dbName) that takes as a parameter the filename of above database and returns two lists, one with the days and one with the temperatures at noon on those days.
Here is My code:
import sqlite3
def getMay(dbName):
conn = sqlite3.connect(dbName)
cur = conn.cursor()
cur.execute('select Day,Temp from May14 where Time= "12:00" order by Day ASC')
print(cur.fetchall())
cur.close()
conn.close()
Here is my output:
[(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7,
9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30,
22.5), (31, 21.0)]
But the correct output should look like:
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], [13.7, 11.1, 12.2, 13.2,
12.9, 12.5, 9.6, 11.6, 13.2, 19.2, 21.7, 15.2, 11.9, 16.4, 12.2, 10.1,
9.8, 16.2, 21.5, 17.8, 17.0, 18.6, 16.5, 21.2, 25.4, 27.8, 27.3, 13.7,
15.0, 22.5, 21.0])
Anyone know how to solve this problem?
Please help! Thank!
one easy solution could be this:
dayTemp_list = [(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7, 9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30, 22.5), (31, 21.0)]
days = []
temp = []
for i in dayTemp_list:
days.append(i[0])
temp.append(i[1])
result = (days,temp)
print result

Resources