Python 3.x 如何替换列表中的元素?

Python 3.x 如何替换列表中的元素?,python-3.x,Python 3.x,我真的不知道如何恰当地表达我的问题,但我会尽我最大的努力。 我正在做一个课堂项目,其中一部分是使用列表 首先,我在列表中设置随机值a=[1,1,1,0,0,0,0] 为了让这更清楚,让我们假设我正在制作一个程序来管理电影院的座位安排。1表示已占用,0表示空闲。因此,这意味着只剩下4个空缺席位。客户使用该程序,该程序为客户选择座位,因此选择了第一个零,现在只剩下三个空位。另一位客户使用该程序,同样的事情会发生,直到所有座位都有人坐,然后打印一条声明,上面写着“所有座位都有人坐” 我该怎么办 我希望

我真的不知道如何恰当地表达我的问题,但我会尽我最大的努力。 我正在做一个课堂项目,其中一部分是使用列表

首先,我在列表中设置随机值<代码>a=[1,1,1,0,0,0,0]

为了让这更清楚,让我们假设我正在制作一个程序来管理电影院的座位安排。1表示已占用,0表示空闲。因此,这意味着只剩下4个空缺席位。客户使用该程序,该程序为客户选择座位,因此选择了第一个零,现在只剩下三个空位。另一位客户使用该程序,同样的事情会发生,直到所有座位都有人坐,然后打印一条声明,上面写着“所有座位都有人坐”

我该怎么办

我希望我能恰当地回答这个问题

提前谢谢

这是我试过的。(和绝对失败的lol):

编辑:


如果第一个元素是
0
,则代码段会将其设置为
1
。您需要一种方法来找到第一个元素的索引0,并将其设置为
1


尝试使用该函数获取一个元素的索引并使用该索引…或仅将总的和当前占用的座位数保存为数字。

如果第一个元素为
0,则您的代码片段将第一个元素设置为
1
。您需要一种方法来找到第一个元素的索引0,并将其设置为
1


尝试使用该函数获取一个元素的索引并使用它…或者只将总的和当前占用的座位数保存为数字。

当您应该检查数组内容时,为什么要将i与0进行比较:

a = [1, 1, 1, 0, 0, 0, 0]
for i, val in enumerate(a):
    if val == 0:
        a[i] = 1

当您应该检查数组内容时,为什么要将i与0进行比较:

a = [1, 1, 1, 0, 0, 0, 0]
for i, val in enumerate(a):
    if val == 0:
        a[i] = 1

从这类问题中猜测,其中一些可能对您来说是新的。然而,我相信看到不同的做事方式总是很好的,你可以随时查阅资料,学到新的东西

您需要检查列表中是否有零,如果找到一个,请将其切换为1。重要的是,你应该停下来(
break
)看看,因为你不想一下子坐满所有的座位

类似这样的东西是一种可能的方式来写出来(同样,有很多方法可以做到这一点,这只是其中之一)。无论您想让某人就座多少次,您仍然需要重复此过程

for i, seat in enumerate(seats):
    if seat == 0:
        seats[i] = 1
        print("Your seat is at", i)
        break  # Stop looking for seats.
else:  # If no break happened. 
    print("No more seats!")

从这类问题中猜测,其中一些可能对您来说是新的。然而,我相信看到不同的做事方式总是很好的,你可以随时查阅资料,学到新的东西

您需要检查列表中是否有零,如果找到一个,请将其切换为1。重要的是,你应该停下来(
break
)看看,因为你不想一下子坐满所有的座位

类似这样的东西是一种可能的方式来写出来(同样,有很多方法可以做到这一点,这只是其中之一)。无论您想让某人就座多少次,您仍然需要重复此过程

for i, seat in enumerate(seats):
    if seat == 0:
        seats[i] = 1
        print("Your seat is at", i)
        break  # Stop looking for seats.
else:  # If no break happened. 
    print("No more seats!")
a[a.index(0)]=1
将用1替换找到的第一个零,如果没有更多的零,则抛出异常。您可以捕获异常:

try:
    a[a.index(0)] = 1
except ValueError:
    print('no more seats')
a[a.index(0)]=1
将用1替换找到的第一个零,如果没有更多的零,则抛出异常。您可以捕获异常:

try:
    a[a.index(0)] = 1
except ValueError:
    print('no more seats')

好的,我为您的问题提供了两个修复方法,使用
for
while
循环。我个人认为,在这种情况下,while循环更可取,以避免使用
break
命令

def original_main():
    a = [1, 1, 1, 0, 0, 0, 0]
    for i in a:
        # i here will be equal to the values in the list, what happens here is that the value is being copied to a new paramater.
        if i == 0:
            a[i] = 1

    print('original_main:', a)


def fixed_main():
    a = [1, 1, 1, 0, 0, 0, 0]

    for index, value in enumerate(a):
        # What happens here is that enumerate takes both the index and value of the element and uses them.
        # You check whether the value of the seat is 0, AKA free.
        if value == 0:
            # You update the location of the same element/ seat to being occupied.
            a[index] = 1
            # To stop the loop, often preferable not to need to use breaks.
            break
    print('fixed_main:', a)


def alternative_main():
    a = [1, 1, 1, 0, 0, 0, 0]
    index = 0
    # Cycle through the list until reaching the end of the list or the seat is free.
    while index < len(a) and a[index] != 0:
        index += 1

    # Check whether there were any empty seats as if there weren't index will be higher or equal to the amount of seats.
    # Thechnically it can only be equal due to the while condition but it is a better practice to cover all scenarios.
    if index >= len(a):
        print('No empty seats.')
    else:
        a[index] = 1

    print('alternative_main:', a)


if __name__ == '__main__':
    original_main()
    print()
    fixed_main()
    alternative_main()

好的,我为您的问题提供了两个修复方法,使用
for
while
循环。我个人认为,在这种情况下,while循环更可取,以避免使用
break
命令

def original_main():
    a = [1, 1, 1, 0, 0, 0, 0]
    for i in a:
        # i here will be equal to the values in the list, what happens here is that the value is being copied to a new paramater.
        if i == 0:
            a[i] = 1

    print('original_main:', a)


def fixed_main():
    a = [1, 1, 1, 0, 0, 0, 0]

    for index, value in enumerate(a):
        # What happens here is that enumerate takes both the index and value of the element and uses them.
        # You check whether the value of the seat is 0, AKA free.
        if value == 0:
            # You update the location of the same element/ seat to being occupied.
            a[index] = 1
            # To stop the loop, often preferable not to need to use breaks.
            break
    print('fixed_main:', a)


def alternative_main():
    a = [1, 1, 1, 0, 0, 0, 0]
    index = 0
    # Cycle through the list until reaching the end of the list or the seat is free.
    while index < len(a) and a[index] != 0:
        index += 1

    # Check whether there were any empty seats as if there weren't index will be higher or equal to the amount of seats.
    # Thechnically it can only be equal due to the while condition but it is a better practice to cover all scenarios.
    if index >= len(a):
        print('No empty seats.')
    else:
        a[index] = 1

    print('alternative_main:', a)


if __name__ == '__main__':
    original_main()
    print()
    fixed_main()
    alternative_main()

是的,我对python完全不熟悉,但除了这一行之外,我了解您的大多数代码。你能给我解释一下这句话吗<代码>对于我,座位在枚举中(座位):
@BaoHsu-Sure
enumerate
是一种同时提供列表中元素和索引的方法。在上面的代码段中,
i
是索引,
seat
是元素。这意味着
seats[i]==seat
。例如,尝试运行以下代码:
for i,elem in enumerate(['hello',world']):print(i,elem)
好的,我现在就知道了!但是如果我想循环这个代码呢?就像我运行这段代码一样,它会给用户分配一个座位,列表中的0会变成1,依此类推,直到列表中的所有零变成1,并且用户收到通知,所有座位都有人坐。我试着用
来做这件事,而True:
却不断地把我分配到同一个座位上。编辑:我把代码放在原来的帖子里,让它更有趣readable@BaoHsu这是因为您在循环中定义了
座位
列表。这意味着它每次都会被重置,因此每次都会分配相同的座位。只要把那一行移到
while
循环之外,我想你就在那里了。是的,我对python完全陌生,但除了这一行之外,我了解你的大部分代码。你能给我解释一下这句话吗<代码>对于我,座位在枚举中(座位):@BaoHsu-Sure
enumerate
是一种同时提供列表中元素和索引的方法。在上面的代码段中,
i
是索引,
seat
是元素。这意味着
seats[i]==seat
。例如,尝试运行以下代码:
for i,elem in enumerate(['hello',world']):print(i,elem)
好的,我现在就知道了!但是如果我想循环这个代码呢?就像我运行这段代码一样,它会给用户分配一个座位,列表中的0会变成1,依此类推,直到列表中的所有零变成1,并且用户收到通知,所有座位都有人坐。我试着使用
来完成它,而True:
却一直在赋值