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

时间:2017-12-10 09:03:12

标签: python data-structures queue

我正在尝试将列表中的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.

1 个答案:

答案 0 :(得分:1)

我对您的队列class进行了一些更改。主要是,.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)