-6

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.

Rakesh
  • 78,594
  • 17
  • 67
  • 103
Amit Shah
  • 3
  • 2

4 Answers4

4

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

TerryA
  • 56,204
  • 11
  • 116
  • 135
2

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
Rakesh
  • 78,594
  • 17
  • 67
  • 103
2

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))
Austin
  • 25,142
  • 4
  • 21
  • 46
1

I'll do something like this:

len(wd.get('work').split(','))

measure the lenght of a list containing each day

DDS
  • 2,048
  • 13
  • 28