Skip to main content
 首页 » 编程设计

python之从队列数据结构列表中调用项目 (Python)

2024年10月01日6artech

我试图让列表中的 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)