尝试通过交替值合并列表时,卡在扩展片上

问题描述 投票:0回答:2

我有一段代码,如下:

ascii_key = [None]*(len(ascii_username) + len(ascii_password))
ascii_key[0::2] = ascii_username
ascii_key[1::2] = ascii_password

我一直遇到以下错误:

ValueError:尝试将大小为8的序列分配给大小为6的扩展片

我认为这与我要准确地填写整个扩展列表有关,如果是这种情况,我有办法在生气之前将两个列表合并吗?

理想的过程是列表["A", "B", "C"]["D", "E", "F"]成为

["A", "D", "B", "E", "C", "F"]

我尝试遵循此帖子Pythonic way to combine two lists in an alternating fashion?的解决方案>

-抱歉,我是新手程序员。

我有一段代码,如下:ascii_key = [无] *(len(ascii_username)+ len(ascii_password))ascii_key [0 :: 2] = ascii_username ascii_key [1 :: 2] = ascii_password以下...

python list sequence slice
2个回答
0
投票

您可以使用zip方法。


0
投票

对于该任务,您可以使用itertools roundrobin中显示的recipes

© www.soinside.com 2019 - 2024. All rights reserved.