Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80433

Re: dictionary issue for formatted print

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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; '-*-': 0.07; 'assign': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'formatting': 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; "'lambda':": 0.16; 'formatted': 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; 'subject:issue': 0.16; 'wrote:': 0.18; 'working.': 0.19; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; 'allows': 0.31; 'style': 0.33; 'but': 0.35; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:* 10': 0.61; 'more': 0.64; 'temporary': 0.65; 'yusuf': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: dictionary issue for formatted print
Date Sun, 09 Nov 2014 16:04:20 +0100
Organization None
References <CAEi_QX=X_pRmD1Zvy1Gqvo4+Wjj_fkqZSY5Vc06SggOzDmeJDQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd9e77.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.15601.1415545475.18130.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415545475 news.xs4all.nl 2867 [2001:888:2000:d::a6]:58250
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80433

Show key headers only | View raw


Yusuf Can Bayrak wrote:

> when dictionary has one value for each key it's okey. I'm just type '%
> greek_letters' and it's working.
> 
> But how can i assign dict's values to formatted print, if it has more
> values than one.
> 
>>
>>    1. # -*- coding: utf-8 -*-
>>    2. greek_letters = {
>>    3.                     'omega': ['ω','Ω'], 'psi': ['ψ', 'Ψ'] , 'kapa':
>>    'κ', 'to': ['τ', 'Τ'], 'lambda': ['λ', 'Λ'],
>>    4.                     'ksi': ['ξ', 'Ξ'], 'delta': ['δ', 'Δ'], 'mu':
>>    ['μ'], 'sigma': ['σ', 'Σ'], 'epsilon': ['ε', 'Ε'],
>>    5.                     'gamma': ['γ', 'Γ'], 'phi': ['φ', 'Φ'],
>>    'theta': ['θ', 'Θ']
>>    6.                 }
>>    7. print 'x(%(to)s) = A * cos(%(omega)s * %(to)s + %(theta)s)' %
>>    greek_letters.values()[1]

You can build a temporary dict:

>>> greek_letters = {
... 'omega': ['ω','Ω'], 'to': ['τ', 'Τ'], 'theta': ['θ', 'Θ']}
>>> print 'x(%(to)s) = A * cos(%(omega)s * %(to)s + %(theta)s)' % {
... k: v[1] for k, v in greek_letters.items()}
x(Τ) = A * cos(Ω * Τ + Θ)

Python also offers an alternative style of formatting that allows 
subscripts:

>>> print 'x({to[0]}) = A * cos({omega[1]} * {to[0]} + {theta[1]})'.format(
... **greek_letters)
x(τ) = A * cos(Ω * τ + Θ)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: dictionary issue for formatted print Peter Otten <__peter__@web.de> - 2014-11-09 16:04 +0100

csiph-web