python 字符串拆分成字母_如何在Python中首次出现字母时拆分字符串?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值