2

it's the second time I'm confronted to this kind of code :

    if "associé" in gender or "gérant" in gender or "président" in gender or "directeur" in gender:
    gen = "male"
elif "associée" in gender or "gérante" in gender or "présidente" in gender or "directrice" in gender:
    gen = "female"
else:
    gen = "error"

I'd like to find a more efficient way to write this code because it looks really bad.

4
  • 1
    What is the reason behind using in? What are possible values for gender? Commented Sep 16, 2016 at 16:09
  • associé for ex is not a gender. What is a gender in your code? Commented Sep 16, 2016 at 16:11
  • Because gender can be a mixt of those (ex : gender = associé gérant or gender = associé président), but the reason of the code is to be able to know if there is a "e" at the end of the word -> in french, "associé" it's a male and "associée" it's a female Commented Sep 16, 2016 at 16:23
  • 1
    Why not just check: if gender.endswith('e') ? Or do I misunderstand? Commented Sep 16, 2016 at 16:59

3 Answers 3

4

Using lists and any:

males = ["associé", "gérant", "président", "directeur"]
females = ["associée", "gérante", "présidente", "directrice"]

if any(m in gender for m in males):
    gen = "male"
elif any(m in gender for m in females):
    gen = "female"
else:
    gen = "Error"
Sign up to request clarification or add additional context in comments.

3 Comments

That's how I would have done this, but I did not know that you can use "any" ! So thank you Daniel
maybe the female check should come first, as the only difference in most of them is only a extra letter the shorted one will prove positive in the larger one "associé" in "associée" == True and I guess that is undesired
@Copperfield: you're right, but this would chance the OPs result.
4

I personally like doing this with sets. For example:

opts = ["associé", "gérant", "président", "directeur"]


if set(opts) & set(gender):
...

& is used for the set intersection operation which returns a new set with the items shared by the sets on either side of the &. This will execute the if block only if there is overlap in gender and opts. You can repeat the process for your elif as well, creating a list of the possible options and checking for overlap between that list and gender. All together, you could do something like this:

male_opts = ["associé", "gérant", "président", "directeur"]
female_opts = ["associée", "gérante", "présidente", "directrice"]

if set(male_opts) & set(gender):
    gen = "male"
elif set(female_opts) & set(gender):
    gen = "female"
else:
    gen = "error"

Also, as @Copperfield points out. You could increase efficiency even more by making the *_opt variables (and potentially even gender sets to begin with:

male_opts = {"associé", "gérant", "président", "directeur"}
female_opts = {"associée", "gérante", "présidente", "directrice"}
gender = set(gender)

if male_opts & gender:
...

Edit:

The code above assumes that gender is an iterable, but it seems from the comments that it is a string instead (e.g., 'associé gérant'. Although the accepted answer is better at this point, you could still use this solution by making gender a set of the words that make up the string:

gender = set(gender.split())

3 Comments

instead of calling set on *_opts each time, make them set from the start
looks like gender is a string, so this would not work as calling set on it will give you a set of the letters
@Copperfield thanks again. I didn't realize it was a string.
0

If you cannot have multiple of these stings in one gender string, maybe something like this?

gen = "error"
for g in ["associé", "gérant", "président", "directeur"]:
    if g in gender:
        gen = "male"
        break
for g in ["associée" "gérante", "présidente", "directrice"]:
    if g in gender:
        gen = "female"
        break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.