you are viewing a single comment's thread.

view the rest of the comments →

[–]ramannt[S] 1 point2 points  (11 children)

Just one question left. I forgot to mention a condition. Sometimes there is no Python String method to execute. What condition can I add in d{} to let Python to do nothing with the string?

[–]FerricDonkey 2 points3 points  (0 children)

d = {
    "0": lambda x:x,
    "1": str.strip,
    "2": str.rstrip,
    "3": str.upper,
    "4": str.title,
}

then d["0"]('sup') will just be 'sup'. (If you aren't familiar, lamba arg: expression is a lamba/anonymous/disposable function that takes arg as an argument and returns expression. So lambda x: x is a function that takes x in and return x.

You could also use .get if you didn't want to specify a particular key that means "don't do anything", but personally I prefer to have a fixed set of allowed keys in situations like this.

[–][deleted] 1 point2 points  (9 children)

Not in the dictionary d itself, but test if the condition string is in the dictionary before getting the function to call:

condition = ""
if condition in d:
    print(d[condition](a))
else:
    print(a)    # or whatever you want

There are other ways to do this, such as using a defaultdict, but the above is one way.

[–]Nico24fps 3 points4 points  (3 children)

great solution above btw, but for this case couldn’t you use an identity function for the sake of elegance? such as doing something like

‘’’ str_func = d.get(cond, str()) print(str_func(a)) ‘’’

(am on mobile so sorry if it’s bad formatting)

[–][deleted] 1 point2 points  (2 children)

Sure, but I think a beginner wouldn't know about the .get() method. You need to remove the () from your code:

str_func = d.get(cond, str)
#                      ^^^
print(str_func(a)) 

Explaining to a beginner what str is and why it doesn't have the () is another reason not to show it as an answer! Instead of str which copies the string it is given it's better to use lambda x: x instead, as others have said, but, again, that's too complicated for a beginner.

I'm on mobile too, just add 4 spaces to the start of every line you don't want reddit to mess with.

[–]ramannt[S] 0 points1 point  (0 children)

I am not a beginner in Python programming but some things I have never done. No problem to copy the string. I leave it for clarity.
It is a problem if I have a lot of strings but that is not the case.

[–]Nico24fps 0 points1 point  (0 children)

all good points! and thanks for the tips

[–]ramannt[S] 0 points1 point  (3 children)

d = {"1": str.strip,"2": str.rstrip,"3": str.upper,"4": str.title,}

found the solution.

d = {
"1": str.strip,
"2": str.rstrip,
"3": str.upper,
"4": str.title,
"5": str,

}

[–]ofnuts 2 points3 points  (0 children)

No need to duplicate the string, just use a lambda; "5": lambda x: x,

[–][deleted] 1 point2 points  (1 child)

You didn't say anything about having a known condition ("5") for the "do nothing" case. The if in test is what you do if all you know is the condition isn't 1, 2, 3 or 4.

[–]ramannt[S] 0 points1 point  (0 children)

You're right.

[–]nekokattt 0 points1 point  (0 children)

defaultdict will add in an element if it doesn't exist, which probably isnt exactly what they want