运行代码时,我再次获得list2。
list1 = [range(100,201)]
print list1
list2 = [list1*5]
print list2
list3 = list2
list3 = [i * 5 for i in list3]
print list3
我尝试按照建议在另一个线程上使用此代码,但仍然无法正常工作。
my_list = [1, 2, 3, 4, 5]
my_new_list = [i * 5 for i in my_list]
print(my_new_list)
[5, 10, 15, 20, 25]
答案 0 :(得分:0)
为任务使用正确的工具:数组而不是列表。它不仅更加方便,而且速度也大大提高。
>>> import numpy as np
>>> list1 = np.arange(100, 201)
>>> list1
array([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])
>>> list2 = 5 * list1
>>> list2
array([ 500, 505, 510, 515, 520, 525, 530, 535, 540, 545, 550,
555, 560, 565, 570, 575, 580, 585, 590, 595, 600, 605,
610, 615, 620, 625, 630, 635, 640, 645, 650, 655, 660,
665, 670, 675, 680, 685, 690, 695, 700, 705, 710, 715,
720, 725, 730, 735, 740, 745, 750, 755, 760, 765, 770,
775, 780, 785, 790, 795, 800, 805, 810, 815, 820, 825,
830, 835, 840, 845, 850, 855, 860, 865, 870, 875, 880,
885, 890, 895, 900, 905, 910, 915, 920, 925, 930, 935,
940, 945, 950, 955, 960, 965, 970, 975, 980, 985, 990,
995, 1000])