Python Forum
nframes = params[:4] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: nframes = params[:4] (/thread-15459.html)



nframes = params[:4] - vipinv23 - Jan-18-2019

Hello,

I am using the following code in wave file processing:

f = wave.open("file.wav", "rb")
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]

but i have not understood the working of "nframes = params[:4]"
what does "params[:4]" means?
[:4] is used for what?

Please guide


RE: nframes = params[:4] - johnb546 - Jan-18-2019

If I was to take a guess I would say that it means assign the 4th entry to nframes from the supplied f.getparams. So if you had 1 2 3 4 as the input it would assign 4 to nframes.

Regards


RE: nframes = params[:4] - vipinv23 - Jan-18-2019

Hello,

I have got it
Thank you


RE: nframes = params[:4] - buran - Jan-18-2019

That is slicing notation. It will give you everything from index 0, up to, but not including index 4. in other words - first 4 elements of params.


RE: nframes = params[:4] - perfringo - Jan-18-2019

(Jan-18-2019, 10:22 AM)vipinv23 Wrote: nchannels, sampwidth, framerate, nframes = params[:4]

As buran already answered, it's slicing combined with unpacking.

What it does can be explained with following code:

>>> a = [1, 2, 3, 4, 5]
>>> a[:4]
[1, 2, 3, 4]
>>> one, two, three, four = a[:4]
>>> one
1
>>> two
2
>>> three
3
>>> four
4



RE: nframes = params[:4] - vipinv23 - Jan-18-2019

Hello All,

Thank you for your replies.

Now I am able to related to [:4] means