-5

I know that the stack overflow community had asked this question before, but, is there a way to make a python script that separates out numbers and letters in a simplified way?

edit: i need to make a script that can separate numbers and letters not only numbers or letters

  • 1
    Do you want to "sort out numbers and letters" or "to know whats a number or not"? – ForceBru Nov 29 '19 at 18:02
  • This question was answered here: https://stackoverflow.com/questions/15046242/how-to-sort-the-letters-in-a-string-alphabetically-in-python – Felipe Américo Nov 29 '19 at 18:04
  • 2
    Does this answer your question? [python distinguish number and string solution](https://stackoverflow.com/questions/44016557/python-distinguish-number-and-string-solution) – prhmma Nov 29 '19 at 18:05
  • 2
    Welcome to Stackoverflow! Your question is pretty vague, and it is not clear what you are trying to accomplish. Please provide an **example** of what the inputs are, and the desired behavior of the program. Explain why the other SO answers are not satisfactory for your use case. – Kevin Nov 29 '19 at 18:08
  • That depends on what you mean by _sorts out_. Having to do this kind of operation can sometimes be a sign that you programmed yourself into a corner. – AMC Nov 29 '19 at 18:31

1 Answers1

0

I'd recommend creating one list and running through it in order to create two lists, one filled with number and the other with letters, sorting them individually and, if necessary, combining into one.

>>> a, b, c = [12, 43, "g", 9, "a", "x"], [], []
>>> for i in a:
...     try:
...             i = int(i)
...             b.append(i)
...     except:
...             if i == str(i):
...                     c.append(i)
...
>>> b = sorted(b)
>>> c = sorted(c)
>>> b
[9, 12, 43]
>>> c
['a', 'g', 'x']
>>> d = b + c #order : numbers, then letters
>>> d
[9, 12, 43, 'a', 'g', 'x']
peki
  • 669
  • 7
  • 24
  • this is very helpful but i need to put it in a file – Amiel Krantz Nov 30 '19 at 12:30
  • Rewrite it in the format you require... And if it is the solution that helps you. mark it as a solution by clicking on the little tick next to my answer. And welcome to Stack Overflow! – peki Nov 30 '19 at 13:50