gpt4 book ai didi

python - 在列表中的两个元素之间创建链接

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:03 26 4
gpt4 key购买 nike

如果问题很简单,请原谅我,但我被困在了这一点上。我有一个看起来像这样的代码:

list=[1,2,3,4]

for Source, Destination in zip(list, list[1:]):
print ("Source: "),Source
print ("Destination: "),Destination

即使它为我提供了每个压缩数组的源和目标输出。那么,我怎样才能得到这样的输出:

Source = 1
Intermediate destination = 2

Intermediate Source = 2
Intermediate destination = 3

Intermediate Source = 3
Final Destination = 4

?

该代码也应该适用于不同大小的列表。不幸的是,请不要提供与 NumPy 相关的解决方案。谢谢。

最佳答案

你可以做到,不是吗?

print "Source =", list[0]
print "Intermediate Destination =", list[1], "\n"
for Source, Destination in zip(list[1:-1], list[2:-1]):
print "Intermediate Source =", Source
print "Intermediate Destination =", Destination, "\n"
print "Intermediate Source =", list[-2]
print "Final Destination =", list[-1]

Source = 1
Intermediate Destination = 2

Intermediate Source = 2
Intermediate Destination = 3

Intermediate Source = 3
Final Destination = 4

关于python - 在列表中的两个元素之间创建链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35085817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com