Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
操纵Python列表_Python_List Manipulation - Fatal编程技术网

操纵Python列表

操纵Python列表,python,list-manipulation,Python,List Manipulation,我有以下代码: import sys A=['Anne','Romeo','Flynn','Mickey'] B=['Bravo','Whiskey','Anne','Flynn','Joe', 'Marianne'] C=['2', 'Joe', 'Marshall','2'] D=['Connor', '2', 'Robert', 'Marshall', 'George', 'Franklin'] E=['2', 'Flynn', '2', 'Richard', 'Phillip'] F=

我有以下代码:

import sys

A=['Anne','Romeo','Flynn','Mickey']
B=['Bravo','Whiskey','Anne','Flynn','Joe', 'Marianne']
C=['2', 'Joe', 'Marshall','2']
D=['Connor', '2', 'Robert', 'Marshall', 'George', 'Franklin']
E=['2', 'Flynn', '2', 'Richard', 'Phillip']
F=['Rex', 'Fer', 'Dan', 'Daniel', 'Didi', 'Didier']

for name in A:
        if name in B:
                match=name
                a_index = A.index(match)
                c_element = C[a_index]
                b_index = B.index(match)
                e_element = E[b_index]
                f_element = F[b_index]
                if c_element == e_element:
                       print([match, c_element, f_element])
但我得到了以下错误:

e_element = E[b_index] 
IndexError: list index out of range
我的输出应该类似于:

Anne     2       Dan
算法应该是这样的:

A和B之间的第一个公共元素是Anne=>matches=>让我们检查列表C和E中与列表A和B相同行的元素=>“2”是列表C的对应元素,“2”是列表E的对应元素=>matches=>在Excel中打开一个新工作表=>编写A和B之间的公共元素(在我们的示例中是Anne)在Excel中=>在col=2,row=1中写入C和E之间的公共元素(在我们的示例中为2)=>从col=3,row=1中的列表F('Dan')中的列表B和E(第7行)的同一行写入元素

目前,我只想在屏幕上显示,但主要目标应该是将这些数据插入Excel,我希望脚本具有更大的可伸缩性(不要仅适用于此示例,因为我的每个列表至少包含10k个元素)

知道代码中有什么错误吗

多谢各位


Dan

可能您的代码缩进不正确:

for name in A:
    if name in B:
        match=name
        a_index = A.index(match)
        c_element = C[a_index]
        b_index = B.index(match)
        e_element = E[b_index]
        f_element = F[b_index]
        if c_element == e_element:
            print([match, c_element, f_element])

什么是
view
viewsrv
?使用调试器检查
b_索引
它要么比
len(E)
大,要么没有-但我认为没有一个会给出另一个错误消息`列表索引必须是整数或片,而不是NoneType`E只有5个元素,而b有6个元素。
B.index
为Marianne返回的索引将超出范围,因为您应该使用
try/except
块优雅地处理这些问题,并指定
except indexer
并确定要执行的操作。@pstatix:我已更正了该错误。现在代码反映了我的上一个trying@Carlos费尔南德斯:出于某种原因,该部分没有适当的压痕。代码完全一样yours@pstatix:由于某种原因,那个零件没有适当的压痕。密码和卡洛斯的一模一样s@dante你是说这个问题没有解决吗?如果是这样,我就无法复制。