Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Data Science by (18.4k points)

I have the below chunk of code:

list1 = ['H', (55, 42, 16), 'C', (54, 9, 67), 'g', (131, 175, 8), 'F', (12, 55, 94), 'n', (160, 157, 163), '8', (1, 137, 228), 'a', (187, 144, 14), 'y', (39, 174, 157), '=', (181, 137, 84), 'J', (102, 8, 192), 'e', (21, 164, 136), '0', (198, 56, 213), 'k', (116, 239, 75), 't', (11, 252, 72), 'z', (201, 63, 84), 'P', (190, 216, 172), 'd', (209, 110, 106), '-', (208, 176, 194), '7', (0, 135, 142), 'N', (132, 180, 88), 'E', (207, 104, 175), 'i', (72, 246, 151), '9', (146, 71, 239), '+', (157, 214, 119), 'Z', (130, 98, 112), 'T', (83, 227, 187), 'o', (57, 69, 90), '/', (3, 214, 108), '1', (32, 170, 75), '2', (37, 72, 190), 'W', (233, 70, 197), 'Q', (224, 251, 81), 'B', (237, 65, 50), 'r', (239, 170, 236), 'G', (176, 20, 228), 'O', (232, 54, 75), 'R', (203, 114, 163), 'U', (81, 144, 138), '_', (144, 188, 124), 'u', (141, 22, 62), 'A', (178, 113, 24), 'q', (166, 134, 7), 'D', (9, 241, 252), 'f', (88, 70, 177), 'V', (196, 52, 161), 'm', (8, 182, 112), 'K', (117, 29, 10), 'c', (140, 191, 15), 'v', (178, 31, 21), 'I', (44, 239, 254), 'p', (39, 252, 102), '4', (186, 134, 228), 'X', (105, 227, 221), '6', (14, 177, 185), 'Y', (151, 85, 90), 'h', (48, 26, 73), '5', (126, 137, 8), 'M', (165, 212, 26), 'w', (147, 193, 65), '3', (19, 0, 114), 's', (226, 61, 190), 'S', (14, 186, 235), 'x', (99, 26, 6), 'j', (82, 88, 133), 'b', (27, 225, 246), 'l', (116, 173, 65), 'L', (211, 8, 246)]

list2 = [(178, 31, 21), (88, 70, 177), (198, 56, 213), (151, 85, 90), (82, 88, 133), (178, 31, 21), (130, 98, 112), (166, 134, 7), (211, 8, 246), (81, 144, 138), (12, 55, 94), (131, 175, 8), (237, 65, 50), (14, 177, 185), (146, 71, 239), (44, 239, 254), (186, 134, 228), (141, 22, 62), (0, 135, 142), (21, 164, 136), (140, 191, 15), (83, 227, 187), (12, 55, 94), (208, 176, 194), (72, 246, 151), (201, 63, 84), (11, 252, 72), (116, 239, 75), (132, 180, 88), (190, 216, 172), (178, 113, 24), (176, 20, 228), (140, 191, 15), (8, 182, 112), (208, 176, 194), (140, 191, 15), (99, 26, 6), (11, 252, 72), (198, 56, 213), (48, 26, 73), (147, 193, 65), (166, 134, 7), (226, 61, 190), (99, 26, 6), (39, 174, 157), (190, 216, 172), (160, 157, 163), (208, 176, 194), (209, 110, 106), (207, 104, 175), (144, 188, 124), (178, 113, 24), (239, 170, 236), (178, 113, 24), (165, 212, 26), (32, 170, 75), (39, 252, 102), (21, 164, 136), (54, 9, 67), (178, 113, 24), (39, 174, 157), (1, 137, 228), (146, 71, 239), (12, 55, 94), (140, 191, 15), (224, 251, 81), (37, 72, 190), (44, 239, 254), (21, 164, 136), (131, 175, 8), (21, 164, 136), (54, 9, 67), (88, 70, 177), (239, 170, 236), (165, 212, 26), (39, 252, 102), (147, 193, 65), (146, 71, 239), (39, 174, 157), (237, 65, 50), (54, 9, 67), (14, 186, 235), (0, 135, 142), (105, 227, 221), (8, 182, 112), (151, 85, 90), (187, 144, 14), (57, 69, 90), (224, 251, 81), (196, 52, 161), (201, 63, 84), (11, 252, 72), (165, 212, 26), (1, 137, 228), (130, 98, 112), (37, 72, 190), (131, 175, 8), (54, 9, 67), (14, 186, 235), (44, 239, 254), (105, 227, 221), (203, 114, 163), (130, 98, 112), (224, 251, 81), (132, 180, 88), (140, 191, 15), (39, 252, 102), (208, 176, 194), (105, 227, 221), (130, 98, 112), (146, 71, 239), (147, 193, 65), (187, 144, 14), (99, 26, 6), (196, 52, 161), (132, 180, 88), (160, 157, 163), (132, 180, 88), (209, 110, 106), (55, 42, 16), (132, 180, 88), (39, 174, 157), (32, 170, 75), (116, 239, 75), (19, 0, 114), (165, 212, 26), (211, 8, 246), (9, 241, 252), (9, 241, 252), (147, 193, 65), (1, 137, 228), (99, 26, 6), (131, 175, 8), (1, 137, 228), (237, 65, 50), (39, 174, 157), (8, 182, 112), (165, 212, 26), (224, 251, 81), (198, 56, 213), (72, 246, 151), (151, 85, 90), (37, 72, 190), (9, 241, 252), (207, 104, 175), (32, 170, 75), (237, 65, 50), (37, 72, 190), (126, 137, 8), (187, 144, 14), (160, 157, 163), (178, 113, 24), (44, 239, 254), (14, 177, 185), (151, 85, 90), (165, 212, 26), (132, 180, 88), (198, 56, 213), (211, 8, 246), (239, 170, 236), (82, 88, 133), (132, 180, 88), (144, 188, 124), (232, 54, 75), (186, 134, 228), (211, 8, 246), (9, 241, 252), (178, 31, 21), (116, 239, 75), (140, 191, 15), (208, 176, 194), (131, 175, 8), (226, 61, 190), (27, 225, 246), (27, 225, 246), (39, 174, 157), (160, 157, 163), (54, 9, 67), (208, 176, 194), (102, 8, 192), (186, 134, 228), (55, 42, 16), (190, 216, 172), (187, 144, 14), (8, 182, 112), (48, 26, 73), (8, 182, 112), (12, 55, 94), (81, 144, 138), (140, 191, 15), (105, 227, 221), (140, 191, 15), (92, 241, 170), (27, 193, 175), (118, 247, 21), (221, 164, 208)]

data = ""

for i in list2:

  if i in list1:

    data += list1[list1.index(i)-1]

And it works correctly, but if I use the list2 of 500 Mb, this code becomes terribly slow, there is an alternative to do this calculation in the swift way, not spending hours for the list of only 500 Mb?

1 Answer

0 votes
by (36.8k points)

Use the dictionary:

list1_elements = dict(zip(list1[1::2], list1[::2]))

data = ''.join(list1_elements[i] for i in list2 if i in list1)

Want to gain skills in Data Science with Python? Sign up today for this Data Science with Python and be a master in it 

Browse Categories

...