如何只大写列表中的每个字符串的标题? [英] How to capitalize only the title of each string in the list?

查看:244
本文介绍了如何只大写列表中的每个字符串的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个问题:编写一个函数,它将一个字符串列表作为参数,并返回一个列表,其中包含大写为标题的每个字符串。也就是说,如果输入参数是 [apple pie,brownies,chocolate,dulce de leche,eclairs] ,你的函数应该返回 [苹果派,布朗尼,巧克力,Dulce De Leche,小饼干]

我的程序(已更新):

我认为我的程序已经运行了!问题是当我输入: [apple pie] 它返回: ['Apple Pie']

  def字符串():
s = []
字符串=输入()).title()
List = strings.replace(''','')。replace('[','')。replace(']','')。split( ,)
List = List + s

返回列表

大写(参数):
r = []
for i in参数:
r.append(i)

返回r

def main():
y =字符串()
x = Capitalize y)
print(x)

main()

我收到一个错误 AttributeError:'list'object has no attribute'title'
请帮助!

  def字符串():

strings = input(请输入字符串列表)
List = strings.replace(''','')。replace('[','')。replace(']' '').split(,)


返回列表

大写(参数):
r = []
我在参数:
m =
for j in i.split():
m + = j [0] .upper()+ j [1:] +
r.append(m.rstrip())


return r

def main():
y = Strings()
x =大写(y)
print(x)

main()



  import re 
strings = input(请输入一个列表的字符串:)
List = [re.sub(r'^ [A-Za-z] |(?<= \ s)[A-Za-z]',lambda m:m。 ('','')。replace(']','')。split(,)。 )]
print(List)


WHOLE QUESTION: Write a function that takes as a parameter a list of strings and returns a list containing the each string capitalized as a title. That is, if the input parameter is ["apple pie", "brownies","chocolate","dulce de leche","eclairs"], your function should return ["Apple Pie", "Brownies","Chocolate","Dulce De Leche","Eclairs"].

My program(UPDATED):

I THINK I GOT MY PROGRAM RUNNING NOW! The problem is when I enter: ["apple pie"] it is returning: ['"Apple Pie"']

def Strings():
  s = []
  strings = input("Please enter a list of strings: ").title()
  List = strings.replace('"','').replace('[','').replace(']','').split(",")
  List = List + s

  return List

def Capitalize(parameter):
  r = []
  for i in parameter:
    r.append(i)

  return r

def main():
  y = Strings()
  x = Capitalize(y)
  print(x)

main()

I am getting an error AttributeError: 'list' object has no attribute 'title' Please help!

解决方案

Just iterate over the name list and then for each name, change the case of first letter only by specifying the index number of first letter. And then add the returned result with the remaining chars then finally append the new name to the already created empty list.

def Strings():

  strings = input("Please enter a list of strings: ")
  List = strings.replace('"','').replace('[','').replace(']','').split(",")


  return List

def Capitalize(parameter):
  r = []
  for i in parameter:
    m = ""
    for j in i.split():
      m += j[0].upper() + j[1:] + " "
    r.append(m.rstrip())  


  return r

def main():
  y = Strings()
  x = Capitalize(y)
  print(x)

main()

OR

import re
strings = input("Please enter a list of strings: ")
List = [re.sub(r'^[A-Za-z]|(?<=\s)[A-Za-z]', lambda m: m.group().upper(), name) for name in strings.replace('"','').replace('[','').replace(']','').split(",")]
print(List)

这篇关于如何只大写列表中的每个字符串的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆