Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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 - Fatal编程技术网

Python从索引值中获取丢失的条目

Python从索引值中获取丢失的条目,python,Python,我有一个列表l,l={'item':['apple','orange','banana','color':['red','orange','yellow']}。我通过l['item']找到了“banana”的索引。index('banana')。“香蕉”的指数为2。我如何通过知道特定列中的索引号在列表中找到“苹果”?l['item'][0]='apple' 解释 类型(l)=dict 类型(l['item'])==列表 l['item'][0]=='apple' l['color'][0]=='

我有一个列表l,
l={'item':['apple','orange','banana','color':['red','orange','yellow']}
。我通过
l['item']找到了“banana”的索引。index('banana')
。“香蕉”的指数为2。我如何通过知道特定列中的索引号在列表中找到“苹果”?

l['item'][0]='apple'

解释

类型(l)=dict
类型(l['item'])==列表
l['item'][0]=='apple'
l['color'][0]=='red'
打印('item:{}具有颜色:{}'。格式(l['item'][0],l['color'][0]))

根据清单中的水果信息:

fruits = {'item': ['apple', 'orange', 'banana'], 'color': ['red', 'orange', 'yellow']}
通过访问每个项目的相同索引,可以获得任何给定项目的名称和颜色。因此,要获取“苹果”您可以访问“水果['item'][0]:

>>> fruits['item'][0], fruits['color'][0]
('apple', 'red')
请注意,如果将相关部分更紧密地组合在一起,通常会更容易获得这样的相关数据。例如,如果我们把它从一个列表列表转换成一个列表呢

fruits_as_dicts = [
    {'item': item, 'color': color} 
    for item in fruits['item'] 
    for color in fruits['color']
]
现在所有关于苹果的信息都是水果,就像[0]:

>>> fruits_as_dicts[0]
{'item': 'apple', 'color': 'red'}
>>> fruits_as_dicts[0]['item']
'apple'
我明白你的问题了


要在您的列表中找到
apple
,您必须执行以下操作:
1[“项”][0]

你的意思是因为你知道“苹果”的索引是0,你可以用
l['item'][0]
打印出来吗?
l
不是一个列表,而是一个命令。。。dict对象没有“列”。现在还不清楚你在问什么,但我认为,鉴于“苹果”的索引为0,那么你需要
l['item'][0]
…pssst-它是一个“l”,而不是数字“1”<代码>l
1