如何使列表的元组元素周围的括号在标准输出中消失
How to make the brackets surrounding tuple elements of a list disappear in stdout
我写了下面的代码:
from itertools import product
list_A = [int(x) for x in input().split()]
list_B = [int(y) for y in input().split()]
print(list(product(list_A, list_B)))
示例输入
1 2
3 4
代码输出
[(1, 3), (1, 4), (2, 3), (2, 4)]
我怎样才能让两个括号消失,而得到 (1, 3), (1, 4), (2, 3), (2, 4)
作为输出?
本质上你是在问如何打印一个列表但去掉两端的括号:
l = [(1,2), (3,4)]
print(', '.join(map(str, l)))
输出:
(1, 2), (3, 4)
你可以这样做:
print(str(list(product(list_A, list_B)))[1:-1])
我写了下面的代码:
from itertools import product
list_A = [int(x) for x in input().split()]
list_B = [int(y) for y in input().split()]
print(list(product(list_A, list_B)))
示例输入
1 2
3 4
代码输出
[(1, 3), (1, 4), (2, 3), (2, 4)]
我怎样才能让两个括号消失,而得到 (1, 3), (1, 4), (2, 3), (2, 4)
作为输出?
本质上你是在问如何打印一个列表但去掉两端的括号:
l = [(1,2), (3,4)]
print(', '.join(map(str, l)))
输出:
(1, 2), (3, 4)
你可以这样做:
print(str(list(product(list_A, list_B)))[1:-1])