简体   繁体   English

如何从列表中删除逗号

[英]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. 我试过使用split(','),但后来意识到你不能,因为它的所有数字都没有。 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] 我的答案是[8、7、6、3、5],但我希望是[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. [1 2 3]无效。 You can try something like this instead: 您可以改用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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