Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80433 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2014-11-09 16:04 +0100 |
| Last post | 2014-11-09 16:04 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: dictionary issue for formatted print Peter Otten <__peter__@web.de> - 2014-11-09 16:04 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-11-09 16:04 +0100 |
| Subject | Re: dictionary issue for formatted print |
| Message-ID | <mailman.15601.1415545475.18130.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web