Home > database >  How to populating a matrix using a list of lists in python?
How to populating a matrix using a list of lists in python?

Time:02-05

So I am unsure as to why I am running into an issue for this very simple task. I have a list of lists called temp that is initialized like this:

temp = [['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', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240\n'], ['11', '14', '16', '2', '10', '1', '4', '8', '26', '6', '5', '33', '41', '12', '53', '3', '7', '77', '9', '13', '21', '65', '60', '42', '35', '15', '17', '44', '22', '36', '75', '28', '20', '25', '56', '96', '24', '39', '18', '31', '38', '19', '34', '43', '32', '29', '46', '55', '37', '45', '30', '94', '51', '66', '89', '27', '49', '64', '48', '52', '50', '40', '23', '47', '72', '63', '69', '71', '208', '68', '74', '95', '59', '62', '86', '78', '70', '102', '129', '58', '67', '79', '101', '84', '73', '92', '76', '80', '57', '91', '82', '61', '88', '90', '93', '97', '103', '87', '134', '98', '99', '83', '113', '81', '111', '121', '135', '107', '117', '109', '105', '108', '104', '112', '100', '118', '110', '115', '128', '116', '85', '122', '133', '131', '119', '126', '124', '114', '179', '132', '210', '142', '152', '127', '155', '149', '120', '180', '141', '144', '139', '138', '130', '147', '240', '145', '185', '136', '173', '148', '143', '146', '162', '163', '137', '156', '188', '153', '157', '165', '154', '158', '160', '164', '175', '167', '159', '170', '150', '172', '166', '161', '182', '178', '183', '177', '184', '181', '151', '176', '187', '186', '169', '174', '190', '123', '194', '189', '196', '168', '204', '197', '200', '201', '195', '199', '198', '193', '192', '191', '202', '106', '206', '203', '125', '207', '218', '140', '205', '214', '221', '212', '217', '220', '209', '215', '216', '219', '224', '230', '213', '222', '223', '229', '227', '211', '225', '228', '226', '234', '232', '233', '231', '171', '235', '236', '238', '237', '239', '54\n'], ['114', '8', '94', '26', '33', '17', '14', '7', '64', '173', '4', '10', '11', '60', '41', '9', '53', '19', '42', '30', '52', '43', '49', '40', '21', '89', '37', '27', '29', '13', '34', '24', '95', '25', '39', '70', '63', '79', '75', '56', '77', '28', '69', '50', '73', '32', '101', '82', '72', '57', '96', '83', '88', '58', '61', '129', '109', '71', '120', '78', '76', '80', '87', '68', '98', '99', '110', '102', '115', '127', '118', '117', '104', '122', '138', '97', '133', '153', '137', '160', '126', '204', '128', '184', '234', '176', '131', '213', '85', '145', '177', '163', '152', '148', '189', '201', '183', '164', '168', '158', '150', '171', '162', '143', '172', '151', '161', '159', '211', '175', '141', '165', '200', '125', '219', '149', '155', '222', '202', '216', '209', '192', '182', '223', '212', '190', '224', '106', '215', '221', '205', '218', '236', '229', '225', '180', '198', '233', '231', '228', '232', '227', '235', '238', '237', '140', '239', '210', '217', '16', '23', '157', '170', '194', '185', '12', '119', '156', '92', '179', '6', '107', '44', '35', '20', '174', '103', '154', '147', '51', '67', '135', '191', '47', '2', '48', '214', '136', '197', '226', '100', '112', '134', '116', '208', '55', '187', '144', '3', '142', '1', '84', '15', '203', '123', '65', '186', '130', '46', '59', '36', '62', '18', '22', '81', '193', '169', '45', '206', '31', '105', '91', '132', '90', '178', '230', '146', '199', '66', '86', '121', '54', '74', '5', '108', '195', '188', '207', '167', '113', '93', '38', '111', '139', '220', '181', '196', '124', '166', '240\n'], ['1', '2', '33', '6', '4', '5', '7', '14', '12', '16', '8', '64', '11', '9', '17', '18', '20', '28', '13', '24', '19', '31', '21', '41', '40', '30', '22', '94', '35', '47', '27', '60', '25', '45', '34', '37', '29', '43', '69', '15', '3', '89', '65', '56', '53', '49', '38', '48', '42', '26', '59', '39', '72', '52', '129', '63', '10', '66', '67', '50', '23', '62', '79', '93', '36', '55', '82', '73', '78', '46', '32', '84', '57', '71', '54', '81', '98', '70', '58', '102', '88', '77', '90', '86', '104', '108', '95', '68', '96', '51', '101', '100', '75', '103', '80', '122', '99', '112', '92', '83', '135', '109', '105', '110', '115', '76', '61', '74', '117', '118', '179', '44', '128', '121', '120', '87', '126', '127', '114', '107', '133', '124', '134', '116', '111', '139', '113', '132', '130', '143', '138', '154', '91', '145', '240', '160', '148', '136', '119', '137', '153', '142', '173', '152', '97', '144', '159', '164', '162', '85', '131', '146', '157', '167', '174', '210', '156', '178', '188', '176', '158', '166', '170', '175', '169', '151', '204', '165', '147', '177', '172', '187', '183', '186', '181', '163', '189', '196', '149', '184', '191', '123', '180', '190', '168', '199', '155', '171', '106', '195', '201', '200', '192', '197', '150', '193', '209', '211', '203', '182', '205', '194', '202', '161', '206', '213', '207', '212', '214', '219', '208', '218', '217', '125', '216', '220', '215', '224', '221', '222', '230', '140', '185', '223', '231', '198', '225', '227', '234', '229', '228', '226', '233', '236', '235', '232', '141', '237', '238', '239\n']]

I also have an empty matrix that has rows that are equal to the length of temp[0] and columns that are equal to the length of temp which in this case would be a 240x4 sized matrix.

Here is my current code to read over each list in temp, find the row that I want to populate which is equal to the value of the list[i]-1 and set this row to the index which is i. I am only doing this for the first column as of now.

for test in temp:
   for i in range(len(test)):
      #print("Entry: ("   str(test[i])   ",0) populated with index: "   str(i 1))
      row = int(test[i])-1
      val = i 1
      matrix[row][0] = val

I would expect the first column of matrix to just be 1, 2,...,240. However after printing out matrix I get the following:

matrix = [[1, 0, 0, 0], [2, 0, 0, 0], [41, 0, 0, 0], [5, 0, 0, 0], [6, 0, 0, 0], [4, 0, 0, 0], [7, 0, 0, 0], [11, 0, 0, 0], [14, 0, 0, 0], [57, 0, 0, 0], [13, 0, 0, 0], [9, 0, 0, 0], [19, 0, 0, 0], [8, 0, 0, 0], [40, 0, 0, 0], [10, 0, 0, 0], [15, 0, 0, 0], [16, 0, 0, 0], [21, 0, 0, 0], [17, 0, 0, 0], [23, 0, 0, 0], [27, 0, 0, 0], [61, 0, 0, 0], [20, 0, 0, 0], [33, 0, 0, 0], [50, 0, 0, 0], [31, 0, 0, 0], [18, 0, 0, 0], [37, 0, 0, 0], [26, 0, 0, 0], [22, 0, 0, 0], [71, 0, 0, 0], [3, 0, 0, 0], [35, 0, 0, 0], [29, 0, 0, 0], [65, 0, 0, 0], [36, 0, 0, 0], [47, 0, 0, 0], [52, 0, 0, 0], [25, 0, 0, 0], [24, 0, 0, 0], [49, 0, 0, 0], [38, 0, 0, 0], [112, 0, 0, 0], [34, 0, 0, 0], [70, 0, 0, 0], [30, 0, 0, 0], [48, 0, 0, 0], [46, 0, 0, 0], [60, 0, 0, 0], [90, 0, 0, 0], [54, 0, 0, 0], [45, 0, 0, 0], [75, 0, 0, 0], [66, 0, 0, 0], [44, 0, 0, 0], [73, 0, 0, 0], [79, 0, 0, 0], [51, 0, 0, 0], [32, 0, 0, 0], [107, 0, 0, 0], [62, 0, 0, 0], [56, 0, 0, 0], [12, 0, 0, 0], [43, 0, 0, 0], [58, 0, 0, 0], [59, 0, 0, 0], [88, 0, 0, 0], [39, 0, 0, 0], [78, 0, 0, 0], [74, 0, 0, 0], [53, 0, 0, 0], [68, 0, 0, 0], [108, 0, 0, 0], [93, 0, 0, 0], [106, 0, 0, 0], [82, 0, 0, 0], [69, 0, 0, 0], [63, 0, 0, 0], [95, 0, 0, 0], [76, 0, 0, 0], [67, 0, 0, 0], [100, 0, 0, 0], [72, 0, 0, 0], [150, 0, 0, 0], [84, 0, 0, 0], [116, 0, 0, 0], [81, 0, 0, 0], [42, 0, 0, 0], [83, 0, 0, 0], [133, 0, 0, 0], [99, 0, 0, 0], [64, 0, 0, 0], [28, 0, 0, 0], [87, 0, 0, 0], [89, 0, 0, 0], [145, 0, 0, 0], [77, 0, 0, 0], [97, 0, 0, 0], [92, 0, 0, 0], [91, 0, 0, 0], [80, 0, 0, 0], [94, 0, 0, 0], [85, 0, 0, 0], [103, 0, 0, 0], [189, 0, 0, 0], [120, 0, 0, 0], [86, 0, 0, 0], [102, 0, 0, 0], [104, 0, 0, 0], [125, 0, 0, 0], [98, 0, 0, 0], [127, 0, 0, 0], [119, 0, 0, 0], [105, 0, 0, 0], [124, 0, 0, 0], [109, 0, 0, 0], [110, 0, 0, 0], [139, 0, 0, 0], [115, 0, 0, 0], [114, 0, 0, 0], [96, 0, 0, 0], [182, 0, 0, 0], [122, 0, 0, 0], [214, 0, 0, 0], [117, 0, 0, 0], [118, 0, 0, 0], [113, 0, 0, 0], [55, 0, 0, 0], [129, 0, 0, 0], [151, 0, 0, 0], [128, 0, 0, 0], [121, 0, 0, 0], [123, 0, 0, 0], [101, 0, 0, 0], [138, 0, 0, 0], [140, 0, 0, 0], [131, 0, 0, 0], [126, 0, 0, 0], [222, 0, 0, 0], [237, 0, 0, 0], [142, 0, 0, 0], [130, 0, 0, 0], [146, 0, 0, 0], [134, 0, 0, 0], [152, 0, 0, 0], [169, 0, 0, 0], [137, 0, 0, 0], [179, 0, 0, 0], [195, 0, 0, 0], [166, 0, 0, 0], [144, 0, 0, 0], [141, 0, 0, 0], [132, 0, 0, 0], [187, 0, 0, 0], [157, 0, 0, 0], [153, 0, 0, 0], [161, 0, 0, 0], [147, 0, 0, 0], [136, 0, 0, 0], [204, 0, 0, 0], [149, 0, 0, 0], [176, 0, 0, 0], [148, 0, 0, 0], [168, 0, 0, 0], [162, 0, 0, 0], [154, 0, 0, 0], [185, 0, 0, 0], [165, 0, 0, 0], [163, 0, 0, 0], [188, 0, 0, 0], [171, 0, 0, 0], [143, 0, 0, 0], [155, 0, 0, 0], [164, 0, 0, 0], [160, 0, 0, 0], [170, 0, 0, 0], [158, 0, 0, 0], [111, 0, 0, 0], [183, 0, 0, 0], [175, 0, 0, 0], [200, 0, 0, 0], [173, 0, 0, 0], [180, 0, 0, 0], [223, 0, 0, 0], [174, 0, 0, 0], [172, 0, 0, 0], [159, 0, 0, 0], [177, 0, 0, 0], [184, 0, 0, 0], [181, 0, 0, 0], [193, 0, 0, 0], [196, 0, 0, 0], [202, 0, 0, 0], [190, 0, 0, 0], [178, 0, 0, 0], [194, 0, 0, 0], [226, 0, 0, 0], [186, 0, 0, 0], [192, 0, 0, 0], [191, 0, 0, 0], [203, 0, 0, 0], [199, 0, 0, 0], [167, 0, 0, 0], [201, 0, 0, 0], [205, 0, 0, 0], [207, 0, 0, 0], [211, 0, 0, 0], [197, 0, 0, 0], [156, 0, 0, 0], [198, 0, 0, 0], [208, 0, 0, 0], [206, 0, 0, 0], [209, 0, 0, 0], [217, 0, 0, 0], [215, 0, 0, 0], [213, 0, 0, 0], [212, 0, 0, 0], [210, 0, 0, 0], [216, 0, 0, 0], [219, 0, 0, 0], [220, 0, 0, 0], [224, 0, 0, 0], [218, 0, 0, 0], [227, 0, 0, 0], [232, 0, 0, 0], [228, 0, 0, 0], [231, 0, 0, 0], [230, 0, 0, 0], [221, 0, 0, 0], [225, 0, 0, 0], [236, 0, 0, 0], [233, 0, 0, 0], [229, 0, 0, 0], [235, 0, 0, 0], [234, 0, 0, 0], [238, 0, 0, 0], [239, 0, 0, 0], [240, 0, 0, 0], [135, 0, 0, 0]]

For some reason column 0 is not following the 1,2,...,240 that I would expect. If the for loop is just going from 0...240 I thought it would be fairly simple to just set the row to that index. I would expect row 3 for example to have a 3 now in the first column, not a 41. Are there any suggestions to fix this?

Here is a smaller test case:

matrix = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
temp = [[1,2,3,4,5], [5,4,3,2,1],[1,4,5,3,2]]
for test in temp:
    for i in range(len(test)):
        #print("Entry: ("   str(test[i])   ",0) populated with index: "   str(i 1))
        row = int(test[i])-1
        val = i 1
        matrix[row][0] = val
print(matrix)

This outputs:

matrix = [[1, 0, 0, 0, 0], [5, 0, 0, 0, 0], [4, 0, 0, 0, 0], [2, 0, 0, 0, 0], [3, 0, 0, 0, 0]]

I would expect the first col. to be 1,2,3,4,5 but it is instead 1,5,4,2,3.

CodePudding user response:

You can avoid all those nested loops with inconvenient index adjustments and conversions using numpy array with short and flexible array indexing:

import numpy as np

matrix = np.array([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]])
temp = np.array([[1,2,3,4,5], [5,4,3,2,1], [1,4,5,3,2]])

matrix[:, 0] = temp[0]
print(matrix)

[[1 0 0 0 0]
 [2 0 0 0 0]
 [3 0 0 0 0]
 [4 0 0 0 0]
 [5 0 0 0 0]]

CodePudding user response:

If I understand correctly, what you want to do is basically transpose the original matrix. If that is the case, you can just use numpy transpose function:

import numpy as np

temp = [[1,2,3,4,5], [5,4,3,2,1],[1,4,5,3,2]]

temp_np = np.array(temp)
result_np = temp_np.T
result = result_np.tolist()
print(result)

Result:

[[1, 5, 1], [2, 4, 4], [3, 3, 5], [4, 2, 3], [5, 1, 2]]
  • Related