0

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?

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59

3 Answers3

2

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
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
1

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
>>> 
Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • 1
    You could improve the efficiency of this code by joining letters in each group when you calculate `groups` and also by replacing that list comprehension with a generator expression. `groups = ("".join(group) for key, group in groupby(string) if key == "G"); "-".join(groups)` – Pranav Hosangadi Apr 14 '22 at 22:33
-1
string1= "1. AGGCHRUSHCCKSGDSKCGGHCSG"
string2=""
for char in string1:
    if char=='G':
        string2+=char
    else:
        string2+='-'
print(string2)

like this?

alexander
  • 195
  • 1
  • 10
  • 3
    This doesn't produce the desired output, and [repeated string concatenation is bad](https://stackoverflow.com/q/37133547/17769815). – BrokenBenchmark Apr 14 '22 at 19:21