0

Given a list like this:

sentence = ("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")

and I want to have it like this:

["Alice Cooper", "George Kendall", "Joe Montana", "Wally West", 45]

Note: The first letter of the first name and surname must be in capital letter and at the end of the list is the sum of all integers inside the tuple with all elements sorted alphabetically.

I was able to do most of this tasks with the next code:

def sortArtists(sentence):
    l = list(sentence)
    list_alpha = []
    list_digit = []

    for item in l:
        if isinstance(item, str):
            list_alpha.append(item)
        else:
            list_digit.append(item)

    add = sum(list_digit)
    arrange = sorted(list_alpha, key=str.lower)
    mystring = str(arrange).title()

    list(mystring).append(add)

    return mystring

print(sortArtists(("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")))

in theory, in the line list(mystring).append(add) should add the sum of 45 that is the total sum of the integers inside the list, but nothing happens.

All that I received is: ['Alice Cooper', 'George Kendall', 'Joe Montana', 'Wally West'] but 45 is missing at the end of the list.

If I write in the return line return mystring + str(add), I get as output:

['Alice Cooper', 'George Kendall', 'Joe Montana', 'Wally West']45

but 45 is outside the brackets []. What can I do to accomplish this?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
myString
  • 11
  • 1
  • 2
    Use a for loop. Use isinstance to determine the type of each tuple item, and use the .title method on the strings. Don't append the sum of the numbers until you've sorted the list. If you get stuck, post your code and clearly explain what's wrong. – PM 2Ring Mar 26 '17 at 04:57
  • It absolutely doesn't make a sense to have a tuple like that, though it makes a little more sense than a list like that. A tuple is a heterogeneous structure where the position of the elements matters; and list elements should be homogeneous; for example: "Name, Number" is a tuple; and "4 names" is a list. – Antti Haapala -- Слава Україні Mar 26 '17 at 05:52
  • 1
    See for example [this answer to "What's the difference between lists and tuples?"](http://stackoverflow.com/a/626871/918959) – Antti Haapala -- Слава Україні Mar 26 '17 at 05:54
  • Also your example has a typo: `"Wally Wes"` has a `t` missing. – Antti Haapala -- Слава Україні Mar 26 '17 at 05:57
  • Is there a reason that "Alice Cooper" does not have a number after his name? – Code-Apprentice Mar 26 '17 at 06:33
  • 1
    Thanks for fixing up your question. That's much better! Your current solution is close, but it's returning a string instead of a list. So get rid of `mystring = str(arrange).title()` and do the case conversion when you add the items to the list, eg `list_alpha.append(item.title())`. And instead of creating a new `arrange` list you can sort `list_alpha` in-place: `list_alpha.sort(key=str.lower)`. – PM 2Ring Mar 27 '17 at 00:03
  • thanks for your help!!! – myString Apr 16 '17 at 23:15

2 Answers2

0

This might me what you want:

sentence = ("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")

result = sorted([a.title() for a in sentence if type(a) == str],key = lambda x: x[0]) + [(sum([b for b in sentence if type(b) == int]))]
zipa
  • 27,316
  • 6
  • 40
  • 58
0

Here's your code with the alterations I suggested in the comments, plus a couple of other minor changes.

def sort_artists(sentence):
    list_alpha = []
    list_num = []

    for item in sentence:
        if isinstance(item, str):
            list_alpha.append(item.title())
        else:
            list_num.append(item)

    total = sum(list_num)
    list_alpha.sort(key=str.lower)
    list_alpha.append(total)
    return list_alpha

sentence = ("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")
result = sort_artists(sentence)
print(result)

output

['Alice Cooper', 'George Kendall', 'Joe Montana', 'Wally West', 45]
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182