tags:

views:

93

answers:

5
[(',', 52),
 ('news', 15),
 ('.', 11),
 ('bbc', 8),
 ('and', 8),
 ('the', 8),
 (':', 6),
 ('music', 5),
 ('-', 5),
 ('blog', 4),
 ('world', 4),
 ('asia', 4),
 ('international', 4),
 ('on', 4),
 ('itunes', 4),
 ('online', 4),
 ('digital', 3)]

Suppose I have this list, with tuples inside.

How do I go through the list and remove elements that don't have alphabetical characters in them?

So that it becomes this:

[('news', 15),
 ('bbc', 8),
 ('and', 8),
 ('the', 8),
 ('music', 5),
 ('blog', 4),
 ('world', 4),
 ('asia', 4),
 ('international', 4),
 ('on', 4),
 ('itunes', 4),
 ('online', 4),
 ('digital', 3)]
+10  A: 
the_list = [(a, b) for a, b in the_list if a.isalpha()]
SilentGhost
`'a.'.isalpha()` -> False, so your solution is incorrect. `any(c.isalpha() for c in a)` will do the job. Or do you have a better solution?
Denis Otkidach
I think given examples do not expand to the question asked. while question itself sounds unambiguous I would say that considering the context OP is working with, it's likely that question simply doesn't reflect the task at hand.
SilentGhost
A: 

This uses string.ascii_letters, but SilentGhost's solution is to be preferred.

>>> from string import ascii_letters
>>> [(a, b) for a, b in l if all(c in ascii_letters for c in a)]
[('news', 15), ('bbc', 8), ('and', 8), ('the', 8), ('music', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)]
Stephan202
+3  A: 

Easiest should be a list comprehension with a regular expression:

import re

lst = [...]
lst = [t for t in lst if re.search(r'\w', t[0])]
sth
regex are clearly overkill here
SilentGhost
not to mention that your regex is wrong
SilentGhost
What is wrong with the answer? it seemed to work :)
TIMEX
@SilentGhost: Allright, `\w` also matches numbers and underscore, but it could be replaced by `[a-z]` or similar. Besides of that the OP requested to remove strings "that don't have alphabetical characters in them". In my interpretation that sounds like he wants to keep everything that has any alphabetical characters in it. So that's what my solution does. Yours removes everything that has a non-alpha in it somewhere. What is "correct" depends on what really should be accomplished.
sth
now you have two problems. :)
James Brooks
A: 

you could use built-in filter function too, Its dedicated to that purpose actually.

filter(lambda x:x[0].isalpha(),LIST)

The result is like this

[('news', 15), 
('bbc', 8), 
('and', 8), 
('the', 8), 
('music', 5), 
('blog', 4), 
('world', 4), 
('asia', 4),
('international', 4), 
('on', 4), 
('itunes', 4), 
('online', 4), 
('digital', 3)]
S.Mark
List comprehensions are more pythonic.
nikow
well, since I knew there is answers with that already, why should I duplicating those?
S.Mark
Thanks Mark! This helps
TIMEX
S.Mark: The filter() built-in function can be considered to be somewhat obsolete for Python. List comprehensions are used far more frequently these days. (They've become increasingly popular ever since their intro; generator expressions may slowly gain on them eventually).
Jim Dennis
ok, personally I prefer using list comprehension too. just want to proof that, there is built-in function.
S.Mark
there's nothing wrong using filter(). More pythonic?? how so? filter() is built in, so what makes it not more Pythonic than others? What's the real definition of Pythonic? filter() is definitely not going obsolete, not at the moment.
ghostdog74
+1  A: 

@OP, just go through the list items one by one, and check the first element of each item. This is just our simple and basic thought process. No need to think too deeply about being pythonic or not, or using fanciful list comprehensions etc.. keep everything simple.

l = [(',', 52),
 ('news', 15),
 ('.', 11),
 ('bbc', 8),
 ('and', 8),
 ('the', 8),
 (':', 6),
 ('music', 5),
 ('-', 5),
 ('blog', 4),
 ('world', 4),
 ('asia', 4),
 ('international', 4),
 ('on', 4),
 ('itunes', 4),
 ('online', 4),
 ('digital', 3)]

for item in l:
    if item[0].isalpha():
        print item

output

$ ./python.py
('news', 15)
('bbc', 8)
('and', 8)
('the', 8)
('music', 5)
('blog', 4)
('world', 4)
('asia', 4)
('international', 4)
('on', 4)
('itunes', 4)
('online', 4)
('digital', 3)
ghostdog74