首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在连续实例之前计数列表中的项

在连续实例之前计数列表中的项
EN

Stack Overflow用户
提问于 2014-03-20 10:30:44
回答 3查看 138关注 0票数 2

我想在用户输入的连续的零数量之前计数数组中的项目数量。

代码语言:javascript
运行
复制
['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E']

例如,如果用户输入3,则在连续的三个零的数量之前,列表中有三个项。

目前,我的代码检查了连续零的数量,并在此基础上返回--如何更改它以获得连续实例之前的项的值?

代码语言:javascript
运行
复制
LargeBlock = max(sum(1 for _ in g) for k, g in groupby(line) if k == '0')
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-20 10:39:10

代码语言:javascript
运行
复制
seats, wanted = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E'], 3
from itertools import groupby
for occ, grp in groupby(enumerate(seats[:-1], 1), key = lambda x: x[1]):
    if occ == '0':
        available = list(grp)
        if len(available) >= wanted:
            print([seats[-1] + str(item[0]) for item in available[:wanted]])

# ['E4', 'E5', 'E6']
票数 1
EN

Stack Overflow用户

发布于 2014-03-20 10:39:09

如果首先将列表转换为string,这要容易得多:

代码语言:javascript
运行
复制
  row = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E']
  seats = ''.join(row[:-1])

一旦以字符串形式出现,就很容易搜索到一组座位:

代码语言:javascript
运行
复制
  block = '0' * n
  location = s.find(block)

下面是单个函数中的所有代码:

代码语言:javascript
运行
复制
def search(available, required):
    'Return the first available block of seats on a given row'
    row = available[-1]
    seats = ''.join(available[:-1])
    block = '0' * required
    i = seats.find(block)
    if i == -1:
        raise ValueError('no block large enough')
    return '%s%d-%s%d' % (row, i+1, row, i+required)


if __name__ == '__main__':
    print search(['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E'], required=3)

如果您想坚持原来的itertools.groupby方法,那么您需要在循环时跟踪位置和值。这是http://docs.python.org/3/library/functions.html#enumerate的工作

代码语言:javascript
运行
复制
>>> def is_occupied(t):
        seat, occupied = t
        return occupied

>>> def seat_number(t):
        seat, occupied = t
        return seat

>>> required = 3
>>> row = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E']
>>> for occupied, groups in groupby(enumerate(row[:-1]), key=is_occupied):
        if occupied == '0':
            seats = list(map(seat_number, groups))
            if len(seats) >= required:
                print(seats[:required])

虽然groupby()可以为您工作,但是要使用一个元组流(例如枚举()生成的元组)是很棘手的。如果您的目标是清晰,最好是跳过函数式编程组合技巧,而只是看上去很正常。

下面是一种直接的、非功能性的方法:

代码语言:javascript
运行
复制
>>> cumulative_open = 0
>>> row_letter = row[-1]

>>> row = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E']
>>> required = 3

>>> row_letter = row[-1]
>>> cumulative_open = 0
>>> for i, occupied in enumerate(row[:-1], 1):
        if occupied == "1":
            cumulative_open = 0
            continue
        cumulative_open += 1
        if cumulative_open >= required:
            print('%s%d-%s%d' % (row_letter, i-required+1, row_letter, i))
            break
else:
    print("Not enough open seats")


E4-E6
票数 5
EN

Stack Overflow用户

发布于 2014-03-20 11:27:10

只是为了好玩..。一个非常基本的剧院/排经理。

代码语言:javascript
运行
复制
class Row():

    class SeatOccupiedException(Exception):
        pass

    def __init__(self, seats):
        """
        Create a theatre row with seats.

        seats ::= number of seats in the row
        """
        self.seats = [False]*seats

    def get_seats(self, group_size=1, empty=True):
        """
        Get seats from the row in chunks according to group size.
        Can get empty or non-empty seats.

        group_size ::= amount of seats needed.
        empty      ::= should the seats be empty or not?
        """
        ret = []
        current_seats = []

        for idx, seat in enumerate(self.seats, 1):
            if seat != empty:
                current_seats.append(idx)
            if len(current_seats) >= group_size:
                ret.append(current_seats[-group_size:])

        return ret

    def occupy_seats(self, seats):
        """
        Occupy some seats
        """
        for seat in seats:
            if self.seats[seat]:
                raise SeatOccupiedException()
            self.seats[seat] = True

    def vacate_seats(self, seats):
        """
        Vacate some seats
        """        
        for seat in seats:            
            self.seats[seat] = False

class Theatre():            

    class RowAlreadyExistsException(Exception):
        pass

    def __init__(self, rows=None, seats_per_row=10):
        """
        Create a theatre with rows, each row has seats.

        rows          ::= a list of the names for each row
        seats_per_row ::= number of seats in the row

        Examples:
        t = Theatre(['A', 'B', 'C']) 
        => produces a theatre with 3 rows (A,B,C) with 10 seats
        t = Theatre('ABCDEFG', 3) 
        => produces a theatre with 7 rows (A,B,C,D,E,F,G) with 3 seats
        """        
        self.rows = {}
        if rows:
            for row in rows:
                self.add_row(row, seats_per_row)

    def add_row(self, row_id, seats):
        """
        Add a row to the theatre

        row_id ::= the name of the row
        seats  ::= number of seats in the row
        """
        if row_id in self.rows:
            raise RowAlreadyExistsException()
        self.rows[row_id] = Row(seats)

    def get_available_seats(self, group_size, row_id=None):
        """
        Get all seats available for a group of a certain size
        Can specify a specific row or all of them

        group_size ::= how many available seats are needed
        row_id     ::= specify a certain row if desired
        """
        ret = {}

        def get_row(k):
            ret[k] = self.rows[k].get_seats(group_size)

        rows = [row_id] if row_id else self.rows
        for row_id in self.rows:
            get_row(row_id)

        return ret

    def occupy_seats(self, row_id, seats):
        """
        Occupy some seats
        """           
        self.rows[row_id].occupy_seats(seats)

    def vacate_seats(self, row_id, seats):
        """
        Vacate some seats
        """   
        self.rows[row_id].vacate_seats(seats)  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22530117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档