Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80371
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'nested': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'python': 0.11; 'def': 0.12; 'bit.': 0.16; 'dimension': 0.16; 'feasible': 0.16; 'help?': 0.16; 'itertools': 0.16; 'loops': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'rule.': 0.16; 'subject:array': 0.16; 'subject:bit': 0.16; 'variants': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'fit': 0.20; 'example': 0.22; 'import': 0.22; 'rules': 0.22; 'header:User- Agent:1': 0.23; 'push': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'tried': 0.27; 'array': 0.29; 'robert': 0.30; "i'm": 0.30; 'code': 0.31; 'quite': 0.32; 'are:': 0.33; 'maybe': 0.34; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'returning': 0.36; 'possible': 0.36; 'hi,': 0.36; 'growing': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'offer': 0.62; "you'll": 0.62; 'reach': 0.63; 'soon': 0.63; 'sum': 0.64; 'results': 0.69; 'optimisation': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: generating 2D bit array variants with specific algorythm |
| Date | Fri, 07 Nov 2014 17:29:30 +0100 |
| Organization | None |
| References | <0e6787f9-88d6-423a-8410-7578fa83d61c@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Gmane-NNTP-Posting-Host | p57bd9b80.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15568.1415377809.18130.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415377809 news.xs4all.nl 2837 [2001:888:2000:d::a6]:48416 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80371 |
Show key headers only | View raw
Robert Voigtländer wrote:
> Hi,
>
> I need to generate all variants of a 2D array with variable dimension
> sizes which fit a specific rule. (up to 200*1000)
>
> The rules are:
> - Values are only 0 or 1
> - the sum of each line bust be 1
> - only valid results must be generated (generating all and only returning
> the valid results takes to long; that's what I tried already)
>
> So for a 2x2 array these would be the valid results:
>
> 10
> 01
>
> 01
> 10
>
> 10
> 10
>
> 01
> 01
>
>
> Must be possible with nested loops and a counter per line. But I don't get
> it.
>
> Can someone help?
Have a look at itertools.product(). If you need to code it in Python -- the
documentation has an example implementation.
from itertools import product
def f(n):
rows = [[0] * n for i in range(n)]
for i, row in enumerate(rows):
row[i] = 1
rows = [tuple(row) for row in rows]
return list(product(rows, repeat=n))
if __name__ == "__main__":
from pprint import pprint
pprint(f(2))
print("---")
pprint(f(3))
However, n**n is growing quite fast; you'll soon reach the limits of what is
feasible in Python. Maybe numpy has something to offer to push these limits
a bit. An optimisation would be to store the position of the 1, i. e.
01
10
00
11
for n=2. If you reorder these you get 0, 1, 2, 3. I think I'm seeing a
pattern...
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
generating 2D bit array variants with specific algorythm Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-11-07 07:39 -0800
Re: generating 2D bit array variants with specific algorythm Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-07 10:51 -0500
Re: generating 2D bit array variants with specific algorythm Robert Voigtländer <r.voigtlaender@gmail.com> - 2014-11-07 08:02 -0800
Re: generating 2D bit array variants with specific algorythm "ast" <nomail@invalid.com> - 2014-11-07 17:40 +0100
Re: generating 2D bit array variants with specific algorythm Peter Otten <__peter__@web.de> - 2014-11-07 17:29 +0100
Re: generating 2D bit array variants with specific algorythm "ast" <nomail@invalid.com> - 2014-11-07 17:57 +0100
Re: generating 2D bit array variants with specific algorythm "ast" <nomail@invalid.com> - 2014-11-07 17:58 +0100
Re:generating 2D bit array variants with specific algorythm Dave Angel <davea@davea.name> - 2014-11-07 14:34 -0500
Re: generating 2D bit array variants with specific algorythm Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-07 15:41 -0500
Re: generating 2D bit array variants with specific algorythm Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-11-08 10:13 +1300
Re: generating 2D bit array variants with specific algorythm sohcahtoa82@gmail.com - 2014-11-07 17:10 -0800
csiph-web