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

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

How to convert Pascal case into a separate strings?

问题

  1. 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?

  1. cities = ["KyivKharkivDniproOdessaDonetsk"]

end result should be

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

答案1

得分: 0

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

  1. cities = "KyivKharkivDniproOdessaDonetsk"
  2. uppers = [] # 存储大写字母的索引的列表
  3. for idx, char in enumerate(cities): # 遍历所有字符和它们的索引
  4. if char.isupper():
  5. uppers.append(idx)
  6. cities_split = [] # 存储最终结果的列表
  7. for i in range(len(uppers) - 1): # 遍历我们找到的大写字母
  8. start_idx = uppers[i] # 选择下一个城市的起始位置
  9. if i == len(uppers) - 1: # 在最后一个大写字母时,只需取字符串的剩余部分
  10. cities_split.append(cities[start_idx:])
  11. else: # 切割两个大写字母之间的部分
  12. end_idx = uppers[i + 1]
  13. 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.

  1. cities = "KyivKharkivDniproOdessaDonetsk"
  2. uppers = [] # list for storing indicies of upper letters
  3. for idx, char in enumerate(cities): # loop through all characters and their indices
  4. if char.isupper():
  5. uppers.append(idx)
  6. cities_split = [] # list for storing final results
  7. for i in range(len(uppers-1)): # loop through the uppers that we found
  8. start_idx = uppers[i] # select the beginning of next city
  9. if i == len(uppers)-1: # at the last upper, just take the rest of the string
  10. cities_split.append(cities[start_idx:])
  11. else: # slice what is inbetween two uppers
  12. end_idx = uppers[i+1]
  13. cities_split.append(cities[start_idx:end_idx])

答案2

得分: -2

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

  1. 这将非常简单就像这样
  2. import re
  3. cities = ["KyivKharkivDniproOdessaDonetsk"]
  4. split_cities = re.findall(r'[A-Z][^A-Z]*', cities[0])
  5. print(split_cities)

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

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

英文:

That would be as easy as this :

  1. import re
  2. cities = ["KyivKharkivDniproOdessaDonetsk"]
  3. split_cities = re.findall(r'[A-Z][^A-Z]*', cities[0])
  4. 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:

确定