I have a randomly generated list of which contains letters, symbols, and numbers and need to separate it into 2 lists -one of numbers and one of the remaining chars. Then I need to separate the numbers into 2 lists-one of composite numbers (4,6,8,9) and one a list of prime numbers(2,3,5,7). I can make a list of the numbers, but it doesn't separate it from the list:

>>>s="1#%$^5hbg458#$54bjyfuig324ghal&%90a11y$#6ty7"
>>>sdigits=''.join([letter for letter in s if letter.isdigit()])
>>>sdigits
'1545854324901167'

but if I then 'print s' the numbers are still in it. Any ideas?

Recommended Answers

All 2 Replies

Don't give up, just keep going ...

s = "1#%$^5hbg458#$54bjyfuig324ghal&%90a11y$#6ty7"

str_digits = ''.join([c for c in s if c.isdigit()])
str_nodigits = ''.join([c for c in s if not c.isdigit()])

str_comp = ''.join([c for c in str_digits if int(c) in (4,6,8,9)])
str_prime = ''.join([c for c in str_digits if int(c) in (2,3,5,7)])

print(str_nodigits)
print(str_digits)
print(str_comp)
print(str_prime)

"""
my result -->
#%$^hbg#$bjyfuigghal&%ay$#ty
1545854324901167
484496
555327
"""

Ah....! That's it! I didn't realize I could add 'not' like that. Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.