3

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

  • 1
    Don't use Python built-in keywords and function names as your variable names. Change `input` to something else. Also, check out https://stackoverflow.com/questions/1801668/convert-a-python-list-with-strings-all-to-lowercase-or-uppercase and many other similar questions is SO – Mazdak Jun 23 '18 at 06:05
  • You can use `str.capitalize` instead of `lambda s: s.capitalize()`. – DYZ Jun 23 '18 at 06:07
  • The line `li = map(lambda x: '"'+x+'"', my_input)` adds quotation marks to your capitalized string. Why are you surprised that the string has the quotation marks? Remove that line. From Python's point of view, `"` and `'` serve the same purpose. – DYZ Jun 23 '18 at 06:08
  • i cannot use str.capitalize() since it is a list –  Jun 23 '18 at 06:12
  • But please read my comment. I said: use `str.capitalize` instead of `lambda s: s.capitalize()`. Just that: `my_input = map(str.capitalize, my_input)`. It makes your code 40% faster. Not using `map` (use list comprehension instead) makes it another 20% faster. – DYZ Jun 23 '18 at 06:14
  • in list of strings ["james"] is not equal to ['james'] –  Jun 23 '18 at 06:19
  • i removed that line and made it str.capitalize but now i want the code to satisfy assert the statement please help and thnk you for the suggestions –  Jun 23 '18 at 06:24
  • Did you try `["james"]==['james']`? The two lists (and strings) are exactly the same. – DYZ Jun 23 '18 at 06:26
  • i tried but it is still showing an error –  Jun 23 '18 at 06:33
  • thank you for your suggestions –  Jun 23 '18 at 06:36
  • @marshmallow, for input like `['KEn watson']`, what do you expect? `['Ken Watson']` or `['Ken watson']`. – hygull Jun 23 '18 at 07:36

3 Answers3

2

Try this:

def capitalize_names(input):
    return list(map(lambda s: s.capitalize(), input))

this is the efficient way of doing this.

Mehrdad Pedramfar
  • 9,989
  • 4
  • 33
  • 55
  • 4
    It is bad to use variable name as **input**. The statements like `m = input;` works perfect and gives ``. – hygull Jun 23 '18 at 06:50
1

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"])
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

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']
Jayhello
  • 4,975
  • 3
  • 44
  • 53