我正在解决一个类项目,我遇到了这个问题(当我向数组追加一个新对象时,似乎所有元素都指向同一个对象) 这是我的代码
import os
from Process import Process
processes = []
# Read the file
file = open("file.txt","r")
# Read the lines
for line in file.readlines() :
# split the line components
process = line.split(";")
global processes
# append a new element to the array
pr = Process(process[0], process[1], "Ready")
# find how many elements in the line to loop through
process_len = len(process)
# loop through the line elements
for element in range(2,process_len) :
# if it's a CPU
if process[element].find("CPU") == 0:
s = process[element].split(":")
pr.add_cpu(s[1])
# if it's an IO
elif process[element].find("IO") == 0:
s = process[element].split(":")
pr.add_io(s[1])
# End of elif
processes.append(pr)
file.close()
print "-------------------------- \n";
for p in processes :
print p.getcpu()
这是我正在阅读的文件:
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35;IO:5;CPU:1
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;
1;500;CPU:20;IO:5;CPU:10;IO:15;CPU:30;IO:11;CPU:35
1;500;CPU:20;IO:5;CPU:10;IO:15;
这是输出:
--------------------------
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
['20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10', '20', '10', '30', '35', '1\n', '20', '10', '30', '20', '10', '30', '35\n', '20', '10']
Process finished with exit code 0
我认为问题在于我创建了对同一个对象的新引用,并且我不断将它们作为新对象添加到数组中,但我不知道如何确定是否是这种情况或我错了,如果是这样的话,我怎么能解决这个问题呢?
编辑: 这是流程类:
class Process :
pid = 0;
pat = 0;
wt = 0;
global ion;
ion = 0;
global cpun;
cpun = 0;
global pState;
pState = "Ready";
global pNature;
pNature = "I\O bound";
global turnAroundTime;
turnAroundTime = 0;
global cpu;
cpu = []
global io;
io = []
# constructor
def __init__(self,id,at,state) :
pid = id;
pat = at;
pState = state;
# define the process nature
@staticmethod
def nature() :
global pNature;
global ion;
global cpun;
if ion > cpun :
pNature = "I\O bound";
else :
pNature = "CPU bound";
# add new cpu
@staticmethod
def add_cpu(cp) :
cpu.append(cp);
global cpun;
cpun = cpun + int(cp);
# add new io
@staticmethod
def add_io(i) :
io.append(i);
global ion;
ion = ion + int(i);
# get the cpu array size
@staticmethod
def get_cpu_size() :
return len(cpu);
# get the IO array size
@staticmethod
def get_io_size() :
return len(io)
# returns the cpu array
@staticmethod
def getcpu():
return cpu
答案 0 :(得分:1)
类过程的变量对于pr的每个实例都是共同的。所以你每次都在粉碎价值观。
import os
processes = []
class Process():
# constructor
def __init__(self, id, at, state):
self.pid = id
self.pat = at
self.pState = state
self.wt = 0
self.ion = 0
self.cpun = 0
self.pNature = "I\O bound"
self.turnAroundTime = 0
self.cpu = []
self.io = []
# define the process nature
def nature(self):
if self.ion > self.cpun:
self.pNature = "I\O bound"
else:
self.pNature = "CPU bound"
# add new cpu
def add_cpu(self, cp):
self.cpu.append(cp)
self.cpun = self.cpun + int(cp)
# add new io
def add_io(self, i):
self.io.append(i)
self.ion = self.ion + int(i)
# get the cpu array size
def get_cpu_size(self):
return len(self.cpu)
# get the IO array size
def get_io_size(self):
return len(self.io)
# returns the cpu array
def getcpu(self):
return self.cpu
# Read the file
file = open("file.txt", "r")
# Read the lines
for line in file.readlines():
# split the line components
process = line.split(";")
# append a new element to the array
pr = Process(process[0], process[1], "Ready")
# find how many elements in the line to loop through
process_len = len(process)
# loop through the line elements
for element in range(2, process_len):
# if it's a CPU
if process[element].find("CPU") == 0:
s = process[element].split(":")
pr.add_cpu(s[1])
# if it's an IO
elif process[element].find("IO") == 0:
s = process[element].split(":")
pr.add_io(s[1])
# End of elif
processes.append(pr)
file.close()
print("-------------------------- \n")
for p in processes:
print(p.getcpu())
PS:不要在Python中使用; 。并尝试使用Pep8。这是一个蟒蛇风格的指南。