我确定这是一个非常简单的问题,但我很难修改对象列表中单个对象内的元素。我有一个名为志愿者设置的对象:
#Create the volunteer class
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
locWorked = []
schedule = []
def __init__(self, employeeId):
self.employeeId = employeeId
def updateSchedule(self,location):
self.schedule.append(location)
def newDayOff(self):
self.totalDaysOff =+1
self.consecutiveDaysOff =+ 1
self.schedule.insert(0,0)
我有一份志愿者名单:
v1 = Volunteer(1)
v2 = Volunteer(2)
v3 = Volunteer(3)
volunteerList = [v1,v2,v3]
我想使用以下命令在列表的一个元素上运行newDayOff():volunteerList [0] .newDayOff()
但每次我这样做都会在列表中的每个Volunteer实例上运行newdayoff()。对此有什么正确的方法?
import csv
#Create the volunteer class
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
locWorked = []
schedule = []
def __init__(self, employeeId):
self.employeeId = employeeId
def updateSchedule(self,location):
self.schedule.append(location)
def newDayOff(self):
self.totalDaysOff =+1
self.consecutiveDaysOff =+ 1
self.schedule.insert(0,0)
# Test Variables
v1 = Volunteer(1)
v2 = Volunteer(2)
v3 = Volunteer(3)
v2.consecutiveDaysOff = 1
v1.totalDaysOff = 1
v2.totalDaysOff = 3
v3.totalDaysOff = 4
numVolunteers = 3
location = [1]
numLocation = 1
volunteerList = [v1,v2,v3]
#test inputs
#Take inputs
# try:
# days = int(input("How many days are in the month? ") )
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
#
# try:
# numVolunteers = input("How many volunteers are there? ")
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
# try:
# location = input("What are the locations? (Should be numbers separated by ',')")
#
# if location == "":
# print("Locations were not entered.")
# exit()
#
# numLocation = 1
# for i in range(0,len(location)):
# if location[i] == ',':
# numLocation = numLocation + 1
#
# location = location.replace(" ", "")
# location = location.split(',')
# location = list(map(int, location))
#
# except ValueError:
# print("Invalid Input... Please Restart Program")
# exit()
#create the volunteer list
def createVolunteerList():
for i in range(0,numVolunteers):
volunteerList.append(Volunteer(i))
def sortArrayByConsecDaysOff():
for i in range(0,len(volunteerList)):
for j in range(0,len(volunteerList)):
if volunteerList[i].consecutiveDaysOff > volunteerList[j].consecutiveDaysOff:
volunteerList[i],volunteerList[j] = volunteerList[j],volunteerList[i]
def sortArrayByTotalDaysOff():
for i in range(0,len(volunteerList)):
for j in range(0,len(volunteerList)):
if volunteerList[i].totalDaysOff < volunteerList[j].totalDaysOff:
volunteerList[i], volunteerList[j] = volunteerList[j], volunteerList[i]
def printVolunteer():
for i in range(0,len(volunteerList)):
v = volunteerList[i]
print(v.employeeId, " " ,v.consecutiveDaysOff, " " ,v.totalDaysOff, " " , v.locWorked, " ", v.schedule, "\n")
def assignDaysOff():
# Define number of days off that can be taken that day
remainingDaysOff = numVolunteers - numLocation
# Loop through volunteers to give weekends to those that have had one consecutive day off.
for i in range(0, len(volunteerList)):
if volunteerList[i].consecutiveDaysOff == 1 and remainingDaysOff > 0:
volunteerList[i].newDayOff()
remainingDaysOff = remainingDaysOff - 1
sortArrayByTotalDaysOff()
# Loop through volunteers to assign weekends to those with the fewest days off so far.
for i in range(0, len(volunteerList)):
if remainingDaysOff > 0 and not volunteerList[i]:
volunteerList[i].newDayOff()
remainingDaysOff = remainingDaysOff - 1
printVolunteer()
volunteerList[0].newDayOff()
printVolunteer()
答案 0 :(得分:1)
改变这个:
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
locWorked = []
schedule = []
到此:
class Volunteer():
consecutiveDaysOff = 0
totalDaysOff = 0
def __init__(self):
self.locWorked = list()
self.schedule = list()
您的代码在您的Volunteer对象的每个实例中使用相同的列表实例。
答案 1 :(得分:0)
更改此
> class Volunteer():
> consecutiveDaysOff = 0
> totalDaysOff = 0
> locWorked = []
> schedule = []
>
> def newDayOff(self):
> self.totalDaysOff =+1
> self.consecutiveDaysOff =+ 1
> self.schedule.insert(0,0)
要
> class Volunteer():
> def __init__(self):
> self.consecutiveDaysOff = 0
> self.totalDaysOff = 0
> self.locWorked = []
> self.schedule = []
>
> def newDayOff(self):
> self.totalDaysOff =+1
> self.consecutiveDaysOff =+ 1
> self.schedule.insert(0,0)
这应解决问题..