问题描述
我有两个列表,可能是不相等的长度,我想能够交叉他们。我希望能够在我的交叉列表的末尾添加额外的值在更长的列表中。我有这个:pre $
def interleave(xs,ys):
a = xs
b = ys
minlength = [len(a),len(b)]
extralist = list()
((minval(minlength))):
pair = a [i],b [i]
interleave.append(pair)$ b $如果len(b)> len(a):
remaining = len(b)-len(a)
,则b flat = flatten(interleave)
c = a + b
打印(余数)
在范围内(余数,-1,-1):
extra = b [-j]
extralist.append(extra)
print(extralist )
len(a)> len(b):
remaining = len(a)-len(b)
print(余数)
,-1,-1):
extra = a [-j]
extralist.append(extra)
final = flat + extralist
return final
但是如果我测试它:
>>>交错([1,2,3],[hi,bye,True,False,33])
[1,'hi',2,'bye',3,True]
>>>
False和33不会出现。什么是我做错了?
编辑:我找到了解决办法。我向后索引:)
解决方案这是一个使用和一个:
>>> from itertools import zip_longest
>>> a = [1,2,3]
>>> b = [hi,bye,True,False,33]
>>>
[1,'hi',2,'bye',3,True,None,False,None,33]
> ;>>
[1,'hi',2,'bye',3,True,False,33]
[zip_longest(x,y,x) >>>
编辑:
如果您不想使用 itertools
,我想您可以这样做:
>>> a = [1,2,3]
>>> b = [hi,bye,True,False,33]
>>> items = sorted((a,b),key = len)
>>> out = []
>>>为枚举中的x,y(items [1]):
... out.append(y)
... try:
... out.append(items [0] [x])
...除了IndexError:
... out.extend(items [1] [x + 1:])
... break
.. 。
>>>
['hi',1,'bye',2,True,3,False,33]
>>>
但是我必须说这个方法不如我的第一个解决方案那么高效。 b
$ b $ hr
$ b
编辑2:
更改枚举
到 zip
会再次降低效率。然而,如果你必须,那么你可以这样做:
>>> a = [1,2,3]
>>> b = [hi,bye,True,False,33]
>>> out = []
>>> items = sorted((a,b),key = len)
>>> (len(items [1])),items [1]):
... out.append(y)
... try:
... out.append(items [0] [x])
...除了IndexError:
... out.extend(items [1] [x + 1:])
... break
...
>>>
['hi',1,'bye',2,True,3,False,33]
>>>
I have two lists that could be not equal in lengths and I want to be able to interleave them. I want to be able to append the extra values in the longer list at the end of my interleaved list.I have this:
def interleave(xs,ys):
a=xs
b=ys
minlength=[len(a),len(b)]
extralist= list()
interleave= list()
for i in range((minval(minlength))):
pair=a[i],b[i]
interleave.append(pair)
flat=flatten(interleave)
c=a+b
if len(b)>len(a):
remainder=len(b)-len(a)
print(remainder)
for j in range(remainder,-1,-1):
extra=b[-j]
extralist.append(extra)
print(extralist)
if len(a)>len(b):
remainder=len(a)-len(b)
print(remainder)
for j in range(remainder,-1,-1):
extra=a[-j]
extralist.append(extra)
final=flat+extralist
return final
but if I test it:
>>> interleave([1,2,3], ["hi", "bye",True, False, 33])
[1, 'hi', 2, 'bye', 3, True]
>>>
The False and 33 don't appear. What is it that Im doing wrong?
EDIT: I found the solution to it. I indexed backwards :)
解决方案 Here is a solution using itertools.zip_longest
and a list comprehension:
>>> from itertools import zip_longest
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> [y for x in zip_longest(a, b) for y in x]
[1, 'hi', 2, 'bye', 3, True, None, False, None, 33]
>>> [y for x in zip_longest(a, b) for y in x if y is not None]
[1, 'hi', 2, 'bye', 3, True, False, 33]
>>>
Edit:
If you don't want to use itertools
, I guess you could do:
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> items = sorted((a,b), key=len)
>>> out = []
>>> for x,y in enumerate(items[1]):
... out.append(y)
... try:
... out.append(items[0][x])
... except IndexError:
... out.extend(items[1][x+1:])
... break
...
>>> out
['hi', 1, 'bye', 2, True, 3, False, 33]
>>>
But I must say this method isn't as efficient as my first solution.
Edit 2:
Changing enumerate
to zip
reduces the efficiency again. However, if you must, then you can do this:
>>> a = [1, 2, 3]
>>> b = ["hi", "bye", True, False, 33]
>>> out = []
>>> items = sorted((a,b), key=len)
>>> for x,y in zip(range(len(items[1])), items[1]):
... out.append(y)
... try:
... out.append(items[0][x])
... except IndexError:
... out.extend(items[1][x+1:])
... break
...
>>> out
['hi', 1, 'bye', 2, True, 3, False, 33]
>>>
这篇关于如何交叉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!