我试图将 3 个人排入列表中,以显示每个人的结果,他们的所有姓名都位于顶部,但只能获得一个没有任何姓名的结果:
Contacting the following
Phone answered: Yes
Booked an appointment: No
Reshedule an appointment again.
我想让输出显示他们的所有姓名和 3 个输出,每个人一个来自 'names'
中存储的信息,并且每个名称不会出现两次。
我想使用队列根据列表对它们进行优先级排序,因此我尝试将它们按顺序排列。 if 和 elif 是根据随机生成器属于任一类别的条件。现在,只是未定义在其中包含名称的方法。
代码
import random
class Queue:
def __init__(self):
self.container = []
def isEmpty(self):
return self.size() == 0
def enqueue(self, item):
self.container.append(item)
def dequeue(self):
self.container.pop(0)
def size(self):
return len(self.container)
def peek(self) :
return self.container[0]
names = ["Alvin", "James", "Peter"]
# Enqueuing
q = Queue()
q.enqueue(random.choice(names))
# Dequeuing and Printing
print("Contacting the following:\n" + "\n".join(q.container)) # unsure about this
for i in range(q.size()):
answered = random.randint(0,1)
booked = random.randint(0, 1)
if(answered == 1 and booked == 1):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: Yes")
print("Booked an appointment: Yes")
print("Booking successful.")
elif(answered==1 and booked==0):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: Yes")
print("Booked an appointment: No")
print("Reshedule an appointment again.")
elif(answered == 0):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: No")
print("Reshedule a callback.")
q.dequeue()
所需输出示例:
Contacting the following
Alvin
James
Peter
Now Calling - James
Phone answered: No
Reshedule a callback.
最佳答案
我对您的队列类
进行了一些更改。首先,.dequeue
方法没有返回它弹出的项目,因此它返回默认值 None
。
我还将 .size
方法更改为 __len__
,以便您可以将 Queue
实例传递给内置 len
函数。并为其提供了一个 iter
方法,您可以轻松地在 for
循环中使用它,或将其传递给 .join
。我还将 .isEmpty
更改为 .is_empty
以符合 Python 的 PEP-0008 样式指南。
由于您希望将每个名称随机添加到队列中而不重复,因此我们不希望此处使用 random.choice
。相反,我们可以使用random.shuffle
;另一种选择是使用random.sample
,尽管当您想从列表中进行部分选择时,这更合适。
from random import seed, shuffle, randrange
# Seed the randomizer so we can reproduce results while testing
seed(9)
class Queue:
def __init__(self):
self.container = []
def __len__(self):
return len(self.container)
def is_empty(self):
return len(self) == 0
def enqueue(self, item):
self.container.append(item)
def dequeue(self):
return self.container.pop(0)
def peek(self) :
return self.container[0]
def __iter__(self):
return iter(self.container)
names = ["Alvin", "James", "Peter"]
# Enqueuing
q = Queue()
# Make a temporary copy of the names that we can
# shuffle without affecting the original list
temp = names.copy()
shuffle(temp)
# Put the shuffled names onto the queue
for name in temp:
q.enqueue(name)
# Dequeuing and Printing
print("Contacting the following")
print('\n'.join(q))
#for name in q:
#print(name)
while not q.is_empty():
name = q.dequeue()
print('\nNow Calling -', name)
answered = randrange(2)
booked = randrange(2)
if answered:
print("Phone answered: Yes")
if booked:
print("Booked an appointment: Yes")
print("Booking successful.")
else:
print("Booked an appointment: No")
print("Reshedule an appointment again.")
else:
print("Phone answered: No")
print("Reshedule a callback.")
输出
Contacting the following
Alvin
Peter
James
Now Calling - Alvin
Phone answered: Yes
Booked an appointment: No
Reshedule an appointment again.
Now Calling - Peter
Phone answered: No
Reshedule a callback.
Now Calling - James
Phone answered: Yes
Booked an appointment: Yes
Booking successful.
在上面的代码中我使用了
print('\n'.join(q))
打印所有名称,因为您在代码中使用.join
来实现此目的。但我还展示了使用简单的 for
循环的替代方法,但我将其注释掉了:
for name in q:
print(name)
关于python - 从队列数据结构列表中调用项目 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737343/