Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65274 > unrolled thread
| Started by | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| First post | 2014-02-02 09:44 -0800 |
| Last post | 2014-02-04 00:11 +0000 |
| Articles | 15 — 8 participants |
Back to article view | Back to comp.lang.python
[newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont314@gmail.com> - 2014-02-02 09:44 -0800
Re: [newbie] making rows of table with discrete values for different number systems Roy Smith <roy@panix.com> - 2014-02-02 13:07 -0500
Re: [newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 07:06 -0800
Re: [newbie] making rows of table with discrete values for different number systems Peter Otten <__peter__@web.de> - 2014-02-02 19:10 +0100
Re: [newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont314@gmail.com> - 2014-02-02 12:51 -0800
Re: [newbie] making rows of table with discrete values for different number systems Asaf Las <roegltd@gmail.com> - 2014-02-02 17:56 -0800
Re: [newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 07:05 -0800
Re: [newbie] making rows of table with discrete values for different number systems Asaf Las <roegltd@gmail.com> - 2014-02-03 07:34 -0800
Re: [newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont314@gmail.com> - 2014-02-03 11:37 -0800
Re: [newbie] making rows of table with discrete values for different number systems Asaf Las <roegltd@gmail.com> - 2014-02-03 11:50 -0800
Re: [newbie] making rows of table with discrete values for different number systems Jean Dupont <jeandupont115@gmail.com> - 2014-02-04 01:18 -0800
Re: [newbie] making rows of table with discrete values for different number systems Asaf Las <roegltd@gmail.com> - 2014-02-03 11:52 -0800
Re: [newbie] making rows of table with discrete values for different number systems Terry Reedy <tjreedy@udel.edu> - 2014-02-03 16:50 -0500
Re: [newbie] making rows of table with discrete values for different number systems Rustom Mody <rustompmody@gmail.com> - 2014-02-03 18:48 -0800
Re: [newbie] making rows of table with discrete values for different number systems Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-04 00:11 +0000
| From | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| Date | 2014-02-02 09:44 -0800 |
| Subject | [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <515e582f-ed17-4d4e-9872-f07f1fda6ed2@googlegroups.com> |
I'm looking for an efficient method to produce rows of tables like this: for base 2 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 . . . 1 1 1 1 for base 3 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 2 . . 2 2 2 2 2 2 As you can see the rows are always twice the size of the base I _don't_ need to have all rows available together in one array which would become too large for higher value number bases. It's sufficient to produce one row after the other, as I will do further data manipulation on such a row immediately. If someone here could suggest best practices to perform this kind of operations,I'd really appreciate it very much kind regards and thanks in advance jean
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-02-02 13:07 -0500 |
| Message-ID | <roy-AF6262.13073802022014@news.panix.com> |
| In reply to | #65274 |
In article <515e582f-ed17-4d4e-9872-f07f1fda6ed2@googlegroups.com>, Jean Dupont <jeandupont314@gmail.com> wrote: > I'm looking for an efficient method to produce rows of tables like this: > > for base 2 > 0 0 0 0 > 0 0 0 1 > 0 0 1 0 > 0 0 1 1 > 0 1 0 0 > . > . > . > 1 1 1 1 > > for base 3 > 0 0 0 0 0 0 > 0 0 0 0 0 1 > 0 0 0 0 0 2 > 0 0 0 0 1 0 > 0 0 0 0 1 1 > 0 0 0 0 1 2 > . > . > 2 2 2 2 2 2 This sounds like a homework problem :-) > As you can see the rows are always twice the size of the base Why? > I _don't_ need to have all rows available together in one array which would > become too large for higher value number bases. It's sufficient to produce > one row after the other, as I will do further data manipulation on such a row > immediately. What I get out of that is that you don't want to just print them, you want to have some function which returns all the generated rows in order. The way to do that is with the yield statement. Take a look at https://wiki.python.org/moin/Generators for some discussion on how that works. Actually, http://stackoverflow.com/questions/231767/ looks like an even better discussion. Does that help you any?
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| Date | 2014-02-03 07:06 -0800 |
| Message-ID | <1a76f8ae-b194-4800-a77c-bd1e95a35a1b@googlegroups.com> |
| In reply to | #65276 |
Op zondag 2 februari 2014 19:07:38 UTC+1 schreef Roy Smith: > In article <515e582f-ed17-4d4e-9872-f07f1fda6ed2@googlegroups.com>, > Jean Dupont <jeandupont314@gmail.com> wrote: > > > I'm looking for an efficient method to produce rows of tables like this: > > > > for base 2 > > 0 0 0 0 > > 0 0 0 1 > > 0 0 1 0 > > 0 0 1 1 > > 0 1 0 0 > > . > > . > > . > > 1 1 1 1 > > > > for base 3 > > 0 0 0 0 0 0 > > 0 0 0 0 0 1 > > 0 0 0 0 0 2 > > 0 0 0 0 1 0 > > 0 0 0 0 1 1 > > 0 0 0 0 1 2 > > . > > . > > 2 2 2 2 2 2 > > This sounds like a homework problem :-) > > > As you can see the rows are always twice the size of the base > > Why? > > > I _don't_ need to have all rows available together in one array which would > > become too large for higher value number bases. It's sufficient to produce > > one row after the other, as I will do further data manipulation on such a row > > immediately. > > What I get out of that is that you don't want to just print them, you > want to have some function which returns all the generated rows in > order. The way to do that is with the yield statement. Take a look at > https://wiki.python.org/moin/Generators for some discussion on how that > works. Actually, http://stackoverflow.com/questions/231767/ > looks like an even better discussion. > > Does that help you any? Thanks, I'll try to figure out what yield does kind regards, jean
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-02-02 19:10 +0100 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <mailman.6309.1391364617.18130.python-list@python.org> |
| In reply to | #65274 |
Jean Dupont wrote: > I'm looking for an efficient method to produce rows of tables like this: > > for base 2 > 0 0 0 0 > 0 0 0 1 > 0 0 1 0 > 0 0 1 1 > 0 1 0 0 > . > . > . > 1 1 1 1 > > for base 3 > 0 0 0 0 0 0 > 0 0 0 0 0 1 > 0 0 0 0 0 2 > 0 0 0 0 1 0 > 0 0 0 0 1 1 > 0 0 0 0 1 2 > . > . > 2 2 2 2 2 2 > > As you can see the rows are always twice the size of the base > I _don't_ need to have all rows available together in one array which > would become too large for higher value number bases. It's sufficient to > produce one row after the other, as I will do further data manipulation on > such a row immediately. > > If someone here could suggest best practices to perform this kind of > operations,I'd really appreciate it very much Have a look at itertools.product(): >>> import itertools >>> for row in itertools.product(range(2), repeat=4): ... print(*row) ... 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| Date | 2014-02-02 12:51 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <5757c6ca-57d8-440f-9c55-b3f723b23404@googlegroups.com> |
| In reply to | #65278 |
Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: > Jean Dupont wrote: > > > I'm looking for an efficient method to produce rows of tables like this: > > > > for base 2 > > 0 0 0 0 > > 0 0 0 1 > > 0 0 1 0 > > 0 0 1 1 > > 0 1 0 0 > > . > > . > > . > > 1 1 1 1 > > > > for base 3 > > 0 0 0 0 0 0 > > 0 0 0 0 0 1 > > 0 0 0 0 0 2 > > 0 0 0 0 1 0 > > 0 0 0 0 1 1 > > 0 0 0 0 1 2 > > . > > . > > 2 2 2 2 2 2 > > > > As you can see the rows are always twice the size of the base > > I _don't_ need to have all rows available together in one array which > > would become too large for higher value number bases. It's sufficient to > > produce one row after the other, as I will do further data manipulation on > > such a row immediately. > > > > If someone here could suggest best practices to perform this kind of > > operations,I'd really appreciate it very much > > Have a look at itertools.product(): > > >>> import itertools > >>> for row in itertools.product(range(2), repeat=4): > ... print(*row) > ... > 0 0 0 0 > 0 0 0 1 > 0 0 1 0 > 0 0 1 1 > 0 1 0 0 > 0 1 0 1 > 0 1 1 0 > 0 1 1 1 > 1 0 0 0 > 1 0 0 1 > 1 0 1 0 > 1 0 1 1 > 1 1 0 0 > 1 1 0 1 > 1 1 1 0 > 1 1 1 1 Thanks for the suggestion I'm going to look into it further kind regards, jean
[toc] | [prev] | [next] | [standalone]
| From | Asaf Las <roegltd@gmail.com> |
|---|---|
| Date | 2014-02-02 17:56 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <3a8c1dff-5b1d-4567-aaab-a81d37a563fd@googlegroups.com> |
| In reply to | #65283 |
On Sunday, February 2, 2014 10:51:15 PM UTC+2, Jean Dupont wrote: > Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: > > I'm looking for an efficient method to produce rows of tables like this: > jean you can also try to make below universal for all needed bases: m = lambda m, n: 1 if m & n else 0 k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)] print (k)
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| Date | 2014-02-03 07:05 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <12f21916-8228-4826-bb9e-7096c77b294d@googlegroups.com> |
| In reply to | #65310 |
Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las: > On Sunday, February 2, 2014 10:51:15 PM UTC+2, Jean Dupont wrote: > > Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: > > > > I'm looking for an efficient method to produce rows of tables like this: > > jean > you can also try to make below universal for all needed bases: > m = lambda m, n: 1 if m & n else 0 > k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)] > print (k) Dear Asaf, I'm not at ease with lamba-notation, could you show me how to modify your example for the base 3 case? I guess then it will be much clearer to me thanks in advance jean
[toc] | [prev] | [next] | [standalone]
| From | Asaf Las <roegltd@gmail.com> |
|---|---|
| Date | 2014-02-03 07:34 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <c0d21b77-7c44-427d-a4bf-8c4de28e567a@googlegroups.com> |
| In reply to | #65337 |
On Monday, February 3, 2014 5:05:40 PM UTC+2, Jean Dupont wrote: > Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las: > > > On Sunday, February 2, 2014 10:51:15 PM UTC+2, Jean Dupont wrote: > > > Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: > > > I'm looking for an efficient method to produce rows of tables like this: > > > jean > > > you can also try to make below universal for all needed bases: > > m = lambda m, n: 1 if m & n else 0 > > k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)] > > > print (k) > > Dear Asaf, > I'm not at ease with lamba-notation, could you show me how to modify your > example for the base 3 case? I guess then it will be much clearer to me > thanks in advance > > jean I don't have to - use normal functions instead :-) /Asaf
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont314@gmail.com> |
|---|---|
| Date | 2014-02-03 11:37 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <cb2d877c-4328-4d35-a1ba-8a9904b7e8fb@googlegroups.com> |
| In reply to | #65340 |
Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las: > On Monday, February 3, 2014 5:05:40 PM UTC+2, Jean Dupont wrote: > > Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las: > > > > > On Sunday, February 2, 2014 10:51:15 PM UTC+2, Jean Dupont wrote: > > > > Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: > > > > I'm looking for an efficient method to produce rows of tables like this: > > > > jean > > > > > you can also try to make below universal for all needed bases: > > > m = lambda m, n: 1 if m & n else 0 > > > k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)] > > > > > print (k) > > > > Dear Asaf, > > I'm not at ease with lamba-notation, could you show me how to modify your > > example for the base 3 case? I guess then it will be much clearer to me > > thanks in advance > > > > jean > > I don't have to - use normal functions instead :-) Of course you don't have to, but I'm curious and learn well by examples :-(
[toc] | [prev] | [next] | [standalone]
| From | Asaf Las <roegltd@gmail.com> |
|---|---|
| Date | 2014-02-03 11:50 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <197d89ae-6a55-4c79-b808-fec3294d0bf6@googlegroups.com> |
| In reply to | #65370 |
On Monday, February 3, 2014 9:37:36 PM UTC+2, Jean Dupont wrote: > Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las: > > Of course you don't have to, but I'm curious and learn well by examples > :-( Hi Jean Don't get me wrong i did not mean to be rude (was joking) - i think if you will do it yourself that will be very good for you - you can learn a lot from that as i did not very long time ago. My apologies for inconvenience. /Asaf
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont115@gmail.com> |
|---|---|
| Date | 2014-02-04 01:18 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <5678e9f7-65a8-4b8e-b116-1ac0169650fd@googlegroups.com> |
| In reply to | #65371 |
Op maandag 3 februari 2014 20:50:04 UTC+1 schreef Asaf Las: > On Monday, February 3, 2014 9:37:36 PM UTC+2, Jean Dupont wrote: > > Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las: > > > > Of course you don't have to, but I'm curious and learn well by examples > > :-( > > Hi Jean > > Don't get me wrong i did not mean to be rude (was joking) - i > think if you will do it yourself that will be very good for > you - you can learn a lot from that as i did not very long time ago. > My apologies for inconvenience. no hard feelings, anyway I am programming it myself using normal functions, when I have finished it I'll post it, then maybe you can do it with lamba-notation, it could be interesting to compare execution times for the two approaches kind regards, jean
[toc] | [prev] | [next] | [standalone]
| From | Asaf Las <roegltd@gmail.com> |
|---|---|
| Date | 2014-02-03 11:52 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <11ba2e57-a7b9-4345-8dd8-96d9dbfedaa8@googlegroups.com> |
| In reply to | #65370 |
On Monday, February 3, 2014 9:37:36 PM UTC+2, Jean Dupont wrote: > Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las: > > Of course you don't have to, but I'm curious and learn well by examples > > :-( And making this design generic is really a good example indeed.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-02-03 16:50 -0500 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <mailman.6365.1391464230.18130.python-list@python.org> |
| In reply to | #65337 |
On 2/3/2014 10:05 AM, Jean Dupont wrote: > Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las: >> On Sunday, February 2, 2014 10:51:15 PM UTC+2, Jean Dupont wrote: >>> Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten: >>> >>> I'm looking for an efficient method to produce rows of tables like this: >>> jean >> you can also try to make below universal for all needed bases: >> m = lambda m, n: 1 if m & n else 0 name = lambda xxx is poor style because it produces a function object lacking a proper name. Reusing the function name as a local is also confusing. The above is equivalent to def m(k, n): return 1 if k & n else 0 >> k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)] >> print (k) > Dear Asaf, > I'm not at ease with lamba-notation, could you show me how to modify your > example for the base 3 case? I guess then it will be much clearer to me > > thanks in advance > jean > -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-02-03 18:48 -0800 |
| Subject | Re: [newbie] making rows of table with discrete values for different number systems |
| Message-ID | <1b65a5a4-f044-479d-8609-2a0339536ef3@googlegroups.com> |
| In reply to | #65379 |
On Tuesday, February 4, 2014 3:20:06 AM UTC+5:30, Terry Reedy wrote: > name = lambda xxx is poor style because it produces a function object > lacking a proper name. Reusing the function name as a local is also > confusing. The above is equivalent to > def m(k, n): return 1 if k & n else 0 Yeah... Well... Except that I'd put it the other way: the second -- shadowing names -- is MUCH worse than naked lambdas. Unless you're entering an obfuscated python contest :D
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-02-04 00:11 +0000 |
| Message-ID | <lcpb7d$vog$6@dont-email.me> |
| In reply to | #65274 |
On Sun, 02 Feb 2014 09:44:05 -0800, Jean Dupont wrote:
> I'm looking for an efficient method to produce rows of tables like this:
>
> for base 2 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 .
> .
> .
> 1 1 1 1
>
> for base 3 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 1 0 0 0 0 0 1 1 0
> 0 0 0 1 2 .
> .
> 2 2 2 2 2 2
>
> As you can see the rows are always twice the size of the base I _don't_
> need to have all rows available together in one array which would become
> too large for higher value number bases. It's sufficient to produce one
> row after the other, as I will do further data manipulation on such a
> row immediately.
>
> If someone here could suggest best practices to perform this kind of
> operations,I'd really appreciate it very much
>
> kind regards and thanks in advance jean
s=3
p=s*2
def b(n,x):
s=[]
while n:
s.append(str(n%x))
n=n/x
if s==[]:
return "0"
return ''.join(s[::-1])
for i in range(s**p):
r=("{:0"+str(p)+"d}").format(int(b(i,s)))
if len(r)==p:
print [int(r[a])for a in range(len(r))]
change s to 2 or 4 etc
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web