Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nframes = params[:4]
#1
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
Reply
#2
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
Reply
#3
Hello,

I have got it
Thank you
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Hello All,

Thank you for your replies.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,602 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  SARIMAX ValueError: xnames and params do not have the same length preetha8715 2 2,793 Oct-26-2018, 08:16 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020