在一个列表中查找元素(在i位置)并返回另一个列表的元素(在相同的i位置)

时间:2013-12-06 16:21:11

标签: list

我所拥有的是:

present_tense = ["say","make","go",'take','come','see',"know", "get", "give", "find", "think", "tell", "become", "feel", "leave"]
past_tense = ["said","made",...]

我希望程序搜索raw_input中引入的单词是否在present_tense中,如果是,则检索位于相同位置的past_tense列表中的元素。

目前这就是我所拥有的,但我想有一种巧妙的方式。

def pasttense(verb):
    if verb in present_tense [0]:           
      pastform = "said"
    elif verb in present_tense [1]:
      pastform = "made"
    elif verb in present_tense [2]:
      pastform = "went"
    elif verb in present_tense [3]:
      pastform = "took"

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

pastform = past_tense[present_tense.index(verb)]

但如果verb不在present_tense,则引发异常

因此,如果不期望例外情况,您可以在verb

中查看present_tense
if verb in present_tense:
    pastform = past_tense[present_tense.index(verb)]