简体   繁体   中英

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

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]

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, ie:

>>> 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

Return the first item, not the list:

return [day for day in range(len(day_list)) if day_list[day] == inp][0]

But what you really want to do, is change your logic:

return day_list.index(inp)

Try this:

return [day for day in range(len(day_list)) if day_list[day] == inp][0]

However, this is not the most efficient way to achieve this. Try this instead:

day_list.index(inp)

I know it looks like I copied the other answer, I swear I didn't :)

This is a good use case for a dictionary:

>>> day_map = {day: index for index, day in enumerate(day_list)}

Use:

>>> day_map['Sunday']
0
>>> day_map['Tuesday']
2

You could also use the calendar module, which has constants defined for the weekdays (in English):

>>> import calendar
>>> def day_to_number(day):
...     return getattr(calendar, day.upper())
... 
>>> day_to_number('wednesday') 
2 # Note that 0 = 'Monday'

If you are using this, you should probably add some error handling:

>>> day_to_number('epoch')
1970

Or:

>>> day_to_number('foo')
Traceback (most recent call last):
  File "<ipython-input-22-4649e99206ae>", line 1, in <module>
    day_to_number('foo')
  File "<ipython-input-14-bf0518eb14b5>", line 2, in day_to_number
    return getattr(calendar, day.upper())
AttributeError: 'module' object has no attribute 'FOO'

This looks overly complicated. You don't need a list comprehension for this task. You can use the index() method:

>>> def day_to_number(inp):
...     return day_list.index(inp)
...
>>> day_to_number("Sunday")
0
>>> day_to_number("Monday")
1
>>> day_to_number("Saturday")
6

If you pass an invalid value, it will raise a ValueError

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM