5
\$\begingroup\$

I am currently learning Python and I wrote the following code.
The code searches the string for all digits that appear after ',, sorts them and joins them into a string.
I have a strong feeling that it can be shortened... Can anyone suggest how?

s = "'bhhd',12 'kjubk',2 'bjki',98 'khjbjj',4"
res = re.findall(r"(',)(\d+)", s)
all = []
for r in res:
 all.append(r[1])
print(",".join(sorted(all, key=int)))
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Nov 17, 2017 at 5:04
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

re.findall() behaves differently depending on whether the regex contains capturing parentheses. If it doesn't contain capturing parentheses, then it just returns the matching text as a flat list.

So, how can you avoid capturing? Just rewrite your regex to use a positive lookbehind assertion.

s = "'bhhd',12 'kjubk',2 'bjki',98 'khjbjj',4"
all = re.findall(r"(?<=',)\d+", s)
print(",".join(sorted(all, key=int)))
answered Nov 17, 2017 at 5:38
\$\endgroup\$
5
  • \$\begingroup\$ thanks - I thought of that this morning! I am understanding that otherwise (i.e. if I wanted both captures), the way I wrote it is as they say the 'pythonic' way ...? \$\endgroup\$ Commented Nov 17, 2017 at 17:54
  • 1
    \$\begingroup\$ If you want to capture, then you should use a list comprehension to build all. \$\endgroup\$ Commented Nov 17, 2017 at 18:25
  • \$\begingroup\$ thanks! I thought that would be the direction but I wasnt sure how. I think I figured it out - the following code works, did I write it well? Let me know, thanks, David. all = [r[1] for r in re.findall(r"(',)(\d+)"] \$\endgroup\$ Commented Nov 19, 2017 at 1:06
  • \$\begingroup\$ That's fine. re.finditer() might be slightly better in that case. \$\endgroup\$ Commented Nov 19, 2017 at 1:46
  • \$\begingroup\$ Ok, thanks. I got that to work but I am not sure why that might be any better... Can you explain? Thanks! \$\endgroup\$ Commented Nov 20, 2017 at 0:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.