我怎么能删除python中的brakets之间的整数之间的空格

时间:2015-10-28 19:15:44

标签: python python-2.7

我有一个问题我想在网上判断它想要我在坐标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做任何事情请帮助我 整个代码我在上面工作

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 

3 个答案:

答案 0 :(得分:1)

您可以使用format

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

你的代码应该是这样的:

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

答案 1 :(得分:1)

要在一般情况下执行此操作,请操作字符串表示。我保持这一点有点过于简单,正如最后一项所示:

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)

答案 2 :(得分:-3)

使用listcomprehension“删除”元组空格;

tuple_ = (2, 3)
tuple_ = [i[0] for i in tuple]

in function

def strip_tuple(tuple_):
   return [i[0] for i in tuple_]