将列表中的单词替换为每个单词的位置

时间:2016-03-09 19:51:38

标签: python

Sentence = input("type in sentence:").split()

以上将输入中的各个单词存储到列表中。但是现在如何将Sentence中的每个单词替换为该单词的位置?

2 个答案:

答案 0 :(得分:0)

使用map和index函数将字符串映射到其索引。

Sentence = str(input("Type in the sentence here:")).split(" ")
indexed_sentence = list(map(lambda x: Sentence.index(x), Sentence))

你也可以这样做:

indexed_sentence = [Sentence.index(x) for x in Sentence]

indexed_sentence = []
for x in Sentence:
    indexed_sentence.append(Sentence.index(x))

indexed_sentence = []
for x in Sentence:
    for count in range(len(Sentence)):
        if x == Sentence[count]:
            indexed_sentence.append(count + 1)
            break

答案 1 :(得分:-1)

这样做:

    $statement = $db_con->prepare("select * from student where student_id = :student_id");
    $statement->execute(array(':student_id' => $_GET['student_id']));
    $row = $statement->fetchAll(PDO::FETCH_ASSOC);