Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在打印列表时删除空格?_Python_Python 3.x_List_Loops_Input - Fatal编程技术网

Python 如何在打印列表时删除空格?

Python 如何在打印列表时删除空格?,python,python-3.x,list,loops,input,Python,Python 3.x,List,Loops,Input,#输入:x+1 #输出:['x',''''+',''1']如果表达式的每个元素都被一个空格分割,则只需使用split()函数: def lexemize_Ari_Exp(): arithmeticExpression = input("Write an Arithmetic Expression: ") list_Of_Arit_Exp = [] for x in arithmeticExpression: list_O

#输入:x+1


#输出:['x',''''+',''1']

如果表达式的每个元素都被一个空格分割,则只需使用
split()
函数:

def lexemize_Ari_Exp():
    
    arithmeticExpression = input("Write an Arithmetic Expression: ")
    list_Of_Arit_Exp = []
  
    for x in arithmeticExpression:
        list_Of_Arit_Exp.append(x)
    print(list_Of_Arit_Exp)
        
lexemize_Ari_Exp()
输出示例:

def lexemize_Ari_Exp():
    
    arithmeticExpression = input("Write an Arithmetic Expression: ")
    list_Of_Arit_Exp = arithmeticExpression.split()
    
    print(list_Of_Arit_Exp)
        
lexemize_Ari_Exp()

您可以简单地添加如下所示的if语句:

算术表达式中x的
:
如果不是x='':
算术表达式附加列表(x)
这是因为for循环在输入字符串的每个字符上迭代,而x只表示每个迭代中的字符

Write an Arithmetic Expression: x + 1
['x', '+', '1']
使用此函数可以从列表中删除所需的元素

def remove (list, toRemove):
     for i in range (0, list.count (toRemove)):
         list.pop (list.index (toRemove))
     return list

适用于每种类型(int、float、string…)

您需要的输出是什么?只需在输入末尾添加.split()函数,然后删除其余的。
list = [0, 1, 0]
print (remove (list, 0))#Remove all the 0 from the list