Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44888
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; '16,': 0.03; '21,': 0.07; 'sys': 0.07; '22,': 0.09; 'arrays': 0.09; 'follows.': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '"in"': 0.16; '(k,': 0.16; '23,': 0.16; '24,': 0.16; 'cleaner': 0.16; 'comma': 0.16; 'elements,': 0.16; 'keyed': 0.16; 'less,': 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; 'roy': 0.16; 'separated': 0.16; 'terribly': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'fit': 0.20; '>>>': 0.22; 'example': 0.22; 'programming': 0.22; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'integer': 0.24; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'array': 0.29; "i'm": 0.30; '13,': 0.31; 'this.': 0.32; 'probably': 0.32; 'url:python': 0.33; '(i.e.': 0.33; 'could': 0.34; 'but': 0.35; 'there': 0.35; '14,': 0.36; 'keyword': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'issue': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'easy': 0.60; 'kind': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'road': 0.65; 'dear': 0.65; 'modeling': 0.65; 'smith': 0.68; 'article': 0.77; 'language!': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: formatted output |
| Date | Tue, 07 May 2013 15:28:27 +0200 |
| Organization | None |
| References | <add22437-64a4-4dfb-b6d9-28832e7698b4@googlegroups.com> <roy-8514D9.08422007052013@news.panix.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p50848b52.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.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 | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1407.1367933324.3114.python-list@python.org> (permalink) |
| Lines | 85 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1367933324 news.xs4all.nl 15957 [2001:888:2000:d::a6]:53840 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44888 |
Show key headers only | View raw
Roy Smith wrote:
> In article <add22437-64a4-4dfb-b6d9-28832e7698b4@googlegroups.com>,
> Sudheer Joseph <sjo.india@gmail.com> wrote:
>
>> Dear members,
>> I need to print few arrays in a tabular form for example
>> below array IL has 25 elements, is there an easy way to print
>> this as 5x5 comma separated table? in python
>>
>> IL=[]
>> for i in np.arange(1,bno+1):
>> IL.append(i)
>> print(IL)
>> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>> in fortran I could do it as below
>> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>> integer matrix(5,5)
>> in=0
>> do, k=1,5
>> do, l=1,5
>> in=in+1
>> matrix(k,l)=in
>> enddo
>> enddo
>> m=5
>> n=5
>> do, i=1,m
>> write(*,"(5i5)") ( matrix(i,j), j=1,n )
>> enddo
>> end
>>
>
> Excellent. My kind of programming language! See
> http://www.python.org/doc/humor/#bad-habits.
>
> Anyway, that translates, more or less, as follows.
>
> Note that I'm modeling the Fortran 2-dimensional array as a dictionary
> keyed by (k, l) tuples. That's easy an convenient, but conceptually a
> poor fit and not terribly efficient. If efficiency is an issue (i.e.
> much larger values of (k, l), you probably want to be looking at numpy.
>
> Also, "in" is a keyword in python, so I changed that to "value".
> There's probably cleaner ways to do this. I did a pretty literal
> transliteration.
>
>
> matrix = {}
> value = 0
> for k in range(1, 6):
> for l in range(1, 6):
> value += 1
> matrix[(k, l)] = value
>
> for i in range(1, 6):
> print ",".join("%5d" % matrix[(i, j)] for j in range(1, 6))
>
> This prints:
>
> 1, 2, 3, 4, 5
> 6, 7, 8, 9, 10
> 11, 12, 13, 14, 15
> 16, 17, 18, 19, 20
> 21, 22, 23, 24, 25
Or, as the OP may be on the road to numpy anyway:
>>> import numpy
>>> a = numpy.arange(1, 26).reshape(5, 5)
>>> a
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
>>> import sys
>>> numpy.savetxt(sys.stdout, a, delimiter=", ", fmt="%5d")
1, 2, 3, 4, 5
6, 7, 8, 9, 10
11, 12, 13, 14, 15
16, 17, 18, 19, 20
21, 22, 23, 24, 25
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
formatted output Sudheer Joseph <sjo.india@gmail.com> - 2013-05-07 05:00 -0700
Re: formatted output Roy Smith <roy@panix.com> - 2013-05-07 08:42 -0400
Re: formatted output Peter Otten <__peter__@web.de> - 2013-05-07 15:28 +0200
csiph-web