Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping in a list with sequence re-read

Tags:

python

list

I have a string (Ex: BCVDBCVCBCBD) which i have converted into a list using the

seq_split = [string[i:i+1] for i in range (0, len(string),1)]

This resulted in a list like ['B' ,'C','V'.........,'D']

Now considering I take a user input, in the form of a number (say for example 2). I need to read every 2nd element from the start i.e 1st letter of sequence and the letter that is 2 positions away from the 1st letter (in count itll be the 3rd letter from start). The second time ill need to read the 2nd letter in the sequence and again the letter 2 positions away. i.e. the 2nd and 4th letter of the sequence . (I know its a little confusing so heres a detailed explanation)

If the user input is the number 2: Then in a new list i will need to append (using the given string as example)- 'BV' , 'CD' , 'VB' and so on in a new list.

Similarly if the input is 4. Then the new list i create will have - 'BB', 'CC' etc... I am confused as to how do i index them.

like image 919
begin.py Avatar asked Nov 03 '12 22:11

begin.py


People also ask

How to Group a list by consecutive elements in Python?

Method : Using enumerate () + groupby () + generator function + lambda This task can be performed using the combination of above functions. In this, we create a generator function, in which we pass the list whose index-element are accessed using enumerate () and grouped by consecutive elements using groupby () and lambda. Works with Python2 only

What is a read group in Sam?

When multiplexing is involved, then each subset of reads originating from a separate library run on that lane will constitute a separate read group. Read groups are identified in the SAM/BAM /CRAM file by a number of tags that are defined in the official SAM specification.

What is the read group ID?

This tag identifies which read group each read belongs to, so each read group's ID must be unique. It is referenced both in the read group definition line in the file header (starting with @RG) and in the RG:Z tag for each read record.

How many times must a sequence show up in regex?

And you would be right. RegEx allows you to specify that a particular sequence must show up exactly five times by appending {5} to its syntax. For example, the expression \d {5} specifies exactly five numeric digits. You can also specify a series of at least four and no more than seven characters by appending {4,7} to the sequence.


2 Answers

You can get the desired list with

[x + y for x, y in zip(string, string[i:])]

where i is the number chosen by the user. Example:

>>> string = "BCVDBCVCBCBD"
>>> i = 2
>>> [x + y for x, y in zip(string, string[i:])]
['BV', 'CD', 'VB', 'DC', 'BV', 'CC', 'VB', 'CC', 'BB', 'CD']
like image 152
Sven Marnach Avatar answered Oct 01 '22 08:10

Sven Marnach


In [31]: L
Out[31]: ['B', 'C', 'V', 'D', 'B', 'C', 'V', 'C', 'B', 'C', 'B', 'D']

In [32]: sep = 2

In [33]: [L[i]+L[i+sep] for i in range(len(L)-sep)]
Out[33]: ['BV', 'CD', 'VB', 'DC', 'BV', 'CC', 'VB', 'CC', 'BB', 'CD']

In [34]: sep = 4

In [35]: [L[i]+L[i+sep] for i in range(len(L)-sep)]
Out[35]: ['BB', 'CC', 'VV', 'DC', 'BB', 'CC', 'VB', 'CD']
like image 32
inspectorG4dget Avatar answered Oct 01 '22 09:10

inspectorG4dget