Loss is nan, stopping training - pytorch
I have a program as in the attached file.
The error is
Loss is nan, stopping training
{'loss_classifier': tensor(nan, device='cuda:0', grad_fn=<NllLossBackward>), 'loss_box_reg': tensor(nan, device='cuda:0', grad_fn=<DivBackward0>), 'loss_mask': tensor(-2.1386e+22, device='cuda:0',
grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_objectness': tensor(0.7630, device='cuda:0', grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_rpn_box_reg': tensor(0.0572, device='cuda:0', grad_fn=<DivBackward0>)}
An exception has occurred, use %tb to see the full traceback.
I have suspect to data and printed all
labels, masks, boxes = self.readxml(annotation_path, img.size)
print(labels)
print(masks.shape)
print(boxes)
I don't see any error in data.
What could be wrong?
[3, 3, 3, 3, 3, 3, 3, 1]
(8, 576, 720)
[[101, 351, 124, 394], [96, 430, 126, 482], [110, 509, 141, 571], [472, 332, 515, 375], [541, 272, 585, 309], [487, 269, 529, 316], [379, 149, 418, 176], [441, 141, 508, 189]]
[3, 2, 3, 3, 3, 3, 1, 1]
(8, 576, 720)
[[164, 402, 221, 465], [260, 390, 322, 487], [328, 325, 372, 372], [312, 287, 374, 322], [318, 218, 339, 236], [180, 257, 225, 289], [400, 246, 534, 326], [487, 189, 529, 229]]
[1, 3, 3, 1, 3, 3, 3]
(7, 576, 720)
[[127, 353, 233, 570], [308, 239, 333, 272], [270, 223, 294, 253], [232, 200, 264, 247], [323, 214, 344, 234], [366, 278, 439, 311], [583, 275, 656, 326]]
[3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 4]
(15, 576, 720)
[[240, 173, 289, 220], [83, 190, 127, 233], [158, 243, 213, 305], [79, 300, 109, 359], [58, 403, 101, 478], [297, 348, 354, 403], [301, 222, 347, 270], [402, 297, 448, 361], [455, 324, 506, 390], [482, 445, 546, 500], [486, 480, 517, 508], [529, 419, 600, 483], [561, 46, 604, 82], [559, 138, 603, 189], [187, 308, 210, 345]]
[3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 1, 3, 3, 3, 3, 1, 3]
(18, 576, 720)
[[273, 158, 321, 202], [232, 101, 278, 143], [278, 84, 323, 132], [179, 67, 221, 101], [131, 23, 174, 71], [495, 456, 566, 524], [486, 507, 547, 570], [302, 308, 358, 369], [268, 400, 290, 431], [325, 360, 383, 417], [206, 475, 268, 540], [465, 196, 556, 318], [451, 168, 493, 211], [520, 103, 558, 139], [586, 34, 622, 69], [568, 134, 609, 184], [650, 135, 688, 224], [298, 142, 346, 184]]
/home/centos/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py:3103: UserWarning: The default behavior for interpolate/upsample with float scale_factor changed in 1.6.0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details.
warnings.warn("The default behavior for interpolate/upsample with float scale_factor changed "
[3, 3, 3, 4, 3, 3, 3, 2, 3, 2]
(10, 576, 720)
[[295, 462, 346, 514], [347, 402, 401, 461], [386, 414, 433, 459], [483, 443, 507, 480], [441, 370, 485, 414], [408, 359, 453, 402], [452, 301, 500, 353], [493, 306, 537, 361], [491, 465, 541, 532], [486, 145, 573, 208]]
[3, 4, 3, 3, 3, 4, 4]
(7, 576, 720)
[[193, 317, 242, 379], [262, 325, 290, 380], [319, 322, 370, 382], [518, 273, 597, 310], [313, 274, 384, 308], [477, 256, 489, 291], [458, 256, 474, 291]]
[3, 3, 3, 3, 2, 3, 3, 3, 4, 3, 3, 3, 2, 3, 3]
(15, 576, 720)
[[136, 109, 182, 151], [184, 129, 228, 178], [85, 65, 129, 115], [541, 122, 575, 172], [230, 157, 305, 233], [300, 214, 349, 270], [344, 265, 385, 328], [89, 200, 137, 240], [109, 244, 131, 274], [84, 309, 114, 371], [187, 322, 248, 380], [58, 401, 103, 474], [557, 421, 636, 507], [554, 149, 598, 206], [633, 60, 676, 107]]
[3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 2, 3, 3, 4, 4, 4, 3, 3, 3, 3]
(27, 576, 720)
[[598, 102, 642, 150], [570, 142, 608, 192], [131, 481, 199, 558], [84, 38, 121, 73], [125, 69, 167, 105], [191, 94, 259, 161], [152, 117, 199, 163], [211, 158, 262, 205], [299, 162, 347, 216], [295, 209, 346, 260], [333, 156, 379, 202], [378, 146, 430, 196], [399, 236, 450, 288], [442, 229, 495, 280], [352, 335, 413, 396], [287, 379, 316, 410], [363, 476, 429, 544], [483, 483, 565, 573], [514, 456, 591, 535], [519, 407, 591, 476], [490, 287, 522, 324], [585, 278, 618, 314], [642, 335, 675, 380], [434, 89, 478, 119], [526, 75, 574, 107], [340, 61, 391, 108], [129, 40, 172, 76]]
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4]
(12, 576, 720)
[[95, 328, 149, 381], [15, 313, 92, 358], [211, 204, 266, 293], [138, 269, 203, 303], [363, 297, 420, 340], [303, 224, 328, 250], [273, 223, 298, 246], [530, 353, 597, 435], [514, 286, 555, 328], [461, 281, 496, 325], [491, 233, 517, 258], [428, 260, 443, 287]]
[3, 3, 3, 1, 2, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3]
(16, 576, 720)
[[124, 100, 168, 142], [293, 208, 345, 262], [176, 124, 224, 173], [143, 247, 210, 333], [79, 311, 115, 385], [301, 305, 366, 370], [335, 256, 381, 315], [317, 430, 374, 483], [467, 347, 518, 408], [503, 455, 578, 519], [437, 152, 495, 269], [582, 450, 646, 524], [23, 482, 86, 571], [556, 150, 600, 203], [592, 109, 633, 157], [632, 60, 676, 106]]
Epoch: [0] [ 0/303] eta: 0:09:51 lr: 0.000000 loss: -262.4095 (-262.4095) loss_classifier: 1.5747 (1.5747) loss_box_reg: 0.6368 (0.6368) loss_mask: -264.8073 (-264.8073) loss_objectness: 0.1623 (0.1623) loss_rpn_box_reg: 0.0240 (0.0240) time: 1.9534 data: 0.3372 max mem: 4226
[3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(13, 576, 720)
[[191, 126, 229, 149], [230, 130, 272, 155], [282, 127, 342, 168], [344, 161, 386, 186], [397, 164, 442, 198], [453, 179, 493, 207], [92, 205, 125, 257], [103, 348, 129, 392], [259, 488, 321, 547], [426, 380, 472, 425], [570, 382, 621, 433], [530, 250, 567, 285], [568, 265, 614, 302]]
[3, 3, 3, 3, 3, 3, 3, 3, 2, 4, 3, 4, 3, 4, 2, 3, 1, 3, 3, 3, 4]
(21, 576, 720)
[[325, 69, 371, 102], [133, 42, 171, 73], [100, 51, 136, 84], [138, 74, 181, 114], [195, 110, 237, 146], [265, 113, 310, 158], [193, 144, 238, 186], [246, 144, 290, 184], [329, 142, 382, 196], [300, 214, 320, 243], [331, 200, 385, 245], [385, 212, 405, 235], [162, 456, 230, 529], [372, 391, 401, 421], [425, 382, 495, 453], [503, 397, 565, 456], [555, 80, 646, 203], [520, 69, 563, 108], [637, 49, 682, 95], [549, 486, 626, 555], [374, 445, 398, 472]]
[3, 3, 1, 3, 3, 4, 3, 3, 3, 3, 3]
(11, 576, 720)
[[101, 387, 158, 451], [459, 303, 499, 351], [503, 242, 569, 333], [450, 245, 476, 275], [479, 220, 503, 246], [429, 257, 442, 291], [326, 234, 349, 260], [295, 227, 321, 250], [234, 277, 271, 315], [338, 211, 358, 231], [181, 256, 236, 291]]
[3, 3, 2, 3, 3, 2, 2, 4, 1, 3, 3, 3, 3, 3, 4]
(15, 576, 720)
[[109, 105, 145, 133], [174, 122, 211, 148], [237, 125, 275, 157], [286, 134, 328, 166], [351, 150, 393, 177], [412, 139, 455, 184], [447, 173, 495, 205], [495, 156, 514, 178], [570, 190, 641, 253], [534, 277, 580, 317], [483, 310, 531, 363], [474, 444, 532, 504], [329, 461, 385, 515], [109, 416, 138, 472], [83, 196, 99, 222]]
[3, 3, 3, 3, 1, 3, 3, 3]
(8, 576, 720)
[[100, 313, 127, 367], [98, 452, 124, 501], [264, 479, 321, 538], [397, 405, 446, 451], [484, 356, 634, 487], [499, 297, 549, 348], [569, 255, 618, 292], [490, 159, 538, 185]]
[3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3]
(11, 576, 720)
[[165, 367, 221, 423], [269, 340, 313, 395], [236, 289, 306, 332], [148, 290, 220, 327], [307, 275, 358, 314], [362, 281, 408, 315], [12, 306, 93, 349], [447, 250, 475, 295], [495, 251, 522, 292], [205, 261, 255, 290], [330, 208, 353, 226]]
[3, 3, 3, 3, 3, 3, 3, 1, 3, 4]
(10, 576, 720)
[[248, 336, 297, 388], [321, 319, 363, 361], [211, 312, 253, 357], [302, 250, 332, 288], [352, 468, 426, 550], [429, 329, 474, 381], [457, 277, 493, 326], [443, 220, 472, 275], [498, 235, 522, 259], [335, 237, 347, 259]]
[3, 3, 3, 3, 3, 3]
(6, 576, 720)
[[165, 427, 240, 505], [373, 449, 457, 528], [336, 371, 395, 438], [250, 363, 312, 431], [219, 271, 296, 319], [250, 237, 279, 267]]
[3, 3, 2, 1, 3, 3, 4, 4, 3, 3, 3, 4, 1, 3]
(14, 576, 720)
[[151, 118, 184, 142], [215, 126, 256, 154], [181, 108, 219, 135], [294, 122, 373, 173], [94, 270, 121, 316], [105, 370, 131, 416], [350, 457, 381, 489], [395, 410, 426, 444], [445, 356, 495, 403], [558, 263, 603, 299], [450, 179, 495, 206], [501, 180, 523, 203], [375, 127, 460, 186], [642, 235, 684, 265]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 1]
(14, 576, 720)
[[159, 388, 226, 460], [184, 342, 231, 394], [126, 290, 207, 329], [212, 285, 284, 333], [298, 277, 349, 313], [375, 274, 420, 309], [362, 250, 390, 280], [236, 237, 265, 270], [280, 229, 305, 257], [314, 204, 341, 242], [243, 196, 285, 236], [453, 236, 483, 290], [489, 250, 518, 286], [504, 191, 529, 233]]
[3, 3, 3, 3, 4, 4, 2, 3, 3, 3]
(10, 576, 720)
[[103, 363, 157, 422], [338, 341, 384, 391], [210, 259, 243, 296], [260, 248, 288, 282], [395, 288, 423, 336], [458, 269, 473, 305], [495, 342, 615, 499], [292, 230, 318, 262], [331, 214, 351, 232], [448, 239, 475, 270]]
[3, 3, 1, 3, 3, 3, 3, 4, 3, 1, 3]
(11, 576, 720)
[[184, 336, 232, 387], [237, 241, 268, 275], [242, 182, 276, 239], [333, 217, 359, 237], [360, 261, 386, 292], [405, 252, 435, 291], [445, 252, 474, 287], [527, 290, 552, 333], [468, 211, 490, 238], [503, 178, 532, 231], [483, 251, 521, 286]]
[3, 2, 4, 3, 3, 3, 3, 3, 4, 3, 3, 1, 3, 1]
(14, 576, 720)
[[269, 330, 314, 384], [201, 292, 246, 375], [354, 323, 375, 372], [272, 276, 311, 325], [325, 231, 351, 261], [288, 216, 308, 240], [259, 235, 286, 275], [227, 231, 255, 252], [315, 455, 354, 552], [588, 358, 647, 418], [510, 358, 570, 420], [415, 263, 480, 375], [510, 282, 548, 321], [500, 191, 533, 233]]
[3, 3, 4, 4, 1, 3, 3, 2, 3, 3, 3]
(11, 576, 720)
[[335, 311, 375, 372], [221, 254, 276, 288], [294, 236, 308, 261], [321, 241, 337, 273], [288, 191, 333, 230], [346, 208, 369, 226], [382, 287, 432, 333], [453, 326, 502, 394], [646, 269, 719, 312], [436, 258, 469, 291], [315, 277, 366, 316]]
[1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(14, 576, 720)
[[280, 169, 401, 300], [607, 91, 647, 135], [565, 139, 606, 193], [173, 129, 219, 174], [522, 120, 555, 161], [452, 312, 500, 376], [330, 382, 392, 440], [178, 220, 230, 266], [94, 230, 148, 287], [66, 266, 99, 324], [70, 375, 108, 442], [469, 431, 542, 502], [601, 472, 687, 544], [639, 53, 679, 96]]
[3, 3, 1, 3, 3, 3, 3, 3, 4, 3]
(10, 576, 720)
[[432, 168, 476, 197], [479, 183, 523, 221], [262, 106, 352, 166], [90, 250, 114, 287], [87, 408, 116, 465], [329, 457, 380, 508], [492, 445, 541, 499], [440, 356, 481, 399], [569, 283, 590, 309], [526, 254, 568, 291]]
[2, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3]
(11, 576, 720)
[[144, 421, 221, 556], [259, 480, 339, 572], [337, 352, 389, 415], [268, 267, 316, 302], [130, 268, 182, 301], [194, 253, 214, 288], [312, 222, 338, 242], [436, 266, 456, 300], [493, 257, 523, 289], [372, 259, 395, 298], [406, 251, 434, 299]]
[3, 3, 3, 3, 4, 3, 4, 4, 3, 3, 3, 3, 3, 3]
(14, 576, 720)
[[259, 340, 302, 400], [106, 355, 158, 412], [88, 289, 172, 330], [175, 284, 246, 332], [36, 284, 76, 335], [279, 238, 311, 266], [352, 288, 393, 326], [396, 275, 431, 315], [360, 261, 391, 294], [494, 274, 572, 313], [620, 262, 694, 306], [397, 218, 420, 243], [297, 206, 319, 226], [580, 398, 661, 482]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 4, 3, 3]
(17, 576, 720)
[[273, 158, 321, 202], [337, 134, 382, 171], [278, 84, 323, 132], [179, 67, 221, 101], [131, 23, 174, 71], [495, 456, 566, 524], [486, 507, 547, 570], [239, 446, 295, 509], [316, 462, 380, 546], [325, 361, 383, 417], [381, 154, 489, 290], [446, 231, 491, 279], [598, 17, 632, 53], [653, 139, 676, 193], [487, 203, 503, 239], [298, 142, 346, 184], [677, 240, 720, 300]]
[3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 1]
(13, 576, 720)
[[181, 356, 227, 413], [260, 337, 303, 386], [202, 295, 281, 334], [381, 297, 437, 334], [478, 281, 539, 314], [475, 232, 499, 258], [447, 254, 474, 289], [395, 258, 422, 292], [502, 181, 531, 233], [461, 198, 475, 220], [387, 480, 469, 574], [226, 207, 258, 257], [300, 192, 351, 228]]
[3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 2, 3, 4]
(24, 576, 720)
[[293, 436, 347, 486], [328, 453, 375, 500], [342, 397, 391, 445], [370, 418, 422, 464], [399, 350, 450, 393], [421, 372, 470, 417], [450, 312, 489, 354], [470, 335, 512, 377], [100, 71, 141, 102], [127, 107, 162, 133], [205, 127, 248, 154], [225, 107, 262, 132], [73, 159, 96, 190], [89, 200, 112, 234], [87, 239, 109, 271], [85, 273, 110, 313], [85, 321, 113, 364], [90, 366, 104, 395], [638, 294, 666, 340], [585, 262, 613, 305], [586, 219, 629, 256], [497, 198, 546, 238], [391, 169, 427, 198], [524, 309, 550, 339]]
[1, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3]
(13, 576, 720)
[[274, 166, 395, 297], [83, 318, 112, 375], [539, 417, 612, 485], [469, 348, 530, 419], [609, 91, 647, 135], [565, 139, 606, 193], [636, 487, 718, 560], [139, 106, 185, 151], [55, 420, 107, 495], [458, 472, 539, 559], [281, 391, 346, 462], [235, 285, 352, 398], [639, 53, 679, 96]]
[3, 3, 2, 3, 1, 1, 3, 3, 3, 3, 1]
(11, 576, 720)
[[265, 415, 327, 487], [329, 327, 375, 392], [267, 268, 350, 338], [178, 257, 231, 288], [222, 180, 258, 254], [349, 183, 383, 221], [404, 312, 459, 363], [533, 303, 577, 350], [435, 266, 508, 311], [623, 272, 691, 320], [396, 224, 436, 288]]
[3, 3, 3, 3, 1, 1, 3]
(7, 576, 720)
[[246, 357, 301, 431], [106, 338, 153, 388], [212, 282, 278, 325], [228, 250, 268, 280], [271, 209, 319, 262], [440, 203, 482, 257], [445, 259, 471, 295]]
[1, 3, 2, 3, 3, 3, 3, 2, 3, 3, 3, 2, 3]
(13, 576, 720)
[[152, 65, 236, 114], [270, 120, 311, 145], [336, 127, 380, 174], [435, 184, 475, 210], [504, 266, 544, 302], [554, 270, 592, 309], [484, 320, 529, 361], [428, 357, 477, 414], [378, 366, 427, 410], [366, 421, 416, 467], [264, 466, 314, 513], [86, 330, 116, 391], [601, 225, 643, 254]]
[4, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3]
(31, 576, 720)
[[45, 62, 59, 82], [150, 87, 188, 118], [212, 105, 252, 129], [157, 141, 202, 166], [210, 179, 256, 214], [268, 164, 315, 192], [284, 143, 327, 172], [317, 125, 361, 155], [361, 164, 397, 190], [375, 127, 416, 153], [421, 149, 464, 182], [414, 177, 455, 204], [299, 208, 346, 240], [382, 211, 427, 240], [431, 245, 489, 274], [505, 232, 559, 271], [518, 202, 559, 236], [549, 195, 594, 224], [504, 158, 550, 197], [587, 174, 631, 200], [604, 267, 657, 299], [470, 319, 518, 376], [432, 328, 473, 373], [432, 372, 477, 414], [385, 349, 434, 408], [384, 409, 432, 456], [326, 410, 382, 459], [309, 470, 363, 522], [207, 487, 274, 554], [84, 204, 108, 237], [467, 188, 509, 217]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 4, 3, 3]
(13, 576, 720)
[[167, 394, 235, 496], [372, 481, 453, 570], [332, 342, 382, 403], [257, 351, 297, 406], [238, 264, 278, 299], [328, 279, 407, 312], [386, 303, 460, 354], [473, 275, 550, 312], [291, 215, 317, 234], [326, 209, 346, 233], [440, 262, 451, 295], [399, 218, 421, 241], [467, 214, 486, 237]]
[3, 4, 1, 3]
(4, 576, 720)
[[404, 247, 427, 276], [458, 258, 472, 290], [482, 197, 518, 241], [487, 255, 513, 287]]
[3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(13, 576, 720)
[[129, 353, 180, 409], [41, 402, 115, 507], [271, 305, 305, 348], [232, 282, 263, 323], [162, 254, 219, 293], [297, 260, 325, 289], [332, 228, 354, 251], [364, 467, 444, 551], [496, 365, 560, 443], [600, 354, 671, 422], [454, 268, 485, 303], [486, 251, 512, 284], [477, 204, 493, 225]]
[3, 4, 3, 4, 3, 1, 3, 1, 3, 3, 3, 2, 3, 3]
(14, 576, 720)
[[250, 476, 325, 568], [308, 311, 324, 357], [164, 290, 235, 326], [56, 285, 90, 329], [156, 261, 209, 292], [222, 215, 271, 266], [291, 231, 315, 263], [322, 199, 354, 230], [447, 280, 481, 336], [496, 322, 540, 371], [546, 380, 617, 447], [473, 228, 508, 260], [405, 214, 426, 243], [331, 311, 370, 356]]
[2, 3, 3, 3, 3, 3, 3, 3, 3]
(9, 576, 720)
[[336, 382, 396, 477], [268, 294, 303, 331], [244, 272, 275, 305], [286, 242, 315, 270], [342, 219, 362, 243], [550, 371, 621, 450], [501, 333, 551, 391], [529, 304, 573, 353], [495, 254, 525, 301]]
[3, 3, 1, 1, 3, 4, 3, 3, 2]
(9, 576, 720)
[[130, 104, 164, 127], [175, 121, 212, 146], [294, 123, 374, 171], [377, 126, 458, 185], [450, 176, 492, 206], [621, 216, 644, 241], [570, 255, 615, 289], [456, 323, 493, 360], [107, 447, 137, 517]]
[3, 3, 3, 3, 3, 1, 3, 2, 4, 3, 1, 4, 3, 1]
(14, 576, 720)
[[117, 337, 168, 390], [172, 357, 223, 415], [154, 424, 218, 539], [259, 405, 320, 475], [339, 342, 382, 401], [398, 213, 448, 324], [355, 261, 387, 297], [268, 239, 295, 276], [248, 253, 263, 283], [312, 223, 338, 245], [488, 202, 534, 298], [467, 279, 483, 312], [284, 220, 306, 239], [238, 180, 277, 237]]
[3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(12, 576, 720)
[[299, 219, 349, 272], [540, 124, 584, 174], [189, 136, 238, 185], [578, 87, 615, 128], [623, 27, 658, 71], [79, 71, 119, 107], [132, 104, 174, 145], [95, 299, 127, 360], [59, 426, 102, 494], [484, 149, 527, 196], [573, 133, 617, 185], [654, 78, 686, 140]]
[3, 3, 3, 3, 3, 1, 1, 4, 4, 3, 3, 3, 3, 3]
(14, 576, 720)
[[162, 418, 225, 495], [180, 357, 227, 412], [258, 337, 302, 385], [210, 251, 255, 284], [260, 237, 292, 268], [243, 197, 278, 238], [501, 183, 530, 233], [480, 216, 491, 239], [403, 217, 414, 241], [490, 256, 521, 289], [449, 253, 474, 289], [399, 261, 427, 292], [524, 292, 577, 336], [654, 277, 715, 314]]
[3, 3, 4, 3, 3, 2, 3, 3, 3, 3, 3, 3, 4]
(13, 576, 720)
[[255, 446, 322, 525], [377, 429, 455, 530], [321, 383, 347, 455], [250, 268, 282, 299], [111, 266, 168, 300], [274, 238, 302, 274], [314, 229, 336, 251], [351, 206, 371, 223], [469, 270, 512, 337], [500, 321, 559, 395], [556, 326, 607, 390], [637, 277, 713, 319], [454, 219, 463, 238]]
[4, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, 1]
(28, 576, 720)
[[237, 69, 256, 96], [259, 76, 305, 121], [314, 95, 377, 165], [268, 120, 311, 160], [253, 182, 304, 230], [281, 166, 336, 214], [317, 151, 361, 196], [508, 454, 588, 532], [204, 150, 249, 197], [106, 19, 145, 56], [119, 66, 158, 101], [280, 54, 322, 87], [110, 210, 155, 246], [273, 414, 329, 478], [319, 253, 382, 318], [462, 205, 512, 260], [411, 88, 462, 121], [477, 57, 542, 112], [614, 6, 647, 41], [585, 44, 619, 78], [541, 75, 579, 125], [579, 82, 621, 131], [528, 130, 574, 182], [557, 150, 599, 203], [662, 193, 700, 256], [617, 40, 652, 78], [632, 62, 672, 106], [384, 253, 543, 389]]
[3, 4, 3, 3, 3, 3, 3, 3]
(8, 576, 720)
[[88, 201, 116, 229], [106, 258, 120, 288], [95, 331, 125, 377], [106, 429, 135, 483], [339, 465, 390, 517], [475, 334, 520, 379], [543, 282, 582, 315], [445, 176, 487, 205]]
[3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3]
(15, 576, 720)
[[175, 89, 217, 120], [229, 107, 258, 130], [264, 117, 299, 139], [307, 120, 346, 151], [364, 166, 405, 192], [431, 180, 473, 208], [507, 157, 547, 181], [60, 68, 95, 91], [90, 241, 113, 279], [87, 302, 114, 344], [85, 437, 114, 495], [351, 409, 395, 458], [446, 334, 494, 389], [520, 280, 569, 331], [617, 247, 665, 280]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3]
(15, 576, 720)
[[393, 165, 430, 198], [342, 162, 380, 189], [270, 124, 308, 157], [233, 108, 270, 134], [294, 151, 334, 176], [196, 113, 233, 139], [127, 98, 163, 123], [99, 77, 133, 104], [411, 377, 463, 425], [507, 397, 576, 470], [538, 276, 583, 313], [80, 178, 105, 209], [97, 225, 110, 251], [85, 268, 108, 308], [323, 454, 378, 504]]
[3, 2, 3, 3, 3, 3, 1, 4, 3, 3, 3, 3]
(12, 576, 720)
[[299, 219, 349, 272], [540, 124, 584, 174], [579, 85, 618, 126], [622, 27, 658, 72], [146, 107, 194, 154], [189, 230, 240, 273], [95, 230, 225, 370], [73, 284, 88, 318], [70, 340, 105, 409], [331, 386, 399, 451], [451, 328, 507, 393], [558, 436, 633, 507]]
Loss is nan, stopping training
{'loss_classifier': tensor(nan, device='cuda:0', grad_fn=<NllLossBackward>), 'loss_box_reg': tensor(nan, device='cuda:0', grad_fn=<DivBackward0>), 'loss_mask': tensor(-2.1386e+22, device='cuda:0',
grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_objectness': tensor(0.7630, device='cuda:0', grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_rpn_box_reg': tensor(0.0572, device='cuda:0', grad_fn=<DivBackward0>)}
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
/home/centos/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3435: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
Related
Saving an array of uint8array in an file using node js
I'm trying to save an array of uint8arrays in an file using node JS, does anyone know how can I do that using fs? Something like that: [ Uint8Array(162) [ 133, 111, 74, 131, 187, 35, 69, 135, 1, 151, 1, 1, 198, 69, 229, 121, 168, 106, 59, 104, 91, 198, 115, 218, 79, 67, 102, 212, 204, 206, 145, 126, 152, 59, 147, 114, 133, 173, 119, 91, 220, 251, 174, 57, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 2, 2, 251, 255, 149, 134, 6, 9, 65, 100, 100, 32, 99, 97, 114, 100, 50, 0, 9, 1, 2, 2, 4, 19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87, 26, 112, 2, 3, 0, ... 62 more items ], Uint8Array(170) [ 133, 111, 74, 131, 67, 31, 18, 227, 1, 159, 1, 1, 187, 35, 69, 135, 0, 7, 102, 52, 237, 242, 169, 192, 199, 237, 238, 142, 226, 200, 98, 129, 144, 184, 181, 198, 33, 248, 228, 223, 158, 171, 250, 192, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 3, 5, 251, 255, 149, 134, 6, 8, 65, 100, 100, 32, 99, 97, 114, 100, 0, 10, 1, 2, 2, 4, 17, 4, 19, 4, 21, 14, 52, 3, 66, 4, 86, 5, 87, 29, 112, 2, 3, ... 70 more items ], Uint8Array(120) [ 133, 111, 74, 131, 71, 17, 199, 138, 1, 110, 1, 67, 31, 18, 227, 131, 113, 252, 222, 71, 172, 34, 205, 40, 96, 11, 236, 242, 153, 43, 182, 4, 136, 135, 36, 6, 79, 52, 223, 123, 188, 42, 7, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 4, 8, 251, 255, 149, 134, 6, 11, 68, 101, 108, 101, 116, 101, 32, 99, 97, 114, 100, 0, 10, 1, 2, 2, 2, 17, 2, 19, 2, 52, 1, 66, 2, 86, 2, 112, 2, 113, 2, 115, ... 20 more items ], Uint8Array(126) [ 133, 111, 74, 131, 51, 54, 68, 129, 1, 116, 1, 71, 17, 199, 138, 51, 167, 78, 185, 254, 251, 73, 245, 53, 153, 253, 146, 203, 139, 250, 206, 105, 223, 51, 227, 156, 55, 3, 146, 177, 243, 235, 140, 16, 125, 67, 180, 114, 128, 5, 72, 143, 131, 122, 35, 124, 139, 62, 93, 187, 5, 9, 251, 255, 149, 134, 6, 17, 77, 97, 114, 107, 32, 99, 97, 114, 100, 32, 97, 115, 32, 100, 111, 110, 101, 0, 9, 1, 2, 2, 2, 21, 6, 52, 1, 66, 2, 86, 2, 112, ... 26 more items ] ]
I am simply using the following, for exporting into docx file: buffer = Uint8Array(86762) [ 80, 75, 3, 4, 10, 0, 0, 0, 8, 0, 79, 72, 111, 85, 212, 85, 145, 41, 234, 1, 0, 0, 43, 11, 0, 0, 19, 0, 0, 0, 91, 67, 111, 110, 116, 101, 110, 116, 95, 84, 121, 112, 101, 115, 93, 46, 120, 109, 108, 197, 150, 205, 78, 227, 48, 20, 133, 247, 60, 133, 229, 45, 106, 92, 64, 26, 141, 80, 83, 22, 252, 44, 25, 164, 233, 60, 128, 27, 223, 164, 134, 248, 71, 182, 91, 232, 219, 207, 117, 66, 67, 169, 18, 198, 154, 54, 98, 83, 41, 182, ... 86662 more items ] fs.writeFileSync('output.docx', buffer) I suppose restoring it will be just fs.readFileSync(pathToFile)
'NoneType' object is not iterable error in python3
I am trying to write a code that takes my input as a command(TEAM),(given name of the team) and then returns to the number of player who are playing in the team that is given in the input.But pycharm gives a'NoneType' object is not iterable error and I do not know how can I turn it into code that workes.Can you help me find where I went wrong? By the way,I assume that there are mistakes even in the way of asking this question, if you share it with me, I will try to improve myself, thanks in advance !! baseball_stats = [['Martin,Leonys', 'TEX', 147, 457, 66, 119, 21, 6, 8], ['Smoak,Justin', 'SEA', 131, 454, 53, 108, 19, 0, 20], ['Ibanez,Raul', 'SEA', 124, 454, 54, 110, 20, 2, 29], ['Infante,Omar', 'DET', 118, 453, 54, 144, 24, 3, 10], ['Bautista,Jose', 'TOR', 118, 452, 82, 117, 24, 0, 28], ['Blanco,Gregor', 'SF', 141, 452, 50, 120, 17, 6, 3], ['Rosario,Wilin', 'COL', 121, 449, 63, 131, 22, 1, 21], ['Uggla,Dan', 'ATL', 136, 448, 60, 80, 10, 3, 22], ['Moss,Brandon', 'OAK', 145, 446, 73, 114, 23, 3, 30], ['Tulowitzki,Troy', 'COL', 126, 446, 72, 139, 27, 0, 25], ['Mauer,Joe', 'MIN', 113, 445, 62, 144, 35, 0, 11], ['Overbay,Lyle', 'NYY', 142, 445, 43, 107, 24, 1, 14], ['Pollock,A.J.', 'ARI', 137, 443, 64, 119, 28, 5, 8], ['Drew,Stephen', 'BOS', 124, 442, 57, 112, 29, 8, 13], ['Viciedo,Dayan', 'CWS', 124, 441, 43, 117, 23, 3, 14], ['Dirks,Andy', 'DET', 131, 438, 60, 112, 16, 2, 9], command, name = input().split() def baseball_stat_machine(command, name): command=input() if command=="TEAM": name=input() for ply in baseball_stats: if name==ply[1]: result=baseball_stats.count(ply[0] result = baseball_stat_machine(command, name) if type(result) == int: print(result)
You must first enter the Information then call the function and finally print the result, if a player is found like so baseball_stats = [['Martin,Leonys', 'TEX', 147, 457, 66, 119, 21, 6, 8], ['Smoak,Justin', 'SEA', 131, 454, 53, 108, 19, 0, 20], ['Ibanez,Raul', 'SEA', 124, 454, 54, 110, 20, 2, 29], ['Infante,Omar', 'DET', 118, 453, 54, 144, 24, 3, 10], ['Bautista,Jose', 'TOR', 118, 452, 82, 117, 24, 0, 28], ['Blanco,Gregor', 'SF', 141, 452, 50, 120, 17, 6, 3], ['Rosario,Wilin', 'COL', 121, 449, 63, 131, 22, 1, 21], ['Uggla,Dan', 'ATL', 136, 448, 60, 80, 10, 3, 22], ['Moss,Brandon', 'OAK', 145, 446, 73, 114, 23, 3, 30], ['Tulowitzki,Troy', 'COL', 126, 446, 72, 139, 27, 0, 25], ['Mauer,Joe', 'MIN', 113, 445, 62, 144, 35, 0, 11], ['Overbay,Lyle', 'NYY', 142, 445, 43, 107, 24, 1, 14], ['Pollock,A.J.', 'ARI', 137, 443, 64, 119, 28, 5, 8], ['Drew,Stephen', 'BOS', 124, 442, 57, 112, 29, 8, 13], ['Viciedo,Dayan', 'CWS', 124, 441, 43, 117, 23, 3, 14], ['Dirks,Andy', 'DET', 131, 438, 60, 112, 16, 2, 9]] def baseball_stat_machine(command, name): if command=="TEAM": for ply in baseball_stats: if name==ply[0]: result=baseball_stats.count(ply[0]) return result command=input("Enter command form example TEAM: ") name=input("ENter a name: ") result = baseball_stat_machine(command, name) if type(result) == int: print(result) This will count the TEAM you enter for SEA is two times there baseball_stats = [['Martin,Leonys', 'TEX', 147, 457, 66, 119, 21, 6, 8], ['Smoak,Justin', 'SEA', 131, 454, 53, 108, 19, 0, 20], ['Ibanez,Raul', 'SEA', 124, 454, 54, 110, 20, 2, 29], ['Infante,Omar', 'DET', 118, 453, 54, 144, 24, 3, 10], ['Bautista,Jose', 'TOR', 118, 452, 82, 117, 24, 0, 28], ['Blanco,Gregor', 'SF', 141, 452, 50, 120, 17, 6, 3], ['Rosario,Wilin', 'COL', 121, 449, 63, 131, 22, 1, 21], ['Uggla,Dan', 'ATL', 136, 448, 60, 80, 10, 3, 22], ['Moss,Brandon', 'OAK', 145, 446, 73, 114, 23, 3, 30], ['Tulowitzki,Troy', 'COL', 126, 446, 72, 139, 27, 0, 25], ['Mauer,Joe', 'MIN', 113, 445, 62, 144, 35, 0, 11], ['Overbay,Lyle', 'NYY', 142, 445, 43, 107, 24, 1, 14], ['Pollock,A.J.', 'ARI', 137, 443, 64, 119, 28, 5, 8], ['Drew,Stephen', 'BOS', 124, 442, 57, 112, 29, 8, 13], ['Viciedo,Dayan', 'CWS', 124, 441, 43, 117, 23, 3, 14], ['Dirks,Andy', 'DET', 131, 438, 60, 112, 16, 2, 9]] def baseball_stat_machine(command, name): if command=="TEAM": result=sum(ply[1].count(name) for ply in baseball_stats) return result command=input("Enter command form example TEAM: ") name=input("ENter a name: ") result = baseball_stat_machine(command, name) if type(result) == int: print(result)
Find the values in a list using a nested list of indices?
I am trying to find the solution to this problem. Given a list of numbers: numbers = [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501, 440, 771, 601, 582, 658, 954, 1154, 1175, 1459, 1786, 1667, 2186, 2558, 2774, 3388, 3448, 4070, 4785, 6060, 4268, 5642, 5236, 4774, 5849, 5966, 6361, 6198, 6411, 5841, 7099, 7933, 9623, 10633, 10581, 10102, 10559, 11231, 10699, 10817, 11012, 11656, 10899, 10028, 9974, 10598, 9200, 9709, 8926, 9263, 8764, 8849, 8894, 9434, 8599, 8946, 8915, 8338, 8371, 8572, 8952, 9268, 8485, 8858, 8529, 8823, 8718, 8846, 8971, 8970, 8587, 8393, 8777, 8961, 8697, 8809, 8217, 8241, 7824, 7772, 7971, 7870, 7717, 7586, 7413, 7165, 7105, 6788, 6843, 6784, 6683, 6683, 6550, 6752, 6710, 6623, 6719, 6569, 6363, 6534, 6491, 6623, 6586, 6587, 6511, 6240, 6410, 6415, 6389, 6214, 6096, 5901, 5828, 5850, 5830, 5779, 5833, 5741, 5607, 5380, 5449, 5484, 5468, 5429, 5387, 5364, 5121, 5186, 5239, 5191, 5185, 5155, 5081, 4892, 5054, 5017, 5016, 5030, 4911, 4839, 4718, 4790, 4767, 4838, 4860, 4797, 4688, 4639, 4642, 4675, 4758, 4843, 4897, 4932, 4670, 4893, 4958, 5064, 5144, 5097, 5106, 5020, 5172, 5310, 5421, 5406, 5361, 5414, 5435, 5612, 5667, 5803, 5960, 6043, 6090, 6109, 6330, 6489, 7112, 7421, 7764, 8026, 8129, 8371, 8835, 9294, 9735, 10376, 10757, 11481, 10981, 11345, 11969, 12673, 13442, 13406, 13690, 14041, 13556, 14937, 14703, 14804, 15843, 16108, 15444, 15704, 17077, 16260, 16392, 17148, 16342, 15886, 17418, 17987, 17834, 18381, 18017, 18431, 19483, 19116, 20368, 20109, 20248, 21577, 20765, 19583, 21333, 21717, 22441, 22313, 22562, 22201, 20717, 23337, 24059, 24538, 24295, 24891, 24087, 23393, 25195, 27267, 26809, 26390, 26046, 26126, 25043, 27829, 27078, 28450, 28701, 27798, 25752, 25838, 27562, 28206, 27729, 27651, 26902, 26265, 26074, 27787, 28116, 27772, 28510, 28917, 28340, 26814, 29499, 28595, 28833, 27849, 27363, 26588, 26095, 27329, 26613, 25938, 23845, 23015, 23955, 23902, 23218, 23330, 23012, 22540] and a nested list of indices: nested_list = [[0, 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, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259], [260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290], [291, 292, 293, 294, 295, 296, 297, 298, 299, 300]] using the nested list of indices I need to return the values of the items in the list. example output would be: [[27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501],[440, 771, 601, 582, 658, 954, 1154, 1175, 1459, 1786, 1667, 2186, 2558, 2774, 3388, 3448, 4070, 4785, 6060, 4268, 5642, 5236, 4774, 5849, 5966, 6361, 6198, 6411, 5841, 7099], etc.... The current piece of code I have is values_from_list = [] for i in nested_list: values_from_list.append([numbers[i] for i in nested_list[0]]) print(values_from_list) which is only returning the set of values i require repeated for i in nested_list output i am currently getting: [[27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501], [27, 24, 33, 52, 54, 53, 61, 71, 57, 163, 182, 196, 228, 270, 302, 501]] I hope my question can be understood, Many Thanks!
Where you are using nested_list[0] you should use i; and then you should use another variable where you use i in the comprehension. Many errors hide in unclear variable naming; one should strive to make sure variable names always correctly identify their purpose. I would rewrite as follows: values_from_list = [] for sublist in nested_list: values_from_list.append([numbers[index] for index in sublist]) print(values_from_list) or, more succintly, values_from_list = [ [numbers[index] for index in sublist] for sublist in nested_list ]
Compute optimal route for picking ressources
Here are the rules (Halite 3): At each turn, you have to take one decision for your ship. You can either move right, move left, move up, move down or stay still (and gather ressoureces). Moving your ship cost your ship 10% of current's cell available ressources. Staying still cost you nothing and you pick 25% of current cell available ressources. If a ship doesn't have enought ressources to pay the 10% to move, he is pinned on the cell for this turn (can't go negative). My goal: The ship starts at the center (0) and should return at this point with atleast (X amount of ressources) in the less turn possible. How can I compute the most efficient path (round wise) that my ship should take to achieve my goal ? Here is some code to compute a random map (the map is random every game) : import random how_Big = 11 center_Pos = int(how_Big/2) #how_Big must be even game_Map = [[random.randint(1,500) for i in range(how_Big)] for x in range(how_Big)] game_Map[center_Pos][center_Pos] = 0 for y in range(how_Big): print(game_Map[y]) [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] I am not really sure how to start. Is there any known algorithm for this ? Thanks Additionnal informations : The matrix is the available ressources at the beginning on each cell. Ship begins with 0 ressources. Ship gains ressources by staying still for a round on a cell (collecting 25% of the cell's ressources). Ship spends ressources (of his own cargo) by moving (the cost is 10% of current cell's available ressources). The path can be returned in anyway, for example (moves = ["up", "still", "still", "up", "still", "still", "down", "down"]). BruteForcing it would be acceptable. X would be the minimum ammount I want my ship's cargo to have when returning to center. Map always has a center Cell Ressources collected or used are always rounded to nearest Int Ressources collected by a ship are removed from the cell Here is an example of what can happen : move_Dict = { "up": [-1, 0], "down": [1, 0], "right": [0, 1], "left": [0, -1], "still": [0, 0], } moves = ["up", "still", "still", "up", "still", "still", "down", "down"] position = [5, 5] cargo = 0 print("initial map :") for y in range(how_Big): print(game_Map[y]) for move in moves: if move == "still": ressources_Collected = round(game_Map[position[0]][position[1]] * 0.25) cargo += ressources_Collected game_Map[position[0]][position[1]] -= ressources_Collected else: ressources_Used = round(game_Map[position[0]][position[1]] * 0.10) cargo -= ressources_Used position = [position[0]+move_Dict[move][0], position[1]+move_Dict[move][1]] print(f"ship is at position {position}, cargo = {cargo}") for y in range(how_Big): print(game_Map[y]) Output: initial map : [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [4, 5], cargo = 0 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 172, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [4, 5], cargo = 43 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 129, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [4, 5], cargo = 75 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [3, 5], cargo = 65 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 479, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [3, 5], cargo = 185 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 359, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [3, 5], cargo = 275 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [4, 5], cargo = 248 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] ship is at position [5, 5], cargo = 238 [110, 179, 97, 467, 347, 336, 368, 298, 107, 84, 123] [415, 86, 12, 75, 354, 74, 250, 221, 51, 254, 252] [368, 235, 389, 1, 155, 186, 149, 135, 458, 243, 344] [391, 480, 485, 358, 416, 269, 270, 354, 203, 436, 146] [62, 132, 490, 33, 445, 97, 127, 274, 130, 77, 356] [239, 11, 459, 245, 214, 0, 324, 162, 58, 394, 202] [241, 395, 46, 78, 191, 384, 203, 191, 56, 474, 237] [85, 480, 181, 98, 122, 482, 90, 351, 257, 266, 182] [398, 125, 195, 423, 219, 290, 140, 166, 413, 499, 428] [213, 367, 142, 471, 141, 407, 382, 229, 332, 455, 53] [207, 12, 319, 54, 246, 274, 474, 312, 170, 374, 188] [Finished in 0.1s]
Palindrome in Python not working
I am trying to make a simple program that displays the palindrome numbers between 2 numbers in Python3 but it doesn't seem to work. I get only 4 output which are 1,2,4 and 8. What about the other numbers like 11, 22, 33, ..., 111, 121,131,..., 191, 222, etc? Here is my code. I can't figure out why it's not working. a = 0 b = 500 a += 1 for i in range(a,b): if(str(a) == str(a)[::-1]): print(a) a += a
Maybe for i in range (0, 500): if str(i) == str(i)[::-1]: print(i)
>>> palindromes = [a for a in range(500) if str(a) == str(a)[::-1]] >>> palindromes [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494] That shows you the principle. If you want to do it in a function, you can use yield instead of constructing the whole list in memory (much more efficient): >>> def palindromes(a, b): ... """Return palindromes in closed interval from a to b""" ... for i in range(a, b): ... if str(i) == str(i)[::-1]: ... yield i ... >>> list(palindromes(0, 500)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494]