问题描述
l1=[1,2,3]
l2=[4,5,6]
输出 = [1/4,2/5,3/6]
我需要输出应该包含斜线和升序。
我搜索了很多没有找到任何用斜杠打印值的解决方案,即 1/4,3/6
解决方法
a= [(f'{x}/{y}') for x,y in zip(l1,l2)]
,
一个简单的解决方案:
l1=[1,2,3]
l2=[4,5,6]
output = ""
for i in range(0,len(l1)): output += str(l1[i]) + "/" + str(l2[i]) + ","
output = list(output[:-1].split(","))
print(output)
,
您可以使用 zip() 将它们放在一起,并使用键参数 sorted() 以升序获取它们。 res 只是打印调用的格式:
l1=[3,1]
l2=[6,4]
print(*(f"{n}/{d}" for n,d in sorted(zip(l1,l2),key=lambda nd:nd[0]/nd[1])),sep=",")
1/4,2/5,3/6
顺便说一句,如果您想确保您的解决方案确实对值进行了排序,您应该使用一个尚未排序的示例