Python中文
首页
教程
问答
标签
搜索
登录
注册
将字符串拆分为文本和数字?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>假设我有这个字符串<code>'foo1bar2xyz'</code></p> <p>我知道其中数字的索引<code>{'1': 3, '2': 7}</code></p> <p>我想形成没有数字的父字符串的子字符串。如何获取删除特定索引的字符串的子字符串?你知道吗</p> <p>在上述情况下,哪个是<code>['foo', 'bar', 'xyz']</code></p> <p>我已经试过了</p> <pre><code>def iterate_string(og_string, start, stop): if start == 0: return og_string[:stop] else: return og_string[start+1:stop] def ret_string(S): digit_dict = {c:i for i,c in enumerate(S) if c.isdigit()} digit_positions = list(digit_dict.values()) # return digit_positions substrings = [] start_index = 0 for position in digit_positions: p = iterate_string(S, start_index, position) substrings.append(p) start_index = position return substrings print ret_string('foo1bar2xyz') </code></pre> <p>但它返回<code>['foo', 'bar']</code></p> <p>相关<a href="https://stackoverflow.com/questions/13779526/python-finding-substring-within-a-list">SO</a><a href="https://stackoverflow.com/questions/430079/how-to-split-strings-into-text-and-number?noredirect=1&lq=1">questions</a></p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>试试这个:</p> <pre><code>l = re.compile("[0-9]").split(s) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
5 回答
如何合并多个xarray数据变量及其坐标?
2 回答
如何合并多个列中具有重复值的行
6 回答
如何合并多个唯一id
5 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
10 回答
如何合并多个字典并添加同一个键的值?(Python)
10 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
2 回答
如何合并多个数据帧
9 回答
如何合并多个数据帧并使用Pandas为假人添加列?
6 回答
如何合并多个数据帧并按时间戳排序
5 回答
如何合并多个数据帧的列表并用另一个lis标记每列
1 回答
如何合并多个数据框中的列
4 回答
如何合并多个文件?
9 回答
如何合并多个查询集?
9 回答
如何合并多个绘图?
4 回答
如何合并多个词典
1 回答
如何合并多个输入数据集(数据帧)?
10 回答
如何合并多条记录中拆分的文本行
4 回答
如何合并多索引列datafram
1 回答
如何合并多级(即多索引)数据帧?
3 回答