0

I'm trying to filter a tuple as follows...some code not included for brevity:

    1) print >> sys.stderr, "audio", audio
    2) print >> sys.stderr, "audio[0]", audio[0]
    3) print >> sys.stderr, "audio[1]", audio[1]
    4) audio_lang = filter(lambda a: a[1]==LANG, audio)

It is being passed a tuple with 2 elements, the run is as follows:

 D:\Staging\Test>cleanMKV.py .
 audio [('fre',), ('eng',)]
 audio[0] ('fre',)
 audio[1] ('eng',)
 Traceback (most recent call last):
 File "D:\Staging\Test\cleanMKV.py",
 audio_lang = filter(lambda a: a[1]
 File "D:\Staging\Test\cleanMKV.py",
 audio_lang = filter(lambda a: a[1]
 IndexError: tuple index out of range

The tuple was created properly with RE and I'm at a point I want to filter as shown on line 4. It is trying to reference the audio a[1] in audio.

Any help appreciated.

1
  • 1
    Are you sure you understand what filter does? Commented Mar 7, 2013 at 0:25

2 Answers 2

2

First, you define

audio = [('fre',), ('eng',)]

which is actually a list of two elements ('fre',) and ('eng',), which are both tuples containing only a single element, which are 'fre' and 'eng' respectively.

Now, if you do

audio_lang = filter(lambda a: a[1]==LANG, audio)

you are saying to keep only elements of audio, where the second elements equals to LANG. But as your list elements ('fre',) and ('eng',) are tuples of length 1, you are getting

IndexError: tuple index out of range

because you are trying to access the second elements, which do not exist.

You could do

audio_lang = filter(lambda a: a[0]==LANG, audio)

that is access the first element or redefine audio and do

audio = ['fre', 'eng']
audio_lang = filter(lambda a: a == LANG, audio)

But I do not understand, what are you trying to accomplish, so this might not be what you want. But hopefully this explains the source of the error you are having.

Sign up to request clarification or add additional context in comments.

Comments

0

a is a tuple but audio is a list. There is only one item in each of the two tuples that make up audio. Therefore, evaluating either audio[0][1] or audio[1][1] will return tuple index out of range.

>>> audio = [('fre',),('eng',)]

>>> audio
[('fre',), ('eng',)]
>>> audio[0]
('fre',)
>>> audio[0][0]
'fre'
>>> audio[0][1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

... but:

>>> audio = [('fre',1),('eng',2)]
>>> audio[0][1]
1

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.