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


Groups > comp.lang.python > #80433

Re: dictionary issue for formatted print

From Peter Otten <__peter__@web.de>
Subject Re: dictionary issue for formatted print
Date 2014-11-09 16:04 +0100
Organization None
References <CAEi_QX=X_pRmD1Zvy1Gqvo4+Wjj_fkqZSY5Vc06SggOzDmeJDQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.15601.1415545475.18130.python-list@python.org> (permalink)

Show all headers | 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