9

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.

Mohammad Yusuf
  • 15,118
  • 9
  • 44
  • 71
FaCoffee
  • 7,061
  • 24
  • 88
  • 162
  • 3
    count commas ( http://stackoverflow.com/a/1155647/5869805 ) and add 1 – burkay Jan 10 '17 at 14:52
  • 2
    Can anyone tell me why this is so much downvoted? I know one should normally include code and what you have tried, but come on. This is a specific, clear and simple question. It does not look to be a "solve my homework"-type question, and I find it completely fair that the OP does not know the concept words he would've needed to google this himself. I for one don't think it is a bad question. – jumps4fun Jan 10 '17 at 14:55
  • 1
    because it's such a basic question. – Jean-François Fabre Jan 10 '17 at 14:56
  • So now people are judging my knowledge. Who said that basic questions shouldn't be asked? – FaCoffee Jan 10 '17 at 14:57
  • 1
    Basic questions are not bad questions, and should not be downvoted for that reason. Take a look at the answers. There are two great ones. One who precisely answers the question super simple, and another one who uses more common methods for actually being able to do more to the collection of elements on a later stage. I am happy to see that the question went from -3 to 0 now. – jumps4fun Jan 10 '17 at 14:58
  • 5
    It doesn't have to do with the complexity of the question but with the belief of people who downvoted that you didn't even google it first. – Ma0 Jan 10 '17 at 14:59
  • 3
    To google something, one needs to know either what the mathematical / code structure concept is called, or otherwise have an idea of how to extract data. This is not always possible, or at least not as easy as some seem to think. To understand that one only needs to count commas and add one, one have to think of that approach first. To understand that you can split a string, you'd have to know about split functions. I understand perfectly well that this is not something everyone could just google. You'd need a level of contextual understanding, not everyone has. – jumps4fun Jan 10 '17 at 15:03

3 Answers3

29

You can count the number of commas:

text.count(",") + 1
# 3
Willem Van Onsem
  • 397,926
  • 29
  • 362
  • 485
Psidom
  • 195,464
  • 25
  • 298
  • 322
  • 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
  • 2
    @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
11

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
  • 397,926
  • 29
  • 362
  • 485
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
  • 12,532
  • 6
  • 50
  • 100
  • 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