1

Imagin I have three lists like:

l = ['2g_', '3k', '3p']
p = ['3f', '2y_', '4k', 'p']
s = ['12g', 'k_', '3p']

So:

>>> ''.join(i[1]*int(i[0])+i[2:] if i[0].isdigit() else i for i in l)
'gg_kkkppp'
>>> ''.join(i[1]*int(i[0])+i[2:] if i[0].isdigit() else i for i in p)
'fffyy_kkkkp'
>>> ''.join(i[1]*int(i[0])+i[2:] if i[0].isdigit() else i for i in s)
'2gk_ppp'

But what in list s: 2gk_ppp must be ggggggggggggk_ppp

8
  • The problem is that you are not fetching the 12, but only the first digit of it. What are you trying to accomplish? Commented Feb 9, 2016 at 9:45
  • I want to capture two digits and print in number of them. for example 12g must print g 12 times... like other patterns in l and p lists Commented Feb 9, 2016 at 9:46
  • It doesn't wok in digits more than 9... I want increase it till 99 Commented Feb 9, 2016 at 9:48
  • @MLSC just replace \w+ in this answer to [a-z] Commented Feb 9, 2016 at 9:48
  • Have a look at regular expressions. "\d+" will match one or more digits (as string, you still have to convert them to a number). Commented Feb 9, 2016 at 9:49

2 Answers 2

4

You can use a nested list comprehension / generator expression with regular expressions.

def join(lst):
    return ''.join((int(n or 1) * c + r 
                    for (n, c, r) 
                    in (re.search(r"(\d*)(\w)(.*)", x).groups() for x in lst)))

First, use re.search with (\d*)(\w)(.*) to get the (optional) number, the character, and the (optional) rest for each string.

[re.search(r"(\d*)(\w)(.*)", x).groups() for x in lst]

For your second example, this gives [('3', 'f', ''), ('2', 'y', '_'), ('4', 'k', ''), ('', 'p', '')]. Now, in the outer generator, you can use or to provide a "default-value" in case the number is '' (or use a ternary expression if you prefer: int(n) if n else 1):

[int(n or '1') * c + r 
 for (n, c, r) 
 in (re.search(r"(\d*)(\w)(.*)", x).groups() for x in lst)]

This gives ['fff', 'yy_', 'kkkk', 'p']. Finally, join to get fffyy_kkkkp.

Sign up to request clarification or add additional context in comments.

Comments

0

This variant is a little bit more imperative and simple to follow:

import re as regex

def process_template(template):
    result = regex.match('(\d*)(.)(.*)', template)
    if (result.group(1)):
        return int(result.group(1)) * result.group(2) + result.group(3)
    else:
        return result.group(2) + result.group(3)

''.join([process_template(template) for template in ['3f', '2y_', '4k', 'p']])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.