How to convert those deprecated parse commands to Python 3.10? - python-3.x

I have a couple of lines written with old Python 2.7's parser commands that don't seem to have equivalents in Python > 3.10. They are:
st = parser.expr(stringToConvert)
tup = st.totuple()
where stringToConvert is a <type 'unicode'> string such as, for example:
a5*X**5 + a4*X**4 + a3*X**3 + a2*X**2 + a1*X + a0 + log(5.0)
In Python 2.7, the two lines up above yield something like:
(258, (332, (306, (310, (311, (312, (313, (316, (317, (318, (319, (320, (321, (322, (323,
(324, (325, (1, 'a5'))))), (16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'),
(322, (323, (324, (325, (2, '5')))))))), (14, '+'), (321, (322, (323, (324, (325,
(1, 'a4'))))), (16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323,
(324, (325, (2, '4')))))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a3'))))),
(16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323, (324, (325,
(2, '3')))))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a2'))))), (16, '*'),
(322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323, (324, (325, (2, '2')))))))),
(14, '+'), (321, (322, (323, (324, (325, (1, 'a1'))))), (16, '*'), (322, (323, (324,
(325, (1, 'X')))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a0')))))), (14, '+'),
(321, (322, (323, (324, (325, (1, 'log')), (327, (7, '('), (335, (336, (306, (310, (311,
(312, (313, (316, (317, (318, (319, (320, (321, (322, (323, (324, (325, (2,
'5.0')))))))))))))))))), (8, ')'))))))))))))))))), (4, ''), (4, ''), (0, ''))
I've been stuck with these two lines for days now. I have no idea what the convoluted tuple above is all about nor to what it is good for, but the code needs it. All I know is that parser is now defunct and so far I've been unable to get along with the new parser(?). The <type 'unicode'> thing may also pose an extra problem, as it is also deprecated (or isn't it?). Any suggestions?
Thanks.

Related

How to print out a dict with nice output

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

How to know the max of this list of tuples? (Python)

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, '♣')]

after sorted one zip object , why list the object is empty

I zip two list into one,then I use sorted function to order it.
But after that, I list zip object will show empty [] .
[(11, 'a'), (1, 'b'), (15, 'c'), (2, 'd'), (3, 'e'), (19, 'f'), (12, 'g'), (23, 'h'), (5, 'i'), (14, 'j'), (21, 'k'), (9, 'l'), (8, 'm'), (22, 'n'), (20, 'o'), (0, 'p'), (6, 'q'), (25, 'r'), (13, 's'), (10, 't'), (18, 'u'), (17, 'v'), (4, 'w'), (24, 'x'), (16, 'y'), (7, 'z')]
[]
import random
eng=[ chr(i) for i in range(ord('a'),ord('a')+26,1)]
enum_eng=zip(list(range(len(eng))),eng)
random.shuffle(eng)
enum_eng=zip(list(range(len(eng))),eng)
print(sorted(enum_eng,key=lambda x : x[1]))
print(list(enum_eng))
I want to compare sorted zip list before and after.

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

make a list of int and a list of string into a list of tuple (int,string)

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

Resources