Python Forum
loop through range until reach size and exclude specific symbol
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop through range until reach size and exclude specific symbol
#1
[inline]
string = "ABC_DEF_GH__ILMN_OPQ__RSTUVZ"
index = 14
for i in string:
print([i-5],[i-4],[i-3],[i-2],[i-1],[i],[i+1],[i+2],[i+3],[i+4],[i+5])
[/inline]

output:
F_GH__ILMN_OPQ__R

I need that considering the position of the specific index that I will provide it will go to this index and will print it + 5 characters on the left and 5 on the right excluding the symbol "_".
the final output will be:
FGHILMNOPQR
so I have 5 characters + M +5 characters ( excluding any _ in the count)
with my code I'm able to indicate the index to print( i have not reported the code) but I'm not able to exclude the "_" from the count of the characters to print.
I need something like:
if you find "_" proceeds of one until you reach a length of 5 ( for each side )
Reply
#2
Are all letters unique?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
actually the real string is made of 4 letters(ACTG):
like:
AAGCTATTGACCTGAAACGATATTG

so you have strings like:

AAG_CTA___TTGA_CCTGAAA_CGA_TATTG
Reply
#4
One can combine itertools.islice and comprehension to achieve desired result:

>>> from itertools import islice
>>> s = "ABC_DEF_GH__ILMN_OPQ__RSTUVZ"
>>> i = 14
>>> before = reversed([*islice((char for char in reversed(s[:i]) if char != "_"), 5)])
>>> after = islice((char for char in s[i+1:] if char != "_"), 5)
>>> "".join((*before, s[i], *after))
'FGHILMNOPQR'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decryption not working if key has same symbol like text Paragoon2 0 312 Nov-11-2023, 09:32 PM
Last Post: Paragoon2
  Regex Include and Exclude patterns in Same Expression starzar 2 785 May-23-2023, 09:12 AM
Last Post: Gribouillis
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,438 May-14-2023, 04:29 PM
Last Post: Winfried
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,324 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  How to save specific variable in for loop in to the database? ilknurg 1 1,143 Mar-09-2022, 10:32 PM
Last Post: cubangt
  matplotlib x axis range goes over the set range Pedroski55 5 3,177 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Running loop at specific frequency mdsousa 3 5,918 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,098 Feb-03-2021, 05:14 AM
Last Post: chaitra
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,060 Nov-13-2020, 07:43 AM
Last Post: buran
  Telegram Users Scrapper - Exclude UserPrivacyRestricted graphite2015 0 2,595 Oct-23-2020, 05:43 AM
Last Post: graphite2015

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020