如何在python中删除制动器之间整数之间的空格



我有一个问题,我想在网上提交判断,它想让我像一样在坐标x,y中打印结果

print (2,3)
(2, 3) # i want to remove this space between the , and 3 to be accepted
# i want it like that
(2,3)

我用c++做的,但我想要python。我向我的朋友们挑战python能做任何事情,请帮助我
proplem的整个代码我在它上工作

Bx,By,Dx,Dy=map(int, raw_input().split())
if Bx>Dx:
  Ax=Dx
  Ay=By
  Cx=Bx
  Cy=Dy
  print (Ax,Ay),(Bx,By),(Cx,Cy),(Dx,Dy) #i want this line to remove the comma between them to print like that (Ax,Ay) not that (Ax, Ay) and so on the line
else:
  Ax=Bx
  Ay=Dy
  Cx=Dx
  Cy=By
  print (Ax,Ay),(Dx,Dy),(Cx,Cy),(Bx,By) # this too 

您可以使用格式:

>>> print "({},{})".format(2,3)
(2,3)

你的代码应该是这样的:

print "({},{})({},{}),({},{}),({},{})".format(Ax,Ay,Bx,By,Cx,Cy,Dx,Dy)

要在一般情况下做到这一点,请操作字符串表示。正如最后一项所展示的那样,我保持了一点过于简单:

def print_stripped(item):
    item_str = item.__repr__()
    print item_str.replace(', ', ',')
tuple1 = (2, 3)
tuple2 = (2, ('a', 3), "hello")
tuple3 = (2, "this, will, lose some spaces", False)
print_stripped(tuple1)
print_stripped(tuple2)
print_stripped(tuple3)

我的空间移除有点太简单了;这是的输出

(2,3)
(2,('a',3),'hello')
(2,'this,will,lose some spaces',False)

使用列表理解"剥离"元组空白;

tuple_ = (2, 3)
tuple_ = [i[0] for i in tuple]
in function
def strip_tuple(tuple_):
   return [i[0] for i in tuple_]

最新更新