我在尝试删除列表中的逗号时遇到了困难。我尝试过使用split(',')但后来意识到你不能因为它的所有数字。任何建议都会很棒!
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)
我的回答是[8,7,6,3,5] 但我希望它是[8 7 6 3 5]
答案 0 :(得分:2)
你也可以使用:str(my_list1).replace(',','')
答案 1 :(得分:1)
而不是:
str(my_list1)
执行:
"[{0}]".format(" " .join(str(x) for x in my_list1))
答案 2 :(得分:0)
[1 2 3]无效。您可以尝试这样的事情:
' '.join(map(str, your_list)) # => '1 2 3'