将帕斯卡命名法转换为单独的字符串。

huangapple go评论65阅读模式
英文:

How to convert Pascal case into a separate strings?

问题

cities = ["Kyiv", "Kharkiv", "Dnipro", "Odessa", "Donetsk"]
英文:

I have a big string in Pascal case and need to make separate strings. How can I do that?

cities = ["KyivKharkivDniproOdessaDonetsk"]

end result should be

cities = ["Kyiv", "Kharkiv", "Dnipro", "Odessa", "Donetsk"]

答案1

得分: 0

如果您不喜欢使用正则表达式(有时是一个有用的工具),可以像之前的答案之一那样,使用 isupper 方法和标准的适合初学者的控制循环来完成这个任务。

cities = "KyivKharkivDniproOdessaDonetsk"

uppers = []  # 存储大写字母的索引的列表

for idx, char in enumerate(cities):  # 遍历所有字符和它们的索引
  if char.isupper():
    uppers.append(idx)

cities_split = []  # 存储最终结果的列表
for i in range(len(uppers) - 1):  # 遍历我们找到的大写字母
  start_idx = uppers[i]  # 选择下一个城市的起始位置
  if i == len(uppers) - 1:  # 在最后一个大写字母时,只需取字符串的剩余部分
    cities_split.append(cities[start_idx:])
  else:  # 切割两个大写字母之间的部分
    end_idx = uppers[i + 1]
    cities_split.append(cities[start_idx:end_idx])

希望这有助于您的代码编写!

英文:

If you don't like using regex (which is sometimes an useful tool) like one of the previous answers, you could simply do this with isupper method and standard beginner-friendly control loops.

cities = "KyivKharkivDniproOdessaDonetsk" 

uppers = []  # list for storing indicies of upper letters

for idx, char in enumerate(cities): #  loop through all characters and their indices
  if char.isupper():
    uppers.append(idx)

cities_split = []  # list for storing final results
for i in range(len(uppers-1)):  # loop through the uppers that we found
  start_idx = uppers[i]  # select the beginning of next city
  if i == len(uppers)-1:  # at the last upper, just take the rest of the string
    cities_split.append(cities[start_idx:])
  else:  # slice what is inbetween two uppers
    end_idx = uppers[i+1]
    cities_split.append(cities[start_idx:end_idx])

答案2

得分: -2

以下是已翻译的代码部分:

这将非常简单就像这样

    import re
    
    cities = ["KyivKharkivDniproOdessaDonetsk"]
    split_cities = re.findall(r'[A-Z][^A-Z]*', cities[0])
    
    print(split_cities)

在上面的代码中,我们使用re.findall()函数来匹配和提取所有以大写字母([A-Z])开头,后跟零个或多个非大写字母([^A-Z]*)的序列。这个模式捕获了每个以帕斯卡命名法表示的单独城市,并返回一个包含各个城市名称的列表。

结果将是['Kyiv', 'Kharkiv', 'Dnipro', 'Odessa', 'Donetsk'],如所需。

英文:

That would be as easy as this :

import re

cities = ["KyivKharkivDniproOdessaDonetsk"]
split_cities = re.findall(r'[A-Z][^A-Z]*', cities[0])

print(split_cities)

In the code above, we use the re.findall() function to match and extract all sequences that start with an uppercase letter ([A-Z]) followed by zero or more non-uppercase letters ([^A-Z]*). This pattern captures each separate city in Pascal case and returns a list of the individual city names.

The result will be ['Kyiv', 'Kharkiv', 'Dnipro', 'Odessa', 'Donetsk'], as desired.

huangapple
  • 本文由 发表于 2023年6月11日 23:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定