简体   繁体   中英

Combine two differently sized lists into a combined set

I'm trying to combine 2 lists that have different data and size into 1, and have the smaller list "wrap" around. I'm looking for a clean way to do this, eg

Input:

list1 = ['apple', 'orange', 'strawberry', 'avocado']
list2 = ['1','2','3']

Output:

[ 
    {"l1": "apple", "l2": "1"}, 
    {"l1": "orange", "l2": "2"}, 
    {"l1": "strawberry", "l2": "3"}, 
    {"l1": "avocado", "l2": "1"}
 ]

Notice that for "avocado" , we went back to "1" and wrapped around list2.

The obvious (and ugly looking) solution is to just start with an empty list, have 2 indexes in a loop, each iteration append a new list item, and the smaller one 'wraps' to the beginning when it reaches the end. Is there a clean way of doing this in Python 2.7?

You can use itertools.cycle to wrap the second list:

from itertools import cycle

lst = [dict(zip(['l1', 'l2'], tup)) for tup in zip(list1, cycle(list2))]

You could use a generator that avoids appending to an empty list:

def func(l1, l2):
    length1 = len(l1)
    length2 = len(l2)
    for idx in range(max(length1, length2)):
        # I use the modulo so the indices wrap around.
        yield {'l1': l1[idx % length1], 'l2': l2[idx % length2]}

list(func(list1, list2))
# [{'l1': 'apple', 'l2': '1'},
#  {'l1': 'orange', 'l2': '2'},
#  {'l1': 'strawberry', 'l2': '3'},
#  {'l1': 'avocado', 'l2': '1'}]

However itertools.cycle (see the other answer) is probably much better.

You can merely use enumerate for simplicity; however, a cleaner solution would involve itertools.cycle :

list1 = ['apple', 'orange', 'strawberry', 'avocado']
list2 = ['1','2','3']
new_list = [{"li":a, "l2":list2[i%len(list2)]} for i, a in enumerate(list1)]

Output:

[{'l2': '1', 'li': 'apple'}, {'l2': '2', 'li': 'orange'}, {'l2': '3', 'li': 'strawberry'}, {'l2': '1', 'li': 'avocado'}]
def processing(opt, longlist, shortlist):
  def processing_iter(inL, refL, outL):
    if refL == []:
      return outL
    elif inL == []:
      return processing_iter(shortlist, refL[1:], outL+opt(refL[0], shortlist[0]))
    else:
      return processing_iter(inL[1:], refL[1:], outL+opt(refL[0], inL[0]))
  return processing_iter(shortlist, longlist, [])


def makedict(a,b): return [{"l1":a, "l2":b}]

list1 = ['apple', 'orange', 'strawberry', 'avocado']
list2 = ['1','2','3']
print(processing(makedict,list1, list2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM