11

I am dealing with text strings such as the following: LN1 2DW, DN21 5BJ, DN21 5BL, ...

In Python, how can I count the number of elements between commas? Each element can be made of 6, 7, or 8 characters, and in my example there are 3 elements shown. The separator is always a comma.

I have never done anything related to text mining so this would be a start for me.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
FaCoffee
  • 7,609
  • 28
  • 99
  • 174

3 Answers3

29

You can count the number of commas:

text.count(",") + 1
# 3
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I'm curious to see a benchmark of this approach (str.count()) vs. split + len. If you have the time, feel free. :-) – Bram Vanroy Jun 20 '18 at 12:03
  • Yes I'd also like to know what's more efficient - counting commas or splitting and using len? – KMunro Nov 12 '19 at 15:19
  • 3
    @BramVanroy Done a real quick test using `ipython` and its `%timeit` built-in. Results: `text.count(',')+1`: 665 ns ± 3.06 ns per loop `len(text.split(','))`: 3.59 µs ± 19.3 ns per loop Tested data was a list of 91 elements. – Skillmon Jan 23 '20 at 13:42
  • @KMunro see my earlier comment. – Skillmon Jan 23 '20 at 13:43
13

If the comma (,) is the separator, you can simply use str.split on the string and then len(..) on the result:

text = 'LN1 2DW, DN21 5BJ, DN21 5B'
number = len(text.split(','))

You can also reuse the list of elements. For instance:

text = 'LN1 2DW, DN21 5BJ, DN21 5B'
tags = text.split(',')
number = len(tags)
#do something with the `tags`
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

Willien and Psidom already mentioned count,

I just wanted to add that in python a string is also iteratable, thus list comprehension could also be applied:

n = len([c for c in ','+text if c==','])

Or

n = sum(1 for c in ','+text if c==',')
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
  • 1
    Why would you do this? SO should provide the best answers not all possible alternatives – Chris_Rands Jan 10 '17 at 15:00
  • 1
    Feel free to vote down, if my answer is against SO policy I'm sure you could ask a moderator to delete it – Uri Goren Jan 10 '17 at 15:35
  • 1
    You're not breaking any rules, but why present worse alternatives than existing answers. There are so many possible answers, like you could include `len(list(filter(lambda x: ord(x) == 44, text)))+1` too but it really doesn't add anything – Chris_Rands Jan 10 '17 at 15:49