Python练习题答案: 阿拉伯字符串【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

阿拉伯字符串【难度:2级】:

答案1:

import re

def camelize(s):
    return "".join([w.capitalize() for w in re.split("\W|_", s)])

答案2:

import re

def camelize(s):
    return ''.join([w.capitalize() for w in re.split('\W|_', s)])

答案3:

import re

def camelize(s):
    return "".join(map(str.capitalize, re.split("\W|_", s)))

答案4:

import re
def camelize(s):
    return "".join(map(str.capitalize, re.findall(r"[a-zA-Z0-9]+", s)))

答案5:

import re

def camelize(s):
    return ''.join(map(str.capitalize, re.findall(r'[a-zA-Z0-9]+', s)))

答案6:

import re

def camelize(str_):
    words = re.findall(r'[a-z0-9]+', str_, flags=re.I)
    return ''.join(word.capitalize() for word in words)

答案7:

import re
def camelize(str_):
    return "".join([i.capitalize() for i in re.findall(r"[A-Za-z0-9]*",str_)])

答案8:

from string import punctuation as p; camelize = lambda s: ''.join([x.capitalize() for x in reduce(lambda a, kv: a.replace(*kv), [(x, ' ') for x in p], s).split(' ')])

答案9:

def camelize(string):
    s = ""
    for i in string.lower():
        if i in "qwertyuiopasdfghjklzxcvbnm1234567890":
            s += i
        else:
            s += " "
    s = s.split()
    lst = []
    for i in s:
        lst.append(i[0].upper() + i[1:])
    return "".join(lst)

答案10:

import re
def camelize(str_):
    pattern = '[a-zA-Z0-9]+'
    return ''.join(c.capitalize() for c in re.findall(pattern,str_))

答案11:

def camelize(st):
    lw = []
    ts = ''
    for i in st+' ':
        if i.isalnum():
            ts += i.lower()
        else:
            lw.append(ts)
            ts = ''
    return ''.join(i.capitalize() for i in lw)

答案12:

import re

def camelize(str_):
    return ''.join(map(lambda s:s[0].upper()+s[1:].lower(), re.findall('[A-Za-z0-9]+',str_)))



Python基础训练营景越Python基础训练营QQ群

Python练习题答案: 阿拉伯字符串【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战_第1张图片
欢迎各位同学加群讨论,一起学习,共同成长!

你可能感兴趣的:(python面试题库和答案,python编程练习,算法,字符串,数据类型,Python编程进阶练习题)