[英]How to print list of tuples without brackets and in different lines python
s =[(1, 2), (2, 3), (3, 4), (1, 3)] s =[(1, 2), (2, 3), (3, 4), (1, 3)]
Output should be:输出应该是:
1 2 1 2
2 3 2 3
3 4 3 4
1 3 1 3
#in python only #仅在python中
"WITHOUT USING FOR LOOP" “不使用for循环”
In below code在下面的代码中
ns=[[4, 4], [5, 4], [3, 3]]
for x in ns:
n=x[0]
m=x[1]
f=list(range(1,n+1))
l=list(range(2,n+1))
permut = itertools.permutations(f, 2)
permut=list(permut)
s=list(filter(lambda x: x[1]==x[0]+1 , permut))
#print(s)
m=m-len(s)
#print(m)
t=list(filter(lambda x: x[1]==x[0]+2 , permut))
#print(t)
for y in range(0,m):
s.append(t.pop(0))
print(*s, sep = "\n")
for values in s:
print(*values) # Simply separates the values so that the print statement puts a space between
This example iterates through and prints each of the tuples as space-delimited strings.此示例遍历并将每个元组打印为空格分隔的字符串。
In a more complicated but correct way, you can do:以更复杂但正确的方式,您可以执行以下操作:
for values in s:
print(" ".join([str(v) for v in values])) # Add a space between the values. Note that this requires them to be strings, hence the str(v)
You can do this by using this code snippet below:您可以使用下面的代码片段来做到这一点:
s =[(1, 2), (2, 3), (3, 4), (1, 3)]
for i in s:
print(*i)
You may try this , it could help you.你可以试试这个,它可以帮助你。
s =[(1, 2), (2, 3), (3, 4), (1, 3)]
for i in s:
print(str(i).strip('()'))
This is one way to iterate over a list without using loops这是在不使用循环的情况下迭代列表的一种方法
li = [(1, 2), (2, 3), (3, 4), (1, 3)]
res = list(map(lambda x: x, li))
print(*res, sep="\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.