i encountered a problem in the below code
def capitalize_names(my_input):
"""fill in the function"""
return
def test_capitalize_names():
assert ["James"] == capitalize_names(["JAMES"])
assert ["Harry", "Jack"] == capitalize_names(["HArry", "jack"])
i used below code to fill in the above function
my_input = map(str.capitalize, my_input)
return my_input
it is returning ['James']
but i want it to return ["James"]
to satisfy the assert statements and i cannot use any control statements
Try this:
def capitalize_names(input):
return list(map(lambda s: s.capitalize(), input))
this is the efficient way of doing this.
The fastest way is:
def capitalize_names(inp):
return [i.capitalize() for i in inp]
def test_capitalize_names():
assert ["James"] == capitalize_names(["JAMES"])
assert ["Harry", "Jack"] == capitalize_names(["HArry", "jack"])
A more succinct way: for example:
import string
def capitalize_names(lst_str):
return map(string.capitalize, lst_str)
if __name__ == '__main__':
print capitalize_names(["HArry", "jack", "JAMES"])
# ['Harry', 'Jack', 'James']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.