I like to know how to use split function to count a total from list.
For example,
workdays = {'work': '5,6,8,10,13,14,15,18,20,22,24,25,28,30'}
Output should be like this
I have worked 14 days.
I like to know how to use split function to count a total from list.
For example,
workdays = {'work': '5,6,8,10,13,14,15,18,20,22,24,25,28,30'}
Output should be like this
I have worked 14 days.
Hint: You can access a value in a dictionary by its key:
>>> workdays = {'work':'1,2,3,4'}
>>> workdays['work']
'1,2,3,4'
Second hint: You can split a string using str.split(delimiter)
like so:
>>> s = '1,2,3,4'
>>> s.split(',')
['1', '2', '3', '4']
Third hint: len()
Use str.split
with len
Ex:
workdays = {'work': '5,6,8,10,13,14,15,18,20,22,24,25,28,30'}
print(len(workdays["work"].split(",")))
Output:
14
That's not a list. You are using a dictionary with key and value. Get the value, split
on comma and find length using len
.
workdays = {'work': '5,6,8,10,13,14,15,18,20,22,24,25,28,30'}
print('I have worked {} days'.format(len(workdays['work'].split(','))))
Also, you could count the number of commas and add 1
to it to get the same result like so:
print('I have worked {} days'.format(workdays['work'].count(',')+1))
I'll do something like this:
len(wd.get('work').split(','))
measure the lenght of a list containing each day