'bry den,

Vzdy sa da spravit nieco taketo:

class atuple(tuple):
    def __getitem__(self, index):
        if isinstance(index, tuple):
            ret = []
            for i in index:
                if isinstance(i, slice):
                    ret.extend(self[i])
                else:
                    ret.append(self[i])
            return tuple(ret)
        return tuple.__getitem__(self, index)

potom:
In [29]: aaa=atuple(xrange(20))

In [30]: aaa
Out[30]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

In [31]: aaa[1,2,5:8]
Out[31]: (1, 2, 5, 6, 7)

Jan Janech


PS: Pre inline-istov:

class atuple(tuple): __getitem__ = lambda self, index: reduce(tuple.__add__, ((self[i] if isinstance(i, slice) else (self[i], )) for i in index)) if isinstance(index, tuple) else tuple.__getitem__(self, index)


MICHÁLEK Jan Mgr. wrote:
Dobrý večer, řeším následující (banální problém), řešení jsem nenašel v knize, 
ani ve webovém tutoriálu, (používám python 2.5), nedaří se mi vybrat z tuple 
(ntice) konkrétní znaky pomocí indexu

Následující příklad mi nefunguje

print '%s,%s,%s'%('a','b','c','d','e','f')[:1,5]

Hlásí mi to následující:

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    print '%s,%s,%s'%('a','b','c','d','e','f')[:1,5]
TypeError: tuple indices must be integers

Indexy mají být celočíselné, hm, ale jak můžu zadat seznam indexů??
Děkuji a přeji dobrou noc Je.
__________ Informace od ESET NOD32 Antivirus, verze databaze 4141 (20090609) 
__________

Tuto zpravu proveril ESET NOD32 Antivirus.

http://www.eset.cz
_______________________________________________
Python mailing list
Python@py.cz
http://www.py.cz/mailman/listinfo/python






--

____________________________
Ing. Jan Janech
Katedra softverovych technologii
Fakulta riadenia a informatiky
Zilinska Univerzita
_______________________________________________
Python mailing list
Python@py.cz
http://www.py.cz/mailman/listinfo/python

Odpovedet emailem