Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print letters in specific pattern in Python

Tags:

I have the follwing string and I split it:

>>> st = '%2g%k%3p' >>> l = filter(None, st.split('%')) >>> print l ['2g', 'k', '3p'] 

Now I want to print the g letter two times, the k letter one time and the p letter three times:

ggkppp 

How is it possible?

like image 216
MLSC Avatar asked Feb 08 '16 09:02

MLSC


People also ask

How do you print certain letters in Python?

all you need to do is add brackets with the char number to the end of the name of the string you want to print, i.e. Show activity on this post. Well if you know the character you want to search you can use this approach. you can change the print statement according to your logic.

How do I print a pattern side by side in Python?

To print the letters side by side you have to concatenate the individual lines. That generally means splitting the lines, joining the corresponding lines, then putting the combined lines back together. It helps that your letters are in a rectangular block, so you don't have to work out the padding for each line.


1 Answers

You could use generator with isdigit() to check wheter your first symbol is digit or not and then return following string with appropriate count. Then you could use join to get your output:

''.join(i[1:]*int(i[0]) if i[0].isdigit() else i for i in l) 

Demonstration:

In [70]: [i[1:]*int(i[0]) if i[0].isdigit() else i for i in l ] Out[70]: ['gg', 'k', 'ppp']  In [71]: ''.join(i[1:]*int(i[0]) if i[0].isdigit() else i for i in l) Out[71]: 'ggkppp' 

EDIT

Using re module when first number is with several digits:

''.join(re.search('(\d+)(\w+)', i).group(2)*int(re.search('(\d+)(\w+)', i).group(1)) if re.search('(\d+)(\w+)', i) else i for i in l) 

Example:

In [144]: l = ['12g', '2kd', 'h', '3p']  In [145]: ''.join(re.search('(\d+)(\w+)', i).group(2)*int(re.search('(\d+)(\w+)', i).group(1)) if re.search('(\d+)(\w+)', i) else i for i in l) Out[145]: 'ggggggggggggkdkdhppp' 

EDIT2

For your input like:

st = '%2g_%3k%3p' 

You could replace _ with empty string and then add _ to the end if the work from list endswith the _ symbol:

st = '%2g_%3k%3p' l = list(filter(None, st.split('%'))) ''.join((re.search('(\d+)(\w+)', i).group(2)*int(re.search('(\d+)(\w+)', i).group(1))).replace("_", "") + '_' * i.endswith('_') if re.search('(\d+)(\w+)', i) else i for i in l) 

Output:

'gg_kkkppp' 

EDIT3

Solution without re module but with usual loops working for 2 digits. You could define functions:

def add_str(ind, st):     if not st.endswith('_'):         return st[ind:] * int(st[:ind])     else:         return st[ind:-1] * int(st[:ind]) + '_'  def collect(l):     final_str = ''     for i in l:         if i[0].isdigit():             if i[1].isdigit():                 final_str += add_str(2, i)             else:                 final_str += add_str(1, i)         else:             final_str += i     return final_str 

And then use them as:

l = ['12g_', '3k', '3p']  print(collect(l)) gggggggggggg_kkkppp 
like image 54
Anton Protopopov Avatar answered Oct 10 '22 16:10

Anton Protopopov