Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

python - How to get a sublist of a list between two words

Starting from a list like this:

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

I want to get the sublist between two specified words. For example, if I have the words 'water' and 'bike' I want to get the sublist:

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

or if the list is

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

and I put the words 'tree' and 'tree' I want to get this sublist:

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

I have also written a program like this in C, but I'm not very good with Python at the moment. This is my C version:

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("
The list is: { ");
    while( p != NULL ) {
        if (p->next == NULL)
            printf("%s ", p->key);
        else
        printf("%s, ", p->key);
        p = p->next;

    }
    printf("}

");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be accomplished with the .index() method of lists, and with the slice notation..

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...