【发布时间】:2014-03-21 21:26:00
【问题描述】:
我有一组来自 a->i 的初始变量、一个完整的变量列表、一个单独的整数值列表和一组由初始变量子集组成的列表:
a=0
b=0
...
i=0
exampleSubList_1 = [c, e, g]
fullList = [a, b, c, d, e, f, g, h, i] #The order of the list is important
otherList = [2, 4, 6, 8, 10, 12, 14, 16, 18] #The order of the list is important
我希望我的程序读取输入 exampleList_X 并在fullList中找到其对应的索引条目,并使用该索引号输出otherList中的对应值。比如……
exampleList_1[0]
#Which is c
#It should find index 2 in fullList
#Entry 2 in otherList is the value 6.
#So it should output
6
这可能吗?我愿意使用元组/字典。
为了清楚起见,这是针对使用 LED 的 Raspberry Pi noughts and crosses 游戏项目。 c、e、g对应从右上到左下的对角线的获胜条件,otherList对应的是树莓派上发出电流点亮LED的引脚。
【问题讨论】:
-
你考虑过
from collections import OrderedDict吗? `` -
@GWW,我已经尝试了各种零碎的东西,但是在尝试将 fullList 与 otherList 进行比较时,我一直卡住。
-
@Dietrich,我试过 Dict,但我没有听说过 OrderedDict。我会看看然后回来找你
-
OP:目前
exampleSubList_1是[0,0,0],所以fullList.index(exampleSubList_1[0]) == 0因为你所有的变量a到i都是0,所以没有区别它们。你是说exampleSubList_1 = ['c','e','g'],fullList = ['a','b','c', ... , 'i']吗? -
@Dietrich,我看过文档,但不是特别清楚。我是否以与 dict 相同的方式启动orderdict,并且对键/值执行相同的命令仍然有效?
标签: python list python-3.x