L
Luis M. Gonzalez
This could be done easier this way:
L = [('even','odd')[n%2] for n in range(8)]
L = [('even','odd')[n%2] for n in range(8)]
Luis said:This could be done easier this way:
L = [('even','odd')[n%2] for n in range(8)]
Daniel said:Let me tell you something: I'm not a one-liner coder, but sometimes It
is necesary.
For example:
I need to translate data from a DataField to Another.
def Evaluate(condition,truepart,falsepart):
if condition:
return truepart
else:
return falsepart
dOldDataFields = {}
dNewDataFields = {}
dNewDataFields = {
'CODE': dOldDataFields['CODEDATA'],
'DATE': dOldDataFields['DATE'],
'CONTACT': Evaluate(dOldDataFields['CONTACTTYPE']==2,
dOldDataFields['FIRSTCONTACT'], dOldDataFields['SECONDCONTACT'])
}
With this, I created a new dic very easy, saving in
dNewDataFields['CONTACT'] the value of dOldDataFields['FIRSTCONTACT']
or the value of dOldDataFields['SECONDCONTACT'] depending on
dOldDataFields['CONTACTTYPE']. How you do this in a practic way without
the use of one-line code? It is needed! You can't avoid it!
Fredrik said:Daniel said:Let me tell you something: I'm not a one-liner coder, but sometimes It
is necesary.
For example:
I need to translate data from a DataField to Another.
def Evaluate(condition,truepart,falsepart):
if condition:
return truepart
else:
return falsepart
dOldDataFields = {}
dNewDataFields = {}
dNewDataFields = {
'CODE': dOldDataFields['CODEDATA'],
'DATE': dOldDataFields['DATE'],
'CONTACT': Evaluate(dOldDataFields['CONTACTTYPE']==2,
dOldDataFields['FIRSTCONTACT'], dOldDataFields['SECONDCONTACT'])
}
With this, I created a new dic very easy, saving in
dNewDataFields['CONTACT'] the value of dOldDataFields['FIRSTCONTACT']
or the value of dOldDataFields['SECONDCONTACT'] depending on
dOldDataFields['CONTACTTYPE']. How you do this in a practic way without
the use of one-line code? It is needed! You can't avoid it!
if you use less verbose names, you can do the same thing in less than half
the number of characters, without a single oneliner:
def convert(old):
new = dict(
CODE=old['CODEDATA'],
DATE=old['DATE']
)
if old['CONTACTTYPE'] == 2:
new['CONTACT'] = old['FIRSTCONTACT']
else:
new['CONTACT'] = old['SECONDCONTACT']
return new
Let me tell you something: I'm not a one-liner coder, but sometimes It
is necesary.
For example:
I need to translate data from a DataField to Another.
def Evaluate(condition,truepart,falsepart):
if condition:
return truepart
else:
return falsepart
dOldDataFields = {}
dNewDataFields = {}
dNewDataFields = {
'CODE': dOldDataFields['CODEDATA'],
'DATE': dOldDataFields['DATE'],
'CONTACT': Evaluate(dOldDataFields['CONTACTTYPE']==2,
dOldDataFields['FIRSTCONTACT'], dOldDataFields['SECONDCONTACT'])
}
With this, I created a new dic very easy, saving in
dNewDataFields['CONTACT'] the value of dOldDataFields['FIRSTCONTACT']
or the value of dOldDataFields['SECONDCONTACT'] depending on
dOldDataFields['CONTACTTYPE']. How you do this in a practic way without
the use of one-line code? It is needed! You can't avoid it! Even using
a = [if_false_expr, if_true_expr][predicate] or a function, you'll
always have to use a one-line code (for this purpose, of course).
Steven D'Aprano said:dNewDataFields['CODE'] = dOldDataFields['CODEDATA']
dNewDataFields['DATE'] = dOldDataFields['DATE']
if dOldDataFields['CONTACTTYPE'] == 2:
dNewDataFields['CONTACT'] = dOldDataFields['FIRSTCONTACT']
else:
dNewDataFields['CONTACT'] = dOldDataFields['SECONDCONTACT']
There. The world didn't end.
Paul said:It gets messy for a more complicated structure:
d = {'x': x1() if x_is_old else x2(),
'y': y1() if y_is_old else y2(),
'z': z1() if z_is_old else z2() }
etc.
I don't find your code any more readable than the OP's
equivalent code:
The OPs code make one pass through the dict, your's makes
two. I do not know what effect (if any) that has in the case of
a very large dict.
Fredrik said:def convert(old):
new = dict(
CODE=old['CODEDATA'],
DATE=old['DATE']
)
if old['CONTACTTYPE'] == 2:
new['CONTACT'] = old['FIRSTCONTACT']
else:
new['CONTACT'] = old['SECONDCONTACT']
return new
I don't find your code any more readable than the OP's
equivalent code:
def convert(old):
new = {
CODE: old['CODEDATA'],
DATE: old['DATE'],
CONTACT: old['FIRSTCONTACT'] \
if old['CONTACTTYPE'] == 2 \
else old['OLDDCONTACT']
}
return new
The OPs code make one pass through the dict, your's makes
two.
I do not know what effect (if any) that has in the case of
a very large dict.
Steven said:Fredrik said:def convert(old):
new = dict(
CODE=old['CODEDATA'],
DATE=old['DATE']
)
if old['CONTACTTYPE'] == 2:
new['CONTACT'] = old['FIRSTCONTACT']
else:
new['CONTACT'] = old['SECONDCONTACT']
return new
I don't find your code any more readable than the OP's
equivalent code:
def convert(old):
new = {
CODE: old['CODEDATA'],
DATE: old['DATE'],
CONTACT: old['FIRSTCONTACT'] \
if old['CONTACTTYPE'] == 2 \
else old['OLDDCONTACT']
}
return new
The problem I have with your code is that it is too
similar to:
def convert(old):
new = {
CODE: old['CODEDATA'],
DATE: old['DATE'],
CONTACT: old['FIRSTCONTACT'] }
if old['CONTACTTYPE'] == 2:
else: old['OLDDCONTACT']
return new
Yes, the above code is broken. But it *looks* right, at
first glance,
unless you train yourself to never write
if cond: TRUE_CLAUSE
else: FALSE_CLAUSE
as a two-liner.
Regardless of whatever benefits the ternary operator is
going to have, in my opinion allowing people to write
TRUE_CLAUSE if COND else FALSE_CLAUSE will increase the
amount of poorly written, hard to read code.
Fredrik said:the OP's question was
How you do this in a practic way without
the use of one-line code ?
the OPs code contains five reads and three writes, my and
steven's versions contain four reads and three writes. the
yet-to-be-released ternary operator would remove one read
from the OPs code, which makes both alternatives equivalent.
dictionary accesses don't involve passes over the dictionary.
I was actually thinking of the effects of hardware
memory cache...
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.