【问题标题】:How to get a sublist of a list between two words如何在两个单词之间获取列表的子列表
【发布时间】:2015-12-26 06:11:00
【问题描述】:

从这样的列表开始:

words = ['tree', 'water', 'dog', 'soap', 'bike', 'cat', 'bird']

我想获取两个指定单词之间的子列表。例如,如果我有 'water''bike' 这两个词,我想获取子列表:

words = ['water', 'dog', 'soap', 'bike']

或者如果列表是

words = ['tree', 'water', 'dog', 'soap', 'tree', 'cat', 'bird']

我输入了'tree''tree'这两个字我想得到这个子列表:

words = ['tree', 'water', 'dog', 'soap', 'tree']

我也用 C 写过这样的程序,但我目前对 Python 不是很好。 这是我的 C 版本:

struct node {
    char *key;
    struct node *next;
    struct node *prev;
};
typedef struct node node;

node *GetSublist(node *, char *, char *);
node *MakeStringList();
void PrintStringList(node *a);

node *GetSublist(node *a, char *k1, char *k2) {

    node *p = a;
    node *n1, *n2;

    while (strcmp(p->key, k1) != 0) {
        p = p->next;
        if (p == NULL) {
            return a;
        }
    }

    n1 = p;
    n2 = p->next;

    if (n1->prev != NULL) {
        while (a != n1) {
            free(a);
            a = a->next;
        }
    }
    a->prev = NULL;

    while (strcmp(n2->key, k2) != 0) {
        n2 = n2->next;
        if (n2 == NULL) {
            return a;
        }
    }

    if (n2->next != NULL) {
        while (n2->next == NULL) {
            free(n2->next);
            n2 = n2->next;
        }
        n2->next = NULL;
    }

    return a;
}

int main(){

    char *k1 = "dog";
    char *k2 = "ball";
    node *list1 = NULL;
    list1 = MakeStringList();
    PrintStringList(list1);
    list1 = GetSublist(list1, k1, k2);
    PrintStringList(list1);


return 0;
}

node *MakeStringList() {             
    node *a = NULL, *punt, *p;
    int i;
    int dim;

    printf("Number of elements: ");
    scanf("%d", &dim);

    for (i=0; i<dim; i=i+1) {
        punt = malloc( sizeof(node) );
        punt->key = (char*)malloc(30*sizeof(char));
        scanf( "%s", punt->key );
        punt->next = NULL;
        punt->prev = NULL;
        if(a == NULL) {
            a = punt;
            p = punt;
        } else {
            p->next = punt;
            punt->prev = p;
            p = punt;
        }
    }
return a;
}

void PrintStringList(node *a) {                   
    node *p = a;
    printf("\nThe list is: { ");
    while( p != NULL ) {
        if (p->next == NULL)
            printf("%s ", p->key);
        else
        printf("%s, ", p->key);
        p = p->next;

    }
    printf("}\n\n");
}

【问题讨论】:

  • 所以words[words.index('water'):words.index('bike')+1]?如果单词出现多次或以不同的顺序出现怎么办?
  • 这应该是一个相当容易的任务(使用list.index(word) 和列表切片)。您能否向我们展示您尝试过的无效操作?
  • @jonrsharpe 如果单词出现多次,这不是问题,在这种情况下,子列表从第一个单词开始并以该单词的最后一个单词结束。谢谢
  • @mgilson 我仍然不知道 .index() 方法,我从 Python 开始。感谢您的帮助
  • @Frepard 你可能会说“这不是问题”,但.index 会为你提供该值的第一个索引!

标签: python list slice


【解决方案1】:

这可以通过列表的.index() 方法和切片符号来完成。

words = ['tree', 'water', 'dog', 'soap', 'cat', 'bird']
start_index = words.index(start_word)
end_index = words.index(end_word)
sublist = words[start_index:end_index+1]

【讨论】:

  • 小心 start_index 和 end_index 作用于同一个词(如树),在本例中,它们都会错误地计算为相同的值。也许 end_index = len(words) - 1 - words[::-1].index('tree') 的一些变化会是谨慎的
  • @HexedAgain 在这种情况下,我可以使用下一个单词的索引(如 cat)。对吗?
  • 好吧,我想你可以 - 如果对于任何具有两个有趣值的列表,你总是知道哪个单词跟在最后一个单词后面,那么你就可以开始了。但是,如果您不能保证这一点,那么根据我的快速示例,您可能会更好地找到反向列表中的第一个索引(这当然是非反向列表中的最后一个索引)
【解决方案2】:
def sublist_two_words(array, start_word, end_word):
    result = []
    for word in array:
        if result or word == start_word:
            result.push(word)
        if word == end_word:
            break
    return result

这样,即使没有end_word,它也会获得整个剩余列表。如果我正确地完成了你的任务。

【讨论】:

    猜你喜欢
    • 2015-02-03
    • 2020-08-01
    • 2011-10-03
    • 2014-12-26
    • 2012-11-22
    • 2011-03-28
    • 2021-05-17
    相关资源
    最近更新 更多