Home  >  Article  >  Backend Development  >  How to get the starting position of a string match in str.contains() in polar coordinates?

How to get the starting position of a string match in str.contains() in polar coordinates?

王林
王林forward
2024-02-10 08:39:03873browse

如何在极坐标上获取 str.contains() 中的字符串匹配起始位置?

Question content

I know I can use str.contains() to check if a column contains a string, for example:

import polars as pl
df = pl.dataframe({"a": ["my name is bob","my little pony, my little pony"]})
(df.with_columns(bbb = pl.col('a').str.slice(1,10000).str.contains(pl.col('a').str.slice(0,10), literal=true)
                  )
 )

What I want is the exact starting position of the race, not just a boolean like:

import re
x = re.search(r"pony","my little pony")
print(x.start(),x.end())

How can I do this?


Correct answer


You can use series.str.find()Method:

import polars as pl
df = pl.DataFrame({"a": ["my name is Bob","my little pony, my little pony"]})
df.with_columns(
    bbb=pl.col('a').str.slice(1,10000).str.find(
            pl.col('a').str.slice(0,10), literal=True)
        )
 )

The above is the detailed content of How to get the starting position of a string match in str.contains() in polar coordinates?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete