gpt4 book ai didi

python - 如何从 for...in... 循环中排除列表中的 1 项

转载 作者:行者123 更新时间:2023-12-01 00:28:00 25 4
gpt4 key购买 nike

我正在编写一个函数来感染和治愈一个城市,但我总是希望 city[0] 保持感染状态。所有城市都是列表的一部分。

def sim_step(cities , p_spread , p_cure):
for city in cities:
if city[1] == True and numpy.random.rand() < p_spread:
zombify(my_world , cities[numpy.random.randint(city[3])])
if city[1] == False and numpy.random.rand() < p_cure:
cure(my_world , cities[numpy.random.randint(city[3])])

最佳答案

你可以这样做:

def sim_step(cities , p_spread , p_cure):
for city in cities[1:]:
if city[1] == True and numpy.random.rand() < p_spread:
zombify(my_world , cities[numpy.random.randint(city[3])])
if city[1] == False and numpy.random.rand() < p_cure:
cure(my_world , cities[numpy.random.randint(city[3])])

区别在于:cities[1:]。这称为切片操作。要了解更多信息,请阅读 this answer .

关于python - 如何从 for...in... 循环中排除列表中的 1 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58401475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com