简体   繁体   English

循环 python 列表中的元素至少一次

[英]Cycle elements in a python list at least once

So I have a simple piece of code:所以我有一段简单的代码:

class Player:
    def __init__(self):
        self.score = 0

players = [Player() for _ in range(10)]

Now I want to:现在我想:

  • Iterate the players in the list indefinitely无限迭代列表中的玩家
  • Skip players with a negative score跳过得分为负的玩家
from itertools import cycle

loop = cycle(players)
loop = filter(lambda player: player.score >= 0, loop)
  • Stop the iteration when there is only one player left with a positive score当只剩下一名得分为正的玩家时停止迭代
  • Or when there are two or more players with a positive score, every one of them has been iterated at least once and each of them has an equal score或者当有两个或多个得分为正的玩家时,他们每个人都至少被迭代了一次并且每个人的得分相等

I have a problem with these conditions and I don't know how to bite them.我对这些情况有疑问,我不知道如何咬它们。

Solution 1解决方案 1

Here's a solution using mostly itertools .这是一个主要使用itertools的解决方案。

import itertools

positive_players = lambda : filter(lambda player: player.score >= 0, players)

loop = itertools.cycle(positive_players())

if len(list(positive_players())) <= 1:
    loop = positive_players()

if len(set([p.score for p in positive_players()])) <= 1:
    loop = positive_players()

Some tests:一些测试:

  • 3 players, all have score zero: stops after one loop. 3 名玩家,均得分为零:循环一圈后停止。
  • 3 players with scores [0, 42, 0]: continues forever 3 名得分为 [0, 42, 0] 的玩家:永远持续
  • 3 players with scores [-1, 42, -2]: stops after printing the one positive player.得分为 [-1, 42, -2] 的 3 名球员:在打印出一名阳性球员后停止。

Solution 2解决方案 2

Here's an alternative solution, using a generator.这是使用生成器的替代解决方案。

def loop_players(players): 
    inx = 0
    positive_count = 0
    positive_scores = set()
    while True: 
        p = players[inx]
        inx += 1
        if p.score < 0: 
            continue 
        positive_count += 1
        positive_scores.add(p.score)
        yield p 
        if inx == len(players):
            inx = 0
            if positive_count < 2: 
                break 
            if len(positive_scores) < 2:
                break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索列表中的元素是否在其他列表中至少包含一次 - Search if elements from list are contained at least once in other list 在跳过元素的同时循环列表(python) - cycle through a list while skipping elements (python) Python:在到达结尾时循环切换列表中的元素 - Python: cycle through elements in list reversing when the end is reached Python 多次从列表中删除最不常用的元素 - Python removing least frequently used elements from a list multiple times Python 检查列表中至少一半元素是否为元组的程序 - Python program that checks if at least half of the elements in a list are tuples 查找列表中最不常见的元素 - Finding least common elements in a list 如何在Python映射的值中比较列表中的元素,并检查是否至少有n个元素匹配? - How to compare elements of a list in a Python map's value, and check to see if at least n number of elements match? 如何创建具有特定长度的随机值列表,每个值在 Python 的范围内至少一次? - How to create a random value list with specific length with each value at least once within the range in Python? 从列表中随机选择至少一次 - Random choice from list at least once 检查包含值列表的df列是否存在python中每个列表子列表中的至少n个元素 - Check df column containing list of values for the presence of at least n elements from each sublist of list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM