My function returns a list with a single integer in it, how can I make it return only the integer?

notMarc

How do I remove the brackets from the result while keeping the function a single line of code?

day_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

def day_to_number(inp):
    return [day for day in range(len(day_list)) if day_list[day] == inp]

print day_to_number("Sunday")
print day_to_number("Monday")
print day_to_number("Tuesday")
print day_to_number("Wednesday")
print day_to_number("Thursday")
print day_to_number("Friday")
print day_to_number("Saturday")

Output:

[0]
[1]
[2]
[3]
[4]
[5]
[6]
timgeb

The list comprehension is overkill. If your list does not contain duplicates (as your sample data shows, just do)

>>> def day_to_number(inp):
...     return day_list.index(inp)
... 
>>> day_to_number("Sunday")
0

I would also advice to make the day_list an argument of the function, i.e.:

>>> def day_to_number(inp, days):
...     return days.index(inp)
... 
>>> day_to_number("Sunday", day_list)
0

Looking it up in the global name space is a bit ugly.

And to make the whole thing more efficient (list.index is O(n)) use a dictionary:

>>> days = dict(zip(day_list, range(len(day_list))))
>>> days
{'Monday': 1, 'Tuesday': 2, 'Friday': 5, 'Wednesday': 3, 'Thursday': 4, 'Sunday': 0, 'Saturday': 6}
>>>
>>> def day_to_number(inp, days):
...     return days[inp]
... 
>>> day_to_number("Sunday", days)
0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my stored procedure that returns only a single integer return code running out of heap space?

From Dev

How can I make a Collection of Integer using a List of "Person"?

From Dev

How can I make the LiveData getValue() function return the list of items from my rooms database in Android?

From Dev

How can I make the LiveData getValue() function return the list of items from my rooms database in Android?

From Dev

how can I validate a list index as an integer?

From Dev

How can I declare that a text field can only contain an integer?

From Dev

How can i append multiple columns of type integer into a single column

From Dev

How should I treat return value of command or function? As string or integer?

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

How do I display an integer with a function that only accepts a char in C?

From Dev

How can I make a integer for every name in a txt file?

From Dev

How can I convert my input String to an integer with a base conversion

From Dev

how to make a class return integer instance

From Dev

How to make pgsql driver to return integer values?

From Dev

How can I sort a List of Pair<String,Integer>?

From Dev

How can I use List<object> with Integer and Float ?in java

From Dev

How can I find the xth digit of the yst integer in a list in Python?

From Dev

How this AbstractList returns List of Integer Objects

From Dev

How can I convert an ldap object to integer in a python function?

From Dev

How to convert a list of multiple integers into a single integer?

From Dev

va_arg returns only parts of my integer

From Dev

Can single method in java returns any one of float or Integer?

From Dev

I have a generator in my Python function; how can I return a modified list?

From Dev

Smali function: Return an Integer

From Dev

Return integer in function String

From Dev

Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?

From Dev

How to set my ArrayList<integer> equal to a function?

From Dev

How do I get my num_digit function to give me the correct output and how do I get it to return any integer value in python?

Related Related

  1. 1

    Why is my stored procedure that returns only a single integer return code running out of heap space?

  2. 2

    How can I make a Collection of Integer using a List of "Person"?

  3. 3

    How can I make the LiveData getValue() function return the list of items from my rooms database in Android?

  4. 4

    How can I make the LiveData getValue() function return the list of items from my rooms database in Android?

  5. 5

    how can I validate a list index as an integer?

  6. 6

    How can I declare that a text field can only contain an integer?

  7. 7

    How can i append multiple columns of type integer into a single column

  8. 8

    How should I treat return value of command or function? As string or integer?

  9. 9

    How can ensure that a function only accepts a positive integer?

  10. 10

    How can ensure that a function only accepts a positive integer?

  11. 11

    How do I display an integer with a function that only accepts a char in C?

  12. 12

    How can I make a integer for every name in a txt file?

  13. 13

    How can I convert my input String to an integer with a base conversion

  14. 14

    how to make a class return integer instance

  15. 15

    How to make pgsql driver to return integer values?

  16. 16

    How can I sort a List of Pair<String,Integer>?

  17. 17

    How can I use List<object> with Integer and Float ?in java

  18. 18

    How can I find the xth digit of the yst integer in a list in Python?

  19. 19

    How this AbstractList returns List of Integer Objects

  20. 20

    How can I convert an ldap object to integer in a python function?

  21. 21

    How to convert a list of multiple integers into a single integer?

  22. 22

    va_arg returns only parts of my integer

  23. 23

    Can single method in java returns any one of float or Integer?

  24. 24

    I have a generator in my Python function; how can I return a modified list?

  25. 25

    Smali function: Return an Integer

  26. 26

    Return integer in function String

  27. 27

    Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?

  28. 28

    How to set my ArrayList<integer> equal to a function?

  29. 29

    How do I get my num_digit function to give me the correct output and how do I get it to return any integer value in python?

HotTag

Archive