[[0, 100, 7, 27, 34, 40, 41, 48, 58, 65, 75, 78, 79, 96, 126, 127, 0],
[0, 2, 45, 54, 56, 57, 59, 66, 67, 82, 86, 102, 124, 133, 0],
[0, 35, 39, 52, 53, 60, 61, 80, 81, 83, 87, 97, 98, 101, 109, 0],
[0, 15, 28, 29, 30, 31, 32, 33, 37, 38, 49, 50, 51, 71, 95, 0],
[0, 3, 16, 22, 23, 44, 72, 73, 74, 90, 110, 131, 0],
[0, 10, 11, 18, 19, 36, 55, 89, 93, 94, 108, 113, 114, 0],
[0, 1, 5, 6, 9, 12, 17, 24, 43, 64, 77, 85, 88, 91, 92, 111, 112, 130, 0],
[0, 13, 20, 42, 62, 68, 84, 99, 104, 116, 119, 125, 128, 129, 132, 0],
[0, 8, 14, 26, 63, 69, 70, 103, 105, 123, 0],
[0, 4, 21, 25, 46, 47, 106, 107, 115, 117, 118, 120, 121, 122, 0],
[0, 76, 0]]
I have the different routes listed above. I need to calculate the distance of every route (11 routes in total)
After this, I have created all edges within a single route.
[[(0, 100),
(100, 7),
(7, 27),
(27, 34),
(34, 40),
(40, 41),
(41, 48),
(48, 58),
(58, 65),
(65, 75),
(75, 78),
(78, 79),
(79, 96),
(96, 126),
(126, 127),
(127, 0)],
[(0, 2),
(2, 45),
(45, 54),
(54, 56),
(56, 57),
(57, 59),
(59, 66),
(66, 67),
(67, 82),
(82, 86),
(86, 102),
(102, 124),
(124, 133),
(133, 0)],
[(0, 35),
(35, 39),
(39, 52),
(52, 53),
(53, 60),
(60, 61),
(61, 80),
(80, 81),
(81, 83),
(83, 87),
(87, 97),
(97, 98),
(98, 101),
(101, 109),
(109, 0)],
[(0, 15),
(15, 28),
(28, 29),
(29, 30),
(30, 31),
(31, 32),
(32, 33),
(33, 37),
(37, 38),
(38, 49),
(49, 50),
(50, 51),
(51, 71),
(71, 95),
(95, 0)],
[(0, 3),
(3, 16),
(16, 22),
(22, 23),
(23, 44),
(44, 72),
(72, 73),
(73, 74),
(74, 90),
(90, 110),
(110, 131),
(131, 0)],
[(0, 10),
(10, 11),
(11, 18),
(18, 19),
(19, 36),
(36, 55),
(55, 89),
(89, 93),
(93, 94),
(94, 108),
(108, 113),
(113, 114),
(114, 0)],
[(0, 1),
(1, 5),
(5, 6),
(6, 9),
(9, 12),
(12, 17),
(17, 24),
(24, 43),
(43, 64),
(64, 77),
(77, 85),
(85, 88),
(88, 91),
(91, 92),
(92, 111),
(111, 112),
(112, 130),
(130, 0)],
[(0, 13),
(13, 20),
(20, 42),
(42, 62),
(62, 68),
(68, 84),
(84, 99),
(99, 104),
(104, 116),
(116, 119),
(119, 125),
(125, 128),
(128, 129),
(129, 132),
(132, 0)],
[(0, 8),
(8, 14),
(14, 26),
(26, 63),
(63, 69),
(69, 70),
(70, 103),
(103, 105),
(105, 123),
(123, 0)],
[(0, 4),
(4, 21),
(21, 25),
(25, 46),
(46, 47),
(47, 106),
(106, 107),
(107, 115),
(115, 117),
(117, 118),
(118, 120),
(120, 121),
(121, 122),
(122, 0)],
[(0, 76), (76, 0)]]
However, I need to calculate the distance between the edges. Every edge consists of 2 numbers which are city numbers in a distance matrix (so 0,100 is the distance from city 0 to city 100). I tried to calculate the distances but cannot keep separate routes.
I already tried this:
a_list=[]
visiting_time={}
for k in range(len(result)):
for (i,j) in visits[k]:
visiting_time[(i,j)]= distance_matrix_new_time[i][j]
f=list(visiting_time.values())
a_list.append(f)
In my code Result is the list with different routes (first list), and visits is the list with all edges (second list)
the output should be like this
[2,3,5,6,3,2,5,8,3,5,2,4,6],[2,6,3,1,9,....],[....] etc.
Can someone help me out?
you could use a list comprehension:
a_list = [[distance_matrix_new_time[i][j] for i, j in l] for l in visits]
This is a part of a large program. I have a list like
cnfn=[(1, -3), (2, -3), (-1, -2, 3), (-1, 4), (-2, 4), (1, 2, -4), (-4, -5), (4, 5), (-3, 6), (-5, 6), (3, 5, -6), (7, -8), (6, -8), (-7, -6, 8), (-6, 9), (-7, 9), (6, 7, -9), (-9, -10), (9, 10), (-8, 11), (-10, 11), (8, 10, -11), (7, -12), (4, -12), (-7, -4, 12), (-12, 13), (-3, 13), (12, 3, -13), (14, -16), (15, -16), (-14, -15, 16), (-16, -17), (16, 17), (-14, 18), (-15, 18), (14, 15, -18), (17, -19), (18, -19), (-17, -18, 19), (13, -20), (19, -20), (-13, -19, 20), (-20, -21), (20, 21), (-19, 22), (-13, 22), (19, 13, -22), (21, -23), (22, -23), (-21, -22, 23), (13, -24), (18, -24), (-13, -18, 24), (-24, 25), (-16, 25), (24, 16, -25), (26, -28), (27, -28), (-26, -27, 28), (-28, -29), (28, 29), (-26, 30), (-27, 30), (26, 27, -30), (29, -31), (30, -31), (-29, -30, 31), (25, -32), (31, -32), (-25, -31, 32), (-32, -33), (32, 33), (-31, 34), (-25, 34), (31, 25, -34), (33, -35), (34, -35), (-33, -34, 35), (25, -36), (30, -36), (-25, -30, 36), (-36, 37), (-28, 37), (36, 28, -37), (38, -40), (39, -40), (-38, -39, 40), (-40, -41), (40, 41), (-38, 42), (-39, 42), (38, 39, -42), (41, -43), (42, -43), (-41, -42, 43), (37, -44), (43, -44), (-37, -43, 44), (-44, -45), (44, 45), (-43, 46), (-37, 46), (43, 37, -46), (45, -47), (46, -47), (-45, -46, 47), (37, -48), (42, -48), (-37, -42, 48), (-48, 49), (-40, 49), (48, 40, -49), (-50, -51), (50, 51), (-51, 53), (-52, 53), (51, 52, -53), (-52, -54), (52, 54), (-54, 55), (-50, 55), (54, 50, -55), (53, -56), (55, -56), (-53, -55, 56), (-56, -57), (56, 57), (58, -59), (57, -59), (-58, -57, 59), (52, -60), (50, -60), (-52, -50, 60), (-59, 61), (-60, 61), (59, 60, -61), (56, -62), (58, -62), (-56, -58, 62), (-58, -63), (58, 63), (57, -64), (63, -64), (-57, -63, 64), (-62, 65), (-64, 65), (62, 64, -65), (-66, -67), (66, 67), (-67, 69), (-68, 69), (67, 68, -69), (-68, -70), (68, 70), (-70, 71), (-66, 71), (70, 66, -71), (69, -72), (71, -72), (-69, -71, 72), (-72, -73), (72, 73), (61, -74), (73, -74), (-61, -73, 74), (68, -75), (66, -75), (-68, -66, 75), (-74, 76), (-75, 76), (74, 75, -76), (72, -77), (61, -77), (-72, -61, 77), (-61, -78), (61, 78), (73, -79), (78, -79), (-73, -78, 79), (-77, 80), (-79, 80), (77, 79, -80), (-81, -82), (81, 82), (-82, 84), (-83, 84), (82, 83, -84), (-83, -85), (83, 85), (-85, 86), (-81, 86), (85, 81, -86), (84, -87), (86, -87), (-84, -86, 87), (-87, -88), (87, 88), (76, -89), (88, -89), (-76, -88, 89), (83, -90), (81, -90), (-83, -81, 90), (-89, 91), (-90, 91), (89, 90, -91), (87, -92), (76, -92), (-87, -76, 92), (-76, -93), (76, 93), (88, -94), (93, -94), (-88, -93, 94), (-92, 95), (-94, 95), (92, 94, -95), (-96, -97), (96, 97), (-97, 99), (-98, 99), (97, 98, -99), (-98, -100), (98, 100), (-100, 101), (-96, 101), (100, 96, -101), (99, -102), (101, -102), (-99, -101, 102), (-102, -103), (102, 103), (91, -104), (103, -104), (-91, -103, 104), (-104, -105), (104, 105), (-104, 106), (-105, 106), (104, 105, -106), (102, -107), (91, -107), (-102, -91, 107), (-91, -108), (91, 108), (103, -109), (108, -109), (-103, -108, 109), (-107, 110), (-109, 110), (107, 109, -110), (-1, 50), (1, -50), (-2, 52), (2, -52), (-7, 58), (7, -58), (-14, 66), (14, -66), (-15, 68), (15, -68), (-26, 81), (26, -81), (-27, 83), (27, -83), (-38, 96), (38, -96), (-39, 98), (39, -98), (-11, -65, -111), (-11, 65, 111), (11, -65, 111), (11, 65, -111), (-23, -80, -112), (-23, 80, 112), (23, -80, 112), (23, 80, -112), (-35, -95, -113), (-35, 95, 113), (35, -95, 113), (35, 95, -113), (-47, -106, -114), (-47, 106, 114), (47, -106, 114), (47, 106, -114), (-49, -110, -115), (-49, 110, 115), (49, -110, 115), (49, 110, -115), (111, 112, 113, 114, 115)]
And there is another list
cnfb=[(1, -3), (2, -3), (-1, -2, 3), (-1, 4), (-2, 4), (1, 2, -4), (4, 5), (-4, -5), (-3, 6), (-5, 6), (3, 5, -6), (7, -8), (6, -8), (-7, -6, 8), (-6, 9), (-7, 9), (6, 7, -9), (9, 10), (-9, -10), (-8, 11), (-10, 11), (8, 10, -11), (7, -12), (4, -12), (-7, -4, 12), (-12, 13), (-3, 13), (12, 3, -13), (14, -16), (15, -16), (-14, -15, 16), (16, 17), (-16, -17), (-14, 18), (-15, 18), (14, 15, -18), (17, -19), (18, -19), (-17, -18, 19), (13, -20), (19, -20), (-13, -19, 20), (20, 21), (-20, -21), (-19, 22), (-13, 22), (19, 13, -22), (21, -23), (22, -23), (-21, -22, 23), (13, -24), (18, -24), (-13, -18, 24), (-24, 25), (-16, 25), (24, 16, -25), (26, -28), (27, -28), (-26, -27, 28), (28, 29), (-28, -29), (-26, 30), (-27, 30), (26, 27, -30), (29, -31), (30, -31), (-29, -30, 31), (25, -32), (31, -32), (-25, -31, 32), (32, 33), (-32, -33), (-31, 34), (-25, 34), (31, 25, -34), (33, -35), (34, -35), (-33, -34, 35), (25, -36), (30, -36), (-25, -30, 36), (-36, 37), (-28, 37), (36, 28, -37), (38, -40), (39, -40), (-38, -39, 40), (40, 41), (-40, -41), (-38, 42), (-39, 42), (38, 39, -42), (41, -43), (42, -43), (-41, -42, 43), (37, -44), (43, -44), (-37, -43, 44), (44, 45), (-44, -45), (-43, 46), (-37, 46), (43, 37, -46), (45, -47), (46, -47), (-45, -46, 47), (37, -48), (42, -48), (-37, -42, 48), (-48, 49), (-40, 49), (48, 40, -49), (50, 51), (-50, -51), (-51, 53), (-52, 53), (51, 52, -53), (52, 54), (-52, -54), (-54, 55), (-50, 55), (54, 50, -55), (53, -56), (55, -56), (-53, -55, 56), (56, 57), (-56, -57), (58, -59), (57, -59), (-58, -57, 59), (52, -60), (50, -60), (-52, -50, 60), (-59, 61), (-60, 61), (59, 60, -61), (56, -62), (58, -62), (-56, -58, 62), (58, 63), (-58, -63), (57, -64), (63, -64), (-57, -63, 64), (-62, 65), (-64, 65), (62, 64, -65), (66, 67), (-66, -67), (-67, 69), (-68, 69), (67, 68, -69), (68, 70), (-68, -70), (-70, 71), (-66, 71), (70, 66, -71), (69, -72), (71, -72), (-69, -71, 72), (72, 73), (-72, -73), (61, -74), (73, -74), (-61, -73, 74), (68, -75), (66, -75), (-68, -66, 75), (-74, 76), (-75, 76), (74, 75, -76), (72, -77), (61, -77), (-72, -61, 77), (61, 78), (-61, -78), (73, -79), (78, -79), (-73, -78, 79), (-77, 80), (-79, 80), (77, 79, -80), (81, 82), (-81, -82), (-82, 84), (-83, 84), (82, 83, -84), (83, 85), (-83, -85), (-85, 86), (-81, 86), (85, 81, -86), (84, -87), (86, -87), (-84, -86, 87), (87, 88), (-87, -88), (76, -89), (88, -89), (-76, -88, 89), (83, -90), (81, -90), (-83, -81, 90), (-89, 91), (-90, 91), (89, 90, -91), (87, -92), (76, -92), (-87, -76, 92), (76, 93), (-76, -93), (88, -94), (93, -94), (-88, -93, 94), (-92, 95), (-94, 95), (92, 94, -95), (96, 97), (-96, -97), (-97, 99), (-98, 99), (97, 98, -99), (98, 100), (-98, -100), (-100, 101), (-96, 101), (100, 96, -101), (99, -102), (101, -102), (-99, -101, 102), (102, 103), (-102, -103), (91, -104), (103, -104), (-91, -103, 104), (104, 105), (-104, -105), (-104, 106), (-105, 106), (104, 105, -106), (102, -107), (91, -107), (-102, -91, 107), (91, 108), (-91, -108), (103, -109), (108, -109), (-103, -108, 109), (-107, 110), (-109, 110), (107, 109, -110), (35, 95, -111), (-35, -95, -111), (-35, 95, 111), (35, -95, 111), (23, 80, -112), (-23, -80, -112), (-23, 80, 112), (23, -80, 112), (49, 106, -113), (-49, -106, -113), (-49, 106, 113), (49, -106, 113), (47, 110, -114), (-47, -110, -114), (-47, 110, 114), (47, -110, 114), (11, 65, -115), (-11, -65, -115), (-11, 65, 115), (11, -65, 115), [111, 112, 113, 114, 115], (-26, 83), (26, -83), (-2, 50), (2, -50), (-38, 98), (38, -98), (-27, 81), (27, -81), (-39, 96), (39, -96), (-7, 58), (7, -58), (-14, 68), (14, -68), (-15, 66), (15, -66), (-1, 52), (1, -52)]
If I check with plane eye the look like having same values but if I put them in the same function the result is different. How can I determine those two have exactly same type and same value?
The two lists are NOT the same. That is why a function may be giving you a different result for the different lists.
To check if 2 lists are identical, you can do:
list1 == list2
So to give some examples:
>>> [1, 2, 3, 4, 5] == [1, 2, 3, 4, 5]
True
>>> [1, 2, 3, 4, 5] == [1, 2, 3, 4, 3]
False
>>> [1, 2, 3, 4, 5] == [5, 4, 3, 2, 1]
False
>>> [(1, 2), (3, 4)] == [(1, 2), (3, 4)]
True
>>> [(1, 2), (3, 4)] == [(1, 2), (3, 5)]
False
If you want to find what the differences are, you can do the following:
[e for e in list1 if e not in list2] + [e for e in list2 if e not in list1]
which I think is actually very readable for what it is.
So we could put that inside a function:
def comp(list1, list2):
return [e for e in list1 if e not in list2] + [e for e in list2 if e not in list1]
and some examples:
>>> comp([1, 2, 3], [1, 2, 3]) #should be empty as no differnence
[]
>>> comp([(1, 2), (3, 4)], [(1, 2), (3, 5)])
[(3, 4), (3, 5)]
>>> comp([(1, 2), (3, 4)], [(1, 2), (3, 5), (6, 7)])
[(3, 4), (3, 5), (6, 7)]
I realize this may be more of a math problem than an actual programming problem. I'm trying to figure this out with python.
So the user is going to specify a range of numbers to me, the min range being 1-2 and max being 1-99. I then have to tell the user how many 3 number combinations can be made in that range. However, the combinations can ONLY be in increasing numeric order. So for example, if the given range is 1-50, I can't say 45 - 10 - 20 is a combination, because it is not in increasing numeric order.
Try the itertools module.
import itertools
numbers = range(1,100)
items = set(list(itertools.combinations(numbers,3)))
for item in items:
print item
It seems to give the desired output.
Be aware: It seems to take a long time (Edit: to print it all at once.).
This is part of the output:
, 73, 75), (76, 86, 91), (42, 91, 92), (8, 54, 71), (11, 54, 87), (37, 79, 86), (2, 17, 32), (44, 67, 78), (14, 24, 56), (10, 64, 79), (9, 90, 94), (39, 52, 88), (62, 78, 90), (9, 60, 71), (23, 25, 30), (5, 27, 92), (33, 74, 78), (68, 70, 84), (48, 79, 95), (8, 70, 95), (23, 68, 78), (14, 45, 78), (8, 36, 73), (72, 86, 88), (13, 26, 74), (35, 60, 86), (3, 29, 76), (6, 15, 74), (46, 54, 73), (7, 41, 88), (48, 59, 90), (23, 30, 73), (71, 83, 91), (42, 78, 96), (44, 60, 92), (27, 46, 68), (27, 72, 88), (34, 74, 78), (24, 55, 93), (84, 93, 97), (32, 36, 73), (7, 31, 38), (28, 43, 66), (29, 37, 40), (19, 33, 96), (45, 66, 77), (25, 66, 72), (22, 60, 74), (59, 60, 76), (30, 57, 82), (11, 16, 51), (41, 48, 99), (5, 21, 86), (18, 27, 98), (26, 34, 95), (19, 72, 74), (32, 34, 35), (43, 68, 93), (36, 57, 77), (20, 50, 90), (25, 71, 99), (47, 74, 87), (9, 26, 35), (20, 24, 89), (27, 67, 83), (3, 19, 70), (20, 72, 79), (24, 36, 79), (8, 25, 43), (49, 53, 87), (24, 63, 68), (21, 63, 92), (21, 56, 72), (26, 43, 87), (79, 92, 94), (22, 41, 98), (45, 55, 88), (30, 46, 94), (38, 71, 79), (17, 51, 81), (43, 65, 97), (40, 56, 72), (19, 62, 88), (31, 38, 98), (15, 25, 79), (24, 45, 71), (52, 87, 98), (20, 39, 82), (23, 33, 44), (43, 68, 88), (6, 8, 29), (36, 73, 95), (48, 78, 84), (22, 38, 84), (21, 65, 97), (30, 31, 57), (27, 28, 38), (2, 33, 46), (24, 29, 51), (4, 6, 45), (64, 71, 93), (14, 36, 68), (36, 51, 62), (20, 40, 68), (19, 71, 81), (33, 60, 81), (13, 25, 60), (17, 39, 68), (68, 69, 81), (18, 19, 89), (2, 28, 61), (4, 67, 71), (12, 26, 52), (34, 41, 46), (22, 27, 59), (28, 56, 96), (1, 25, 53), (39, 61, 90), (11, 31, 44), (17, 40, 82), (16, 21, 73), (19, 78, 93), (10, 16, 36), (21, 30, 32), (15, 23, 69), (9, 21, 28), (20, 29, 40), (11, 48, 61), (36, 71, 81), (19, 24, 48), (7, 49, 61), (15, 74, 99), (13, 45, 85)])