0

I have the following list:

L = [('carga', 'NCFS000', 'superior', 'AQ0CS0'),('carga', 'NCFS000', 'frontal', 'AQ0CS0')]

How can remove 'NCFS000', 'AQ0CS0' and the square brackets?, something like this:

[('carga', 'superior'),('carga', 'frontal')]

This is what i all ready tried:

def remove_values_from_list(the_list, val):
   return [value for value in the_list if value != val]

print "Esta es el bigrama final:\n",\
    remove_values_from_list(L, 'NCFS000')

But the id still in the list, how can i drop the id´s and get all the words in the desired format?. How can i declare a regex for this task?. Thanks

john doe
  • 2,233
  • 7
  • 37
  • 58
  • How can I "declare a regex for this task" is exactly the wrong thing to ask. It's almost always easier to process data that have been parsed into a nice data structure than to convert that data structure to a string and try to regex it into something you can parse back into a different data structure. – abarnert Oct 16 '14 at 18:48
  • so, is more efficient to use a data structure insted of a regex? – john doe Oct 16 '14 at 19:00
  • First, "more efficient" is almost always completely irrelevant; if they're both linear-time, and this isn't a bottleneck, who cares which one is faster? But yes, in the rare cases where it matters, using the data structure you already have will usually be more efficient than representing it as a string, using a regex, and re-parsing it. More importantly, it will almost certainly be easier to read, understand, extend, and debug for you and everyone else who has to deal with your code. – abarnert Oct 16 '14 at 19:33

5 Answers5

1

You can use a list comprehension and slicing:

>>> L = [('carga', 'NCFS000', 'superior', 'AQ0CS0'),('carga', 'NCFS000', 'frontal', 'AQ0CS0')]
>>> [x[::2] for x in L]
[('carga', 'superior'), ('carga', 'frontal')]
>>>
Community
  • 1
  • 1
  • 1
    That only works if the position of the unwanted items is fixed, not if you want to remove the items by value. – greschd Oct 16 '14 at 21:22
0

The problem here is that you have a nested collection, but not a nested loop. The top-level list doesn't have any values == 'NCFS000', so after removing all such values, nothing has changed.

What you want is: for each tuple in the list, remove each value == 'NCFS000', right? You've got two "each"es there, so you need two fors somewhere in your code.

def remove_values_from_list(the_list, val):
    return [[value for value in the_sublist if value != val]
            for the_sublist in the_list]

Of course this gives you a list of lists, not the list of tuples you started with. If that's a problem, Python doesn't have a "tuple comprehension", but it does that the tuple constructor and generator expressions, which is good enough:

def remove_values_from_list(the_list, val):
    return [tuple(value for value in the_sublist if value != val)
            for the_sublist in the_list]
abarnert
  • 354,177
  • 51
  • 601
  • 671
0

You are only iterating over the tuples, instead of the values themselves. If you want to remove multiple values at once, you can do it like this:

L = [('carga', 'NCFS000', 'superior', 'AQ0CS0'),('carga', 'NCFS000', 'frontal', 'AQ0CS0')]

def remove_values_from_list(the_list, val):    
    return [tuple(value for value in inner_list if value not in val) for inner_list in the_list]

print "Esta es el bigrama final:\n",\
    remove_values_from_list(L, ['NCFS000','AQ0CS0'])
greschd
  • 606
  • 8
  • 19
0

In your code given value != va compares 'NCFS000' with ('carga', 'NCFS000', 'superior', 'AQ0CS0') for example, not giving the desired result. You need to have a nested list comprehension.

This code filters based on a regex

import re
def remove_values_from_list(the_list, regex):
   return [tuple(val for val in value if re.match(regex, val)) for value in the_list]
print remove_values_from_list(L, r'^[a-z]+$')

I can't post comments, so I have to do this here... My use of regex is not the same as what abarnert is thinking. He thinks of turning the data structure into a string, and then running a regex on it. That would be absurd, as he says. But I am running a regex on the strings in the data structure. This I think we could both agree is good.

Michael Buckley
  • 591
  • 4
  • 14
0

You could compose your function by the following item:

L = [('carga', 'NCFS000', 'superior', 'AQ0CS0'),('carga', 'NCFS000', 'frontal', 'AQ0CS0')]
r = frozenset(['NCFS000', 'AQ0CS0'])
result = [filter(lambda i: i not in r, t) for t in L]

, the result is what you expect.

eth3lbert
  • 813
  • 5
  • 5