Find (Start:End) Positions That Sublists Occur Within A List . Python
1
0
Entering edit mode
12.4 years ago

If one had a long list of numbers:

example=['130','90','150','123','133','120','160','45','67','55','34']

and sub lists within the list like

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

how would you generate a function that takes these sublists and gives you the positions that they occurred in the original string? to get the results:

results=[[0-2],[1-2],[5-8]]

I was trying something along the lines of

example=['130','90','150','123','133','120','160','45','67','55','34']

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

for p in range(len(example)):
    for lists in sub_lists:
        if lists in example:
            print p

but that was not working?

python list • 6.6k views
ADD COMMENT
4
Entering edit mode

As it stands, this is a basic Python programming question, better suited to stackoverflow.com. Is there relevance to a bioinformatics problem?

ADD REPLY
1
Entering edit mode

Something similar was closed yesterday http://biostar.stackexchange.com/questions/15328/find-4-values-in-window-size-6-that-fit-criteria-add-to-list-until-3-do-not-fi

STACKOVERFLOW is the place for such questions.

ADD REPLY
1
Entering edit mode

agreed, closed because no obvious relation to bioinformatics

ADD REPLY
2
Entering edit mode
12.4 years ago

I agree with Neil about it being basic programming question. A lot of these algorithm questions can be solved pretty easily if you just read up on the python native functions. For example, you can use the list.index(value) method to find index of a value in a list:

example=['130','90','150','123','133','120','160','45','67','55','34']
sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]
result = []
for sub in sub_lists:
    result.append([example.index(sub[0]),example.index(sub[-1])])
print result
ADD COMMENT

Login before adding your answer.

Traffic: 1948 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6