1

I need to read sp_list1 such that three elements from each list from corresponding positions are in a list. The next three (non-overlapping) are put into a separate list such that one makes a list of list.

Input: seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT']

Desired Output

seq_list_list1 =[['ATG','ATG','ATG'],['CTA','CTA','CTA'],['TCA','TCA','TCA'],['TTA','TTA','TTT']]

I have a feeling this should be doable using something like list comprehensions, but I can't figure it out (in particular, I can't figure out how to access the index of an item such that one picks three consecutive indices that are non-overlapping when using a list comprehension).

Rspacer
  • 2,369
  • 1
  • 14
  • 40
  • You must have realized that `append` takes an argument. Why didn't you provide one? – TigerhawkT3 Oct 31 '16 at 00:32
  • That was one part I was unsure about. Do I just put an empty list, like I have made the edit in? – Rspacer Oct 31 '16 at 00:37
  • It looks like the code that would actually do what you want is radically different from what you attempted (which wouldn't even run without error, for the reason given above). It is of course doable, but SO is not a coding service. You'll have to learn more and make another attempt. – TigerhawkT3 Oct 31 '16 at 00:38
  • Have you read the documentation for `list.append`? – TigerhawkT3 Oct 31 '16 at 00:40
  • 1
    appending empty list is useless - maybe `append(x)` or something similar could be usefull but it still need some other work. – furas Oct 31 '16 at 00:40
  • Your stated algorithm won't even produce the results you're asking for (only one line in the example starts with `>`, so you'd get nothing for the first list if you were looking for that). It's starting to look like you think you can get people to do your work for you by tossing out a few unrelated snippets. – TigerhawkT3 Oct 31 '16 at 00:42
  • 1
    @TigerhawkT3 Ouch! In the recent past, whenever I hav sought out to ask doubts.. I have always tried to write the code myself, because I do not have a good hold on syntax and some of the logic conversions into Python. I have never used SO as a 'coding service', but always attempted to learn from the answers. That being said, I am removing the first part of the question and will pose it as a separate one as it not relevant to the title. – Rspacer Oct 31 '16 at 01:03
  • @TigerhawkT3 And, for the second portion I genuinely do not know how I can go about coding it. I am still familiarizing myself with comprehensions. I always try to look up to check of any one else has asked a similar question prior to asking the same one. – Rspacer Oct 31 '16 at 01:03
  • 1
    Sorry, I didn't mean it to sting - unless it motivates you to learn more. :) Study up, refine your idea of what the program should do, and keep trying. – TigerhawkT3 Oct 31 '16 at 01:05
  • Your question is still a request for someone to write your code for you. If you don't know Python syntax, learn it. Again, SO is not a coding service. – TigerhawkT3 Oct 31 '16 at 01:17

2 Answers2

0

You may use this code here, you may manipulate it according to your desire. I hope it helps:

seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT']
n=3

seq_list1_empty=[]
counter = 0

for k in range(len(seq_list1)+1):
    for j in seq_list1:
        seq_list1_empty.append([j[i:i+n] for i in range(0, len(j), n)][counter])# this will reassemble the string as an index
    counter+=1

counter1=0
counter2=3
final_dic=[]
for i in range(4):
    final_dic.append(seq_list1_empty[counter1:counter2])#you access the first index and the third index here
    counter1+=3
    counter2+=3
print final_dic

The output is

[['ATG', 'ATG', 'ATG'], ['CTA', 'CTA', 'CTA'], ['TCA', 'TCA', 'TCA'], ['TTA', 'TTA', 'TTT']]
Sam Al-Ghammari
  • 1,021
  • 7
  • 23
  • 1) You are doing someone's work for them, and SO is not a coding service. 2) ___The output isn't even right.___ – TigerhawkT3 Oct 31 '16 at 01:50
  • I'm trying to help here. I am still checking the output PRO :D – Sam Al-Ghammari Oct 31 '16 at 01:59
  • After the edit, you are still doing someone's work for them with zero explanation (useless to the OP as well as future visitors), and it's still wrong. – TigerhawkT3 Oct 31 '16 at 02:00
  • 1
    I am downvoting due to the fact that @TigerhawkT3 already mentioned. Doing the OP's homework it not going to help him learn. And I'm sorry if you did, but I still see not explanation of your code. It helps before you answer if you ask yourself if the question: _If and how my will my answer help future users_. If you cannot answer with a definitive yes to the question above, it's best not to post an answer. – Christian Dean Oct 31 '16 at 02:22
  • @TigerhawkT3 @ leaf: Apologies, I was away for a while and did not see these comments. I am not trying to pick up a fight here nor do I want to be one of those OPs who just does a "use-and-throw" of the community. I am only asking the following questions, to UNDERSTAND this community better. (1/2) – Rspacer Oct 31 '16 at 02:57
  • 3
    I am unable to understand how is my current question different from the one here http://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists or http://stackoverflow.com/questions/6632188/explicitly-select-items-from-a-python-list-or-tuple. Why are these questions massively upvoted or answered and what is the difference between theirs and mine?(2/2) – Rspacer Oct 31 '16 at 02:57
  • 3
    Also, @TigerhawkT3 I don't mean to throw you under the bus, but a OP asked a question similar to how I have phrased, without asking for reason/ explanation or anything else at http://stackoverflow.com/questions/40326582/convert-a-dict-object-to-nested-list/40326592#40326592. I noticed that you have replied to the same, but I don't recognize or understand why you did reply to that OP and not ask him about the attempted code or tell him what you just told me? – Rspacer Oct 31 '16 at 03:08
0
seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT']


def new_string(string, cut):
    string_list = list(string) # turn string into list

    # create new list by appending characters from from index specified by
    # cut variable
    new_string_list = [string_list[i] for i in range(cut, len(string_list))]

    # join list characters into a string again
    new_string = "".join(new_string_list)

    # return new string
    return new_string


new_sequence = [] # new main sequence

# first for loop is for getting the 3 sets of numbers
for i in range(4):
    sub_seq = [] # contains sub sequence

    # second for loop ensures all three sets have there sub_sets added to the
    #sub sequence
    for set in range(3):
        new_set = seq_list1[set][0:3] #create new_set
        sub_seq.append(new_set) # append new_set into sub_sequence


    #checks if sub_seq has three sub_sets withing it, if so
    if len(sub_seq) == 3:
        #the first three sub_sets in seq_list1 sets are removed
        for i in range(3):
            # new_string function removes parts of strings and returns a new
            # string look at function above

            new_set = new_string(seq_list1[i], 3) # sub_set removed
            seq_list1[i] = new_set # new set assigned to seq_list1

    # new_sub sequence is added to new_sequence
    new_sequence.append(sub_seq)

    #sub_seq is errased for next sub_sequence
    sub_seq = []


print(new_sequence)

Try this. I'm sorry if it's difficult to understand, not very proficient at documentation.

Nex_Lite
  • 358
  • 2
  • 7