0

I have a long list like this: A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']

How can I get only the letters part without numbers and make them another list?

5 Answers 5

3
>>> [''.join([l for l in x if l.isalpha()]) for x in xs]
['ABCd', 'Efhgh', 'dhAsadjkhdk']
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to make an intermediate list, str.join works on any iterable.
2

Using re.sub, this is quite simple!

>>> strings = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> import re
>>> [re.sub(r'[^A-Za-z]+', '', s) for s in strings]
['ABCd', 'Efhgh', 'dhAsadjkhdk']

Want the numbers too?

>>> [re.sub(r'[^\d]+', '', s) for s in strings]
['123', '345', '23']

1 Comment

Much better than flattening the list of matches. +1
2
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> B = list(map(lambda x: ''.join([letter for letter in x if letter.isalpha()]), A))
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']

filter() would have been a better choice than map():

>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> B = [''.join(filter(str.isalpha, a)) for a in A]
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']

This, of course, is basically identical to @chrisaycock's answer.

If you'd like to eliminate duplicate entries, use a set:

>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = set(''.join(filter(str.isalpha, a)) for a in A)
>>> B
{'Efhgh', 'dhAsadjkhdk', 'ABCd'}

If you'd like to eliminate duplicate entries but you need to preserve the ordering of the original list, sort a set by index:

>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = [''.join(filter(str.isalpha, a)) for a in A]
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk', 'ABCd']
>>> C = sorted(set(B), key=lambda x: B.index(x))
>>> C
['ABCd', 'Efhgh', 'dhAsadjkhdk']

or avoid an intermediate list and set by doing it manually:

>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = []
>>> for a in A:
...     b = ''.join(filter(str.isalpha, a))
...     if b not in B:
...             B.append(b)
...
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']

3 Comments

Your solution is better than chrisaycock's answer, if only for the simple reason that in Python 3.x, filter returns an iterator instead of a string. (He's using an intermediate list instead of a generator) In Python 2.x, the advantage is pure elegance, you can simply do filter(str.isalpha, s) and it will automatically return a string without you having to join because filter does a smart type-check.
Thank you @TigerhawkT3, I think I will use this. But after that, I need only one string if there's the same word of it. Like, I need only 'ABCd' but there is 5 'ABCd' element in the B list that you wrote actually.
I've updated my answer to include solutions for removing duplicates, with and without preserving the original list's order.
0
>>> values = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']

>>> [value.split()[0] for value in values]
['ABCd', 'Efhgh', 'dhAsadjkhdk']

2 Comments

This assumes a certain structure which hasn't been mentioned or confirmed and may be coincidental.
@TigerhawkT3 need more test cases.
0

Use regular expressions to search for the groups of letters in the list:

words = map(lambda s: re.findall(r'[a-zA-Z]+', s), A)
words = list(itertools.chain.from_iterable(words))

Comments

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.