Python novice grammar questions.

Asked 1 years ago, Updated 1 years ago, 81 views

if ch in digits:
            return {
                0: {k1or4: 1 if k1or4 in digits else 4 for k1or4 in digits+letters},
                1: {k1: 1 for k1 in digits},
            }[aa][ch]

I understand the Dictionary expression, but I don't know how to interpret if, for.

And return { ][ ] What is this expression?

python-2.7

2022-09-22 14:43

1 Answers

The code above can be thought of as follows.

# Create in advance without generating at the time of return -- if written.
d = {0: {}, 1: {}}

for k1or4 in digits+letters:
  if k1or4 in digits:
    d[0][k1or4] = 1
  else:
    d[0][k1or4] = 4

for k1 in digits:
  d[1][k1] = 1

if ch in digits:
  return d[aa][ch]

The expression you asked is an expression that creates the corresponding dictionary when returning and then obtains the result value according to the aa and ch variables. (Refer to the last line of the code above)

It's hard to say exactly without the front and back codes, but... First, the aa variable can have a value of 0 or 1, and the value in d[aa] dictionary is obtained according to the value of ch. One thing I'm curious about is that if you look at this code alone, the return value will always be 1 (?) When aaa is 0 or 1, ch always has a value of 1 if it exists in digits.


2022-09-22 14:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.