13

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.

0

3 Answers 3

31

You can count the number of commas:

text.count(",") + 1
# 3
Sign up to request clarification or add additional context in comments.

4 Comments

I'm curious to see a benchmark of this approach (str.count()) vs. split + len. If you have the time, feel free. :-)
Yes I'd also like to know what's more efficient - counting commas or splitting and using len?
@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.
@KMunro see my earlier comment.
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`

6 Comments

Text.count (',')+1
This work indeed more efficiently, but the next step is probably iterating over the elements.
Spaces are not ignored here
@sagar: in the loop, one can use .strip etc. to simply post-process every element.
I'm curious to see a benchmark of this approach (split + len) vs. str.count(). If you have the time, feel free. :-)
|
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==',')

3 Comments

Why would you do this? SO should provide the best answers not all possible alternatives
Feel free to vote down, if my answer is against SO policy I'm sure you could ask a moderator to delete it
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.