2021年5月1日星期六

Write a function pairs that takes a list l as an argument. It should return a new list, which contains all consecutive overlapping pairs of l

In other words, should contain all (l[i-1], l[i]) for all valid values of i, on order of ascending i. Clearly, the length of the result should be one less than the length of l (except, of course, if l was empty—then the result should also be empty, since there are no such pairs). For example, if l was equal to [1, 'a', ['x', 0], 2.0], then the result should be equal to [(1, 'a'), ('a', ['x', 0]), (['x', 0], 2.0)].Again, you should not modify the argument list in any way, and you should always return a new list. Furthermore, all pairs (i.e., tuples of length two) should contain (references to) the original elements of l.

This is what I have so far:

def pairs(l):    if len(l) < 2:       return None     pairsList = []    for i in range(1, len(l)):      pairsList.append([l[i-1], l[i]])    return pairsList  
https://stackoverflow.com/questions/67352119/write-a-function-pairs-that-takes-a-list-l-as-an-argument-it-should-return-a-ne May 02, 2021 at 08:58AM

没有评论:

发表评论