Mocking
Mocking

Reputation: 1894

Splitting a string into a two separate lists of numbers and letters -python

The title sort of says it all. For example: I want to split

stringtosplit = 'hello57world' 

into

letters = ['h','e','l','l','o','w','o','r','l','d']
numbers = ['5', '7']

then make both of them back into strings,

letters = 'helloworld'
numbers = '57'

is there any neat way to do this? I want to keep my code as concise as possible. Numbers and letters can occur anywhere in the string and whitespace and special characters are already filtered out.

Upvotes: 2

Views: 5342

Answers (6)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58271

Tools:

  1. You should use this Python Regex with group. I belive that will provide most cleanup way:

    r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
    #           ^^^^^         ^^^^              ^^^
    #    before numbers      numbers        after numbers  
    # any group can be absent
    
  2. Then you can use re.findall(pattern, string, flags=0).

    Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples. if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

Test-Cases:

>>> re.findall(r, 'hello57world')[0]  # your string 
('hello', '57', 'world')
>>> re.findall(r, 'hello57')[0]  # word after number ""
('hello', '57', '')
>>> re.findall(r, '3234abcd')[0] # word before number ""
('', '3234', 'abcd')
>>> re.findall(r, '450')[0]  # only number
('', '450', '')
>>> re.findall(r, 'hello')[0]  # number is ""
('hello', '', '')
>>> re.findall(r, '')[0] # empty string
('', '', '')

Code:
Now you can write simple code in three lines:

>>> stringtosplit = 'hello57world' 
>>> r = r"(?P<first>[a-z]*)(?P<num>[0-9]*)(?P<last>[a-z]*)"
>>> f, n, l = re.findall(r, stringtosplit)[0]
>>> n
'57'
>>> f + l
'helloworld'

Give it a try!!

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Using regex:

import re

stringtosplit = 'hello57world'
letters = ''.join(re.findall('([a-zA-Z])', stringtosplit))
numbers = ''.join(re.findall('([0-9])', stringtosplit))

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19264

>>> stringtosplit = 'hello57world' 
>>> letters = []
>>> numbers = []
>>> for k in stringtosplit:
...     if k.isalpha() == True:
...         letters.append(k)
...     elif k.isdigit() == True:
...         numbers.append(k)
... 
>>> letters
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
>>> numbers
['5', '7']
>>> letters = ''.join(letters)
>>> numbers = ''.join(numbers)
>>> letters
'helloworld'
>>> numbers
'57'

Use str.isalpha to check if the variable is a letter, and str.isdigit to check if it is a number. Then use ''.join(str) to convert from a list to a str.

Upvotes: 1

jgritty
jgritty

Reputation: 11925

import string

letters = []
numbers = []

for achar in thestring:
    if achar in string.ascii_letters:
        letters.append(achar)
    if achar in string.digits:
        letters.append(achar)

letters_string = "".join(letters)
numbers_string = "".join(numbers)

Upvotes: 0

Steinar Lima
Steinar Lima

Reputation: 7821

Use str.join, str.isalpha, str.isdigit and generator comprehensions:

>>> s = 'hello57world'
>>> alphas = ''.join(c for c in s if c.isalpha())
>>> nums = ''.join(c for c in s if c.isdigit())
>>> print alphas, nums
helloworld 57

Upvotes: 1

Sheng
Sheng

Reputation: 3555

Typically, you could do it like:

>>> stringtosplit = 'hello57world'
>>> onlyLetter = "".join([i for i in stringtosplit if i.isalpha()])
>>> onlyLetter
'helloworld'
>>> onlyDig = "".join([i for i in stringtosplit if i.isdigit()])
>>> onlyDig

The function i.isalpha() will test whether i is a letter, and i.isdigit() test whether i is a digit.

Upvotes: 1

Related Questions