Python:字符串到驼峰式大小写

xzv2uavs  于 2022-12-02  发布在  Python
关注(0)|答案(5)|浏览(209)

This is a question from Codewars: Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).
The input test cases are as follows:

test.describe("Testing function to_camel_case")
test.it("Basic tests")
test.assert_equals(to_camel_case(''), '', "An empty string was provided but not returned")
test.assert_equals(to_camel_case("the_stealth_warrior"), "theStealthWarrior", "to_camel_case('the_stealth_warrior') did not return correct value")
test.assert_equals(to_camel_case("The-Stealth-Warrior"), "TheStealthWarrior", "to_camel_case('The-Stealth-Warrior') did not return correct value")
test.assert_equals(to_camel_case("A-B-C"), "ABC", "to_camel_case('A-B-C') did not return correct value")

This is what I've tried so far:

def to_camel_case(text):
    str=text
    str=str.replace(' ','')
    for i in str:
        if ( str[i] == '-'):
            str[i]=str.replace('-','')
            str[i+1].toUpperCase()
        elif ( str[i] == '_'):
            str[i]=str.replace('-','')
            str[i+1].toUpperCase()
    return str

It passes the first two tests but not the main ones :

test.assert_equals(to_camel_case("the_stealth_warrior"), "theStealthWarrior", "to_camel_case('the_stealth_warrior') did not return correct value")
    test.assert_equals(to_camel_case("The-Stealth-Warrior"), "TheStealthWarrior", "to_camel_case('The-Stealth-Warrior') did not return correct value")
    test.assert_equals(to_camel_case("A-B-C"), "ABC", "to_camel_case('A-B-C') did not return correct value")

What am I doing wrong?

w7t8yxp5

w7t8yxp51#

您可能有一个工作实现,其中有您的评论中提到的轻微错误,但我建议您:

  • 按分隔符分隔
  • 对除第一个标记以外的所有标记应用大写
  • 重新加入令牌

我的实现是:

def to_camel_case(text):
    s = text.replace("-", " ").replace("_", " ")
    s = s.split()
    if len(text) == 0:
        return text
    return s[0] + ''.join(i.capitalize() for i in s[1:])

运行测试的输出是:

>>> to_camel_case("the_stealth_warrior")
'theStealthWarrior'
>>> to_camel_case("The-Stealth-Warrior")
'TheStealthWarrior'
>>> to_camel_case("A-B-C")
'ABC'
2sbarzqh

2sbarzqh2#

from re import sub

def to_camelcase(s):
  s = sub(r"(_|-)+", " ", s).title().replace(" ", "").replace("*","")
  return ''.join([s[0].lower(), s[1:]])

print(to_camelcase('some_string_with_underscore'))
print(to_camelcase('Some string with Spaces'))
print(to_camelcase('some-string-with-dashes'))
print(to_camelcase('some string-with dashes_underscores and spaces'))
print(to_camelcase('some*string*with*asterisks'))
8i9zcol2

8i9zcol23#

我认为首先你们应该稍微改变一下语法,因为'i'是一个字符串而不是整数。

for i in str:
    if (i == '-'):
    ...
c0vxltue

c0vxltue4#

这是我简单方法

def to_camel_case(text):
    #your code herdlfldfde
    s = text.replace("-", " ").replace("_", " ")
    s = s.split()
    if len(text) == 0:
     return text
   return s[0] + ' '.join(s[1:]).title().replace(" ", "")
8aqjt8rx

8aqjt8rx5#

支持pascal/snake转换为camel的潜在解决方案/包。

# pip install camelCasing
from camelCasing import camelCasing as cc

for s in ['the_stealth_warrior', 'The-Stealth-Warrior', 'A-B-C']:
    print(cc.toCamelCase(s=s, user_acronyms=None))

theStealthWarrior
theStealthWarrior
ABC

相关问题