Run the following code,
a = [1, 2, 3, 4, 5]
head, *tail = a
p head
p tail
You will get the result
1
[2, 3, 4, 5]
Who can help me to explain the statement head,*tail = a, Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
head, *tail = ameans to assign the first element of the arrayatohead, and assign the rest of the elements totail.*, sometimes called the “splat operator,” does a number of things with arrays. When it’s on the left side of an assignment operator (=), as in your example, it just means “take everything left over.”If you omitted the splat in that code, it would do this instead:
But when you add the splat to
tailit means “Everything that didn’t get assigned to the previous variables (head), assign totail.”