0

I have a string -XX--XXX--XX- I'm trying to count each X and append it afterward. The above string should eventually be [2, 3, 2]. Here is a snippet of what I came up with:

knots = '-XX-XXX-XX-'

#Converting above string to ['XX', 'XXX', 'XX']
knots_without_dashes = knots.split('-')

For each 'X' in my list ['XX', 'XXX' and 'XX'] I want to count until the comma and append the result in an empty list without using counter library.

2
  • Hmm..I think you meant your output should be [2, 3, 2]? Might want to edit the question! Otherwise my answer is not correct. Commented Dec 1, 2016 at 23:45
  • yes edited my bad Commented Dec 1, 2016 at 23:49

1 Answer 1

1
[len(segment) for segment in knots.split('-') if len(segment)>0]

This will create a list made up of the length of each block that is separated by '-'.

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

2 Comments

Appreciate the help, this is exactly what I'm looking for to also solve the second half of the whole problem :)
NP! It's a very 'pythonic' sort of a problem, so this feature, which is called a "list comprehension" is a typical solution.

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.