help

Tim Chase python.list at tim.thechases.com
Thu Jul 19 20:46:36 EDT 2012


On 07/19/12 19:32, Miriam Gomez Rios wrote:
>>>>   s = "('per1','persona1.1','pro1'),('per1','persona1.1','pro2')"
>>   resulting_tuple_of_tuples = ast.literal_eval(s)
>
> Traceback (most recent call last):
>   File "C:\Users\Miriam\scripts\prueba_datos", line 124, in <module>
>     print resulting_tuple.select('perf1','*','*')
> AttributeError: 'tuple' object has no attribute 'select'

[copying the list, trimming your reply and changing to inline
quoting for easier reading]

I'm not sure why you expect there to be a .select method on the
resulting object.  Neither a list-of-tuples or a tuple-of-tuples has
a .select method.  It *sounds* like what you want to do is then to
do something like

  d = ast.literal_eval(s) # data as a tuple-of-tuples
  filtered = [t for t in d where t[0] == "perf1"]

or you can do

  filtered = [(v1,v2,v3) for (v1,v2,v3) in d where v1=="perf1"]

which allows you to assign more meaningful names to the bits, as
well as rearrange/select the contents:

  filtered = [(v3,v2) for (v1,v2,v3) in d where v1=="perf1"]

or you can even do further work like instantiating objects:

  filtered = [MyCls(v3,v2) for (v1,v2,v3) in d where v1=="perf1"]

Espero q' eso le ayuda a usted.

-tkc






More information about the Python-list mailing list