Jump to content
  • Advertisement
Sign in to follow this  
glBender

[Python] string parsing, locating specific chars

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In python, how can I get the position of every character x in a string? string.find and string.index seem to find the first instance, I want a list of all of them. For example, given the string "Hello World" and the character 'l', I would want [3,4,10]. Are there any built in functions that can do this? Or a simple way?

Share this post


Link to post
Share on other sites
Advertisement
Nevermind, my god I love python! Sorry for even asking...


def contains(string,char):
list = []
for i in range(0,len(string)):
if string == char:
list = list +
return list



Share this post


Link to post
Share on other sites
A more idiomatic (and shorter!) way would be using a list comprehension:

def indices(string, char) :
return [ i for i in range(0, len(string)) if string == char ]



EDIT: Another one, just for fun:

def indices(string, char) :
return filter(lambda i: string == char, range(0, len(string)))

Share this post


Link to post
Share on other sites
I'm used to writing simple, structured, code in c++... and I have to say that both of those you posted are... I need practice.

Share this post


Link to post
Share on other sites
Different language, different idioms :)

Anyway, in C++, an explicit loop would probably be the cleanest solution, although one *might* be tempted to write something like:


#include <string>
#include <vector>

#include <boost/iterator/counting_iterator.hpp>
#include <boost/lambda/lambda.hpp>

std::vector<int> indices(std::string str, char ch)
{
using namespace boost;

std::vector<int> result;

remove_copy_if(counting_iterator<int>(0),
counting_iterator<int>(str.size()),
back_inserter(result),
lambda::var(str)[lambda::_1] != ch);

return result;
}



(Yes, it works. No, it's not pretty :P)

Share this post


Link to post
Share on other sites
Quote:

Original post by Sharlin
A more idiomatic (and shorter!) way would be using a list comprehension:
def indices(string, char) :
return [ i for i in range(0, len(string)) if string == char ]



Instead of range you can use enumerate which returns pairs of (index,value):

def indices(string, char):
return [i for i,c in enumerate(string) if c == char]



Share this post


Link to post
Share on other sites
Ooh, nice! I wondered if there was something like that. Now it really reads like pseudocode :)

Share this post


Link to post
Share on other sites
string.find and string.index both take start and end indices within which to search (they behave like slices).

Strings are also iterable in Python, so if you merely wish to iterate over the elements (letters) of the string:
for c in string:
...

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
string.find and string.index both take start and end indices within which to search (they behave like slices).

Strings are also iterable in Python, so if you merely wish to iterate over the elements (letters) of the string:
for c in string:
...


Yeah, thats what I was doing originally and why I had problems, because I wanted to get the position of each occurence of the char, and iterating just told me I had found the occurence, not where it was.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!