0

I have this long conditional statement

num = ['One', 'Five', 'Three', 'Four']
rating = []
for num in num:
    if  num == 'One':
        rating.append('1/5')
    elif num == 'Two':
        rating.append('2/5')
    elif num == 'Three':
        rating.append('3/5')
    elif num == 'Four':
        rating.append('4/5')
    elif num == 'Five':
        rating.append('5/5')
print(ratings)

and returns

['1/5', '5/5', '3/5', '4/5']

But I would like to improve this because I want this to be part of a line of code and this is too much to add.

I'm making some attempts with lambda functions but I have not made it yet. Thanks so much for your help!

AdN
  • 161
  • 1
  • 10
  • 1
    You could use a function, that does this. Define it somewhere and then use it in code. – Ondřej Baštař Apr 07 '21 at 10:21
  • Can you clarify what you are looking for? How do you expect this to fit into one line of code – do you want the entire code crammed into one line, or just create an abstraction that stores the logic elsewhere? Have you considered using a dictionary for the mapping from words to fractions? – MisterMiyagi Apr 07 '21 at 10:21
  • Does this answer your question? [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – MisterMiyagi Apr 07 '21 at 10:22

4 Answers4

2

Use a dictionary pairs to hold the corresponding values and then use a list comprehension to map it over nums.

pairs = {'One': '1/5', 'Two': '2/5', 'Three': '3/5', 'Four': '4/5', 'Five': '5/5'}

nums = ['One', 'Five', 'Three', 'Four']
ratings = [pairs[num] for num in nums if num in pairs]
sunnytown
  • 1,844
  • 1
  • 6
  • 13
  • Thanks sunnytown, this could be a solution. The list `num` is provided after a for loop that appends the result into the list `num`. So, I thought I can use an expression inside the loop in order to get every appended value in `num` and check if any of the conditions in the code above match and store the value "number/5" at once in the `num` list. I'll check this and the mapping proposed by @MauriceMeyer and see what happens. Thank you so much guys for your anwers. – AdN Apr 07 '21 at 11:15
2

You could use a mapping dictionary, f-strings and a list comprehension:

nums = ['One', 'Five', 'Three', 'Four']
mapping = {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}

ratings = [f"{mapping.get(num)}/5" for num in nums if num in mapping]
print(ratings)

Out:

['1/5', '5/5', '3/5', '4/5']
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
1

You can use word2number package:

pip install word2number

and do something like:

from word2number as w2n

num = ['One', 'Five', 'Three', 'Four']
rating = [f'{w2n.word_to_numfor(i.lower())}/5' for i in num]
K.Mat
  • 1,341
  • 11
  • 17
  • Thanks so much for this solution, I didn't know about the existence of the `word2number` package. – AdN Apr 07 '21 at 11:17
1

The simplest and fastest method is the following:

num = ['One', 'Five', 'Three', 'Four']
ratings = ['One', 'Two', 'Three', 'Four', 'Five']
rating = [ str(ratings.index(_num)+1)+'/5' for _num in num ]
print(rating)
['1/5', '5/5', '3/5', '4/5']
quantummind
  • 2,086
  • 1
  • 14
  • 20