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

王林
Release: 2024-02-10 08:39:03
forward
876 people have browsed it

如何在极坐标上获取 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)
                  )
 )
Copy after login

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())
Copy after login

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)
        )
 )
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!