snf_dodgehemi
snf_dodgehemi

Reputation: 47

Deriving pattern for runs of letters

I have a list of string "1. AGGCHRUSHCCKSGDSKCGGHCSG" I would like to get all "G" in my string and print a pattern like this: GG-G-GG-G

and if there are no Gs in my string, it should print "No G found".

I have tried basic string, substring, and print in python, but that's all I got. I can't find an Excel formula for this either. How can I generate this pattern?

Upvotes: 0

Views: 65

Answers (3)

Paul M.
Paul M.

Reputation: 10799

EDIT - With @PranavHosangadi's suggestions:

from itertools import groupby

string = "1. AGGCHRUSHCCKSGDSKCGGHCSG"

groups = ("".join(group) for key, group in groupby(string) if key == "G")

print("-".join(groups))

Output:

GG-G-GG-G
>>> 

Upvotes: 1

BrokenBenchmark
BrokenBenchmark

Reputation: 19243

You can use a regular expression to replace sequences of one or more non-"G" characters with a single dash, and then use .strip() to remove any leading or trailing dashes:

import re

data = "1. AGGCHRUSHCCKSGDSKCGGHCSG"
result = re.sub(r"[^G]+", r"-", data).strip("-")
if "G" in result:
    print(result)
else:
    print("No G found")

This outputs:

GG-G-GG-G

Upvotes: 2

alexander
alexander

Reputation: 195

string1= "1. AGGCHRUSHCCKSGDSKCGGHCSG"
string2=""
for char in string1:
    if char=='G':
        string2+=char
    else:
        string2+='-'
print(string2)

like this?

Upvotes: -1

Related Questions