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

时间:2016-01-29 13:31:11

标签: python list

请原谅我,如果这个问题很简单,那么我就会陷入困境。我有一个代码如下:

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相关的解决方案。谢谢。

6 个答案:

答案 0 :(得分:2)

你能做到,不是吗?

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

答案 1 :(得分:1)

这有效:

from __future__ import print_function


def show_linked(lst):
    if len(lst) < 2:
        raise ValueError('List must have least 2 elements. {} found'.format(len(lst)))
    pairs = list(zip(lst, lst[1:])) 
    source, destination = pairs[0]
    print ("Source:", source)
    if len(lst) > 2:
        print ("Intermediate destination:", destination)
        print()
        for source, destination in pairs[1:-1]:
            print("Intermediate Source:", source)
            print("Intermediate destination:", destination)
            print()
        source, destination = pairs[-1]
        print("Intermediate Source:", source)
    print("Final destination:", destination)

测试:

>>> show_linked([1, 2])
Source: 1
Final destination: 2

>>> show_linked([1, 2, 3])
Source: 1
Intermediate destination: 2

Intermediate Source: 2
Final destination: 3

>>> show_linked([1, 2, 3, 4])
Source: 1
Intermediate destination: 2

Intermediate Source: 2
Intermediate destination: 3

Intermediate Source: 3
Final destination: 4

答案 2 :(得分:0)

您可以使用enumerate跟踪索引:

lst = [1, 2, 3, 4]

for i, (src, dst) in enumerate(zip(lst, lst[1:])):
    src_header = 'Source' if i == 0 else 'Intermediate source'
    dst_header = 'Final destination' if i == len(lst) - 2 else 'Intermediate destination'
    print '{} = {}'.format(src_header, src)
    print '{} = {}\n'.format(dst_header, dst)

答案 3 :(得分:0)

最简单的方法是使用枚举并检查源是否是第一个元素以及目标是否是最后一个:

>>> your_list = [1,2,3,4]
>>> for index, Source in enumerate(your_list[:-1]):
     print("{}Source: {}".format("Intermediate " if index != 0 else "",Source))
     print("{}Destination: {}\n".format("Intermediate " if index != len(your_list)-2 else "",your_list[index+1]))


Source: 1
Intermediate Destination: 2

Intermediate Source: 2
Intermediate Destination: 3

Intermediate Source: 3
Destination: 4

>>> your_list = [1,2]
>>> for index, Source in enumerate(your_list[:-1]):
     print("{}Source: {}".format("Intermediate " if index != 0 else "",Source))
     print("{}Destination: {}".format("Intermediate " if index != len(your_list)-2 else "",your_list[index+1]))


Source: 1
Destination: 2

答案 4 :(得分:0)

这可能是最直观的方式:

  def print_path(link, *state):
      print("{}Source: {}".format(state[0], link[0]))
      print("{}Destination: {}".format(state[1], link[1]))

  links = list(zip(your_list, your_list[1:]))
  print_path(links[0], "", "Intermediate ")

  for link in links[1:-1]:
    print_path(link, "Intermediate ", "Intermediate ")

  print_path(links[-1], "Intermediate ", "Final ")

答案 5 :(得分:0)

使用带枚举的枚举来计算循环中的迭代次数。

a = [1,2,3,4]
for idx,(i,j) in enumerate(zip(a,a[1:])):
    if idx == 0:
        print 'Source = ',i
    else:
        print 'Intermediate Source =',i
    if idx == len(a[1:])-1:
        print 'Final Destination =',j
    else:
        print 'Intermediate desintation =',j
    print

这将为您提供输出:

Source =  1
Intermediate desintation = 2

Intermediate Source = 2
Intermediate desintation = 3

Intermediate Source = 3
Final Destination = 4