在python中如何根据大写字母分隔字符串

2024-09-28 13:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试通过推特定位用户。你知道吗

我决定数一数词频。你知道吗

但是有一些词像'WeWouldwin''AtGEO' 我想把它们分开,分别数一数。你知道吗

我想知道有没有一个pythonic的方法根据大写字母来区分它们?你知道吗

所以我会把这两个词分开,比如“We”、“would”、“Win”和“At”、“GEO”。你知道吗

我尝试了以下链接中的方法:

Split a string at uppercase letters

但这将给出单个大写字母(例如,“G”、“E”、“O”而不是“GEO”)


Tags: 方法用户链接大写字母pythonicwinat区分
1条回答
网友
1楼 · 发布于 2024-09-28 13:12:14

你可以使用这个脚本

word = 'WeWouldWin'
start = 0
array = []
for pos, char in enumerate(word):
    if char.isupper() and pos !=0 and word[pos-1].islower():
        array.append(word[start:pos])
        start = pos
array.append(word[start:len(word)])
print(array)

敬礼。你知道吗

相关问题 更多 >

    热门问题