如何在python中使用切片将列表合并为一个列表?
我正在尝试将3个不同的列表合并为1个列表。我可以用两个列表来完成,但当我添加第三个列表时,我开始出现一个错误,即如何在python中使用切片将列表合并为一个列表?,python,list,slice,Python,List,Slice,我正在尝试将3个不同的列表合并为1个列表。我可以用两个列表来完成,但当我添加第三个列表时,我开始出现一个错误,即 ValueError: attempt to assign sequence of size 22 to extended slice of size 17. 每个不同的列表应该交替。如果列表中没有更多的项目要包含在“结果”中,那么它应该只替换为其他两个列表。关于如何实现这一点有什么建议吗 print len(reddit_feed_dic) #22
ValueError: attempt to assign sequence of size 22 to extended slice of size 17.
每个不同的列表应该交替。如果列表中没有更多的项目要包含在“结果”中,那么它应该只替换为其他两个列表。关于如何实现这一点有什么建议吗
print len(reddit_feed_dic) #22
print len(favorites_feed_dic) #22
print len(user_videos) #6
result = [None]*(len(favorites_feed_dic)+len(reddit_feed_dic)+len(user_videos))
print len(result)
result[::3] = reddit_feed_dic
result[1::3] = favorites_feed_dic
result[2::3] = user_videos
下面是一个示例数据:
reddit_feed_dic = [r1,r2,r3, ...r22]
favorite_feed_dic = [f1,f1,f3, ...f22]
user_videos = [u1, u2 u3, ...u6]
我希望结果是:
result = [r1,f1,u1,
r2, f2, u2,
r3, f3, u3,
r4, f4, u4,
r5, f5, u5,
r6, f6, u6,
r7,f7,
r8,f8,
r9,f9,...
r22,f22]
如果要用None
填充空白:
>>> list(chain(*izip_longest('abcdef', '12345', '#!$')))
['a', '1', '#', 'b', '2', '!', 'c', '3', '$', 'd', '4', None, 'e', '5', None, 'f', None, None]
如果您根本不希望出现空项,并且假设起始列表中没有None
:
>>> filter(lambda x: x is not None, chain(*izip_longest('abcdef', '12345', '#!$')))
['a', '1', '#', 'b', '2', '!', 'c', '3', '$', 'd', '4', 'e', '5', 'f']
如果您的列表中确实包含None
,那么这就有点骗人了。同样的想法,但是我们将过滤掉一个保证不出现在原始列表中的“null”对象(因为我们刚刚创建了它!),而不是过滤掉None
最好的方法是使用文档中提供的配方:
>>> from itertools import cycle, islice
>>> def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
>>> reddit_feed = ['r1','r2','r3']
>>> favorite_feed = ['f1','f2','f3']
>>> user_videos = ['u1','u2','u3']
>>> list(roundrobin(reddit_feed, favorite_feed, user_videos))
['r1', 'f1', 'u1', 'r2', 'f2', 'u2', 'r3', 'f3', 'u3']
请举例说明您期望的输入和输出。@BrenBarn刚刚添加了我正在寻找的结果。我唯一不喜欢的是,如果他在列表中没有
None
,这就行不通了。@A.R.S.同意。我认为,我提出了一个解决方案。最好不要重新发明轮子,最有效的解决方案已经由itertools
def interleave(*iterables):
null = object()
return ifilter(lambda x: x is not null, chain(*izip_longest(*iterables, fillvalue=null)))
>>> from itertools import cycle, islice
>>> def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
>>> reddit_feed = ['r1','r2','r3']
>>> favorite_feed = ['f1','f2','f3']
>>> user_videos = ['u1','u2','u3']
>>> list(roundrobin(reddit_feed, favorite_feed, user_videos))
['r1', 'f1', 'u1', 'r2', 'f2', 'u2', 'r3', 'f3', 'u3']
python 3.2
from itertools import zip_longest
result = [j for i in zip_longest(list1,list2,list3) for j in i if j!=None]