简体   繁体   中英

how to remove a comma from a list

I'm having difficulty trying to remove the commas in my list. I have tried using split(',') but then realised you can't because its all numbers. Any advice would be great !

class UnorderedList:

def __init__(self):
    self.head = None


def add(self, item): #add to the beginning of the list
    new_node = Node(item)
    new_node.set_next(self.head)
    self.head = new_node

def size(self):
    current = self.head
    count = 0
    while current != None:
        count = count + 1
        current = current.get_next()
    return count

def is_empty(self):
    return self.head == None

def search(self,item):
    current = self.head
    while current != None:
        if current.get_data() == item:
            return True
        else:
            current = current.get_next()
    return False

def remove(self, item):
    #Assumes the item is in the linked list
    current = self.head
    previous = None
    found = False
    while not found:
        if current.get_data() == item:
            found = True
        else:
            previous = current
            current = current.get_next()
    if previous == None:
        self.head = current.get_next() #remove the first node
    else:
        previous.set_next(current.get_next())

def __str__(self):
    my_list1 = []
    current = self.head
    while current != None:
        my_list1 += [current.get_data()]
        current = current.get_next()
    return str(my_list1)

def __iter__(self):
    return LinkedListIterator(self.head)

My answer is coming out as [8, 7, 6, 3, 5] But I want it is [8 7 6 3 5]

您也可以使用: str(my_list1).replace( ',' , ' ' )

Instead of:

str(my_list1)

Do:

"[{0}]".format(" " .join(str(x) for x in my_list1))

[1 2 3] is invalid. You can try something like this instead:

' '.join(map(str, your_list))  # => '1 2 3'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM