访问列表中的列表元素

2024-04-26 11:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要访问列表中的列表元素

我尝试了以下代码:

connections ={}
for i in connections_with_names:
    connections['dog']=[dict1[i] for i in dict2[i] if i not in connections_with_names[0]['dog']]

但我回来了:

KeyError: 0

此外,connections_with_names['dog']看起来像这样:

[['furry',
  'loud',
  'shed']]

我需要for i in dict2[i] if i not in connections_with_names[0]['dog']]能够访问3个元素(furry、loud、shed)并排除这些元素。我认为这个问题与connections_with_names[0]部分有关,但不确定


Tags: 代码in元素列表forifnameswith