如何在Python中首次出现字母时拆分字符串? [英] How can I split a string at the first occurrence of a letter in Python?

查看:298
本文介绍了如何在Python中首次出现字母时拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A具有以下格式的一系列字符串.演示示例如下:

71 1 * abwhf

8 askg

*14 snbsb

00ab

我正在尝试编写一个Python 3程序,该程序将使用 for 循环遍历每个字符串,并在首次出现字母时将其拆分为一个包含两个元素的列表. >

以上字符串的输出将成为包含以下元素的列表:

71 1 *abwhf

8askg

*14snbsb

00ab

前三个示例的第一个字符串后应有一个 空格,但这仅在编辑器中显示

如何以这种方式分割字符串?

这里有两个相关的帖子:

第一个问题的第一个答案允许我在第一次出现单个字符而不是多个字符(如字母表中的所有字母)时将字符串拆分.

第二个字母允许我拆分第一个字母,但不只一次.使用此方法将导致包含许多元素的数组.

解决方案

使用re.search:

import re

strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]


def split_on_letter(s):
    match = re.compile("[^\W\d]").search(s)
    return [s[:match.start()], s[match.start():]]


for s in strs:
    print split_on_letter(s)

正则表达式[^\W\d]匹配所有字母字符.

\W匹配所有非字母数字字符,而\d匹配所有数字字符. ^集合开头的内容会反转选择,以匹配所有不匹配(非字母数字或数字)的所有内容(对应所有字母).

match搜索字符串以查找匹配表达式的第一个匹配项的索引.您可以根据匹配的位置对原始字符串进行切片,以获得两个列表.

A have a series of strings in the following format. Demonstration examples would look like this:

71 1 * abwhf

8 askg

*14 snbsb

00ab

I am attempting to write a Python 3 program that will use a for loop to cycle through each string and split it once at the first occurrence of a letter into a list with two elements.

The output for the strings above would become lists with the following elements:

71 1 * and abwhf

8and askg

*14 and snbsb

00 and ab

There is supposed to be a space after the first string of the first three examples but this only shows in the editor

How can I split the string in this way?

Two posts look of relevance here:

The first answer for the first question allows me to split a string at the first occurrence of a single character but not multiple characters (like all the letters of the alphabet).

The second allows me to split at the first letter, but not just one time. Using this would result in an array with many elements.

解决方案

Using re.search:

import re

strs = ["71 1 * abwhf", "8 askg", "*14 snbsb", "00ab"]


def split_on_letter(s):
    match = re.compile("[^\W\d]").search(s)
    return [s[:match.start()], s[match.start():]]


for s in strs:
    print split_on_letter(s)

The regex [^\W\d] matches all alphabetical characters.

\W matches all non-alphanumeric characters and \d matches all numeric characters. ^ at the beginning of the set inverts the selection to match everything that is not (non-alphanumeric or numeric), which corresponds to all letters.

match searches the string to find the index of the first occurrence of the matching expression. You can slice the original string based on the location of the match to get two lists.

这篇关于如何在Python中首次出现字母时拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆