0

I am having issues with formatting and understand lists and strings when it comes to classes. So I have this code here:

class User:
    def __init__(self,title):
        self.tile=tile
        self.rank={}

    def addCard(self,compID,number):
        if compID in self.cards and number > self.cards[compID]:
            self.cards[compID]=number
        elif compID not in self.cards:
            self.cards[compID]=number   

    def __str__(self):
        self.cardList = []
        for compID, number in self.cards.items():
            final = compID + "-" + str(number)
            self.cardList.append(temp)
            self.cardList.sort()
        return self.tile + ":" + " " + "Card scores:" + str(self.cardList)

so my result looks like this:

OUTPUT 1:
Cpt.Fred: Card scores: ['diamond-22', 'hearts-4', 'spades-3']
Lt.Connor: Card scores: ['diamond-43']

I am trying to make my result look like this:

OUTPUT 2:
Cpt.Fred: Card scores: [ diamond-22, hearts-4, spades-3 ]
Lt.Connor: Card scores: [ diamond-43 ]

The data is not whats important, its how to get rid of the " ' " at the beginning and the end of the results. I think it has something to do with my last def() statement but I have been trying to format it every way with no luck. Can anyone help turn the first output to look like the second output?

5
  • I think you're going to have to do some manual list parsing to get it to print how you want. I don't know how to remove those quotes from strings in lists. Commented Jul 18, 2014 at 18:08
  • possible duplicate of Remove single quotes from python list item Commented Jul 18, 2014 at 18:13
  • not duplicate: linked question asks for type casting elements of a list, this one is about str vs repr Commented Jul 18, 2014 at 18:16
  • @metaperture, it has nothing to do with casting it is how to remove single quotes from a python list item Commented Jul 18, 2014 at 18:29
  • All answers (including the accepted answer) do type casting. There's only a single line on the page about string joins. "Remove single quotes" is the name of the question but apparently everyone took it to mean "convert strs to ints". Commented Jul 18, 2014 at 18:33

3 Answers 3

3

Instead of calling str(self.cardList), you should do:

return "%s: Card scores: [%s]" % (self.title, ", ".join(self.cardList))

The problem is that str on a list calls repr (includes quotes) on the list's elements, whereas you just want the str of the elements joined by commas.

Sign up to request clarification or add additional context in comments.

Comments

0

You may need to process and print the list manually. Here's one way you could do that.

def printList(cardlist):
  printstr = "["                   # Start with [.
  for card in cardlist:
    printstr += card + ", "        "card, "
  printstr = printstr[:-2] + "]"   # Remove the last ", " before adding the last ].

1 Comment

your for loop can instead be ', '.join(cardlist)
0

The problem is in "str(self.cardList)". It prints out the list as is, meaning it puts strings into quotations to distinguished them from numbers and other objects.

def list_to_string(some_list):
    str = "["
    for i in range(len(some_list)):
        str += some_list[i]
        if i < len(some_list)-1:
            str += ", "
    str += "]"
    return str

this would parse your list into a string without the quotations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.