Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Martin =?UTF-8?Q?Sch=C3=B6=C3=B6n?= Newsgroups: comp.lang.python Subject: Re: How to rearrange array using Python? Date: 21 Oct 2015 18:47:24 GMT Organization: @ Home Lines: 34 Message-ID: References: <1468455.P0rGZF1LBf@PointedEars.de> X-Trace: individual.net G8wVzTNmuHwe0fzKXNbiPws0vn/kkdKH2RhLHjRaOghwV7ODhl Cancel-Lock: sha1:jCJhMDXm4XndWH+s6Gj8ayD9UGc= User-Agent: slrn/pre1.0.0-18 (Linux) Xref: csiph.com comp.lang.python:97869 Den 2015-10-20 skrev Ian Kelly : >> >> Anyone into CSP willing to offer me a hint? > > I assume that your variables are the individuals and the domains of > those variables are the rooms. Based on the python-constraint docs, > your constraint could look something like this: > > from collections import Counter > > ROOM_SIZE = { > 'A': 3, > 'B': 3, > 'C': 4, > 'D': 4, > 'E': 5, > } > > def room_size_constraint(*v): > counter = Counter(v.values()) > return all(count <= ROOM_SIZE[room] > for room, count in counter.items()) > > problem.addConstraint(room_size_constraint) Bingo! Just what I needed but didn't know where to look for. Now I 'only' have to read https://docs.python.org/dev/library/collections.html#counter-objects to understand what's really going on in the code :-) Then I will try less benign examples. /Martin