PhD
PhD

Reputation: 11334

What does this line of Python code do?

I'm currently reading through a Python code repo and I'm not sure I understand this syntax:

Sp = S[:Kp,:]
Sc = S[Kp:,:]

I'm guessing it has something to do with splicing but I'm not sure of how the comma operator is being used. S comes from an external system and its format is not discernable by looking at the code. I'm going to guess it's a list/array/matrix. Kp is an integer variable.

What exactly will Sp and Sc hold after the above code is run?

Upvotes: 2

Views: 165

Answers (1)

wim
wim

Reputation: 363566

S is likely a numpy ndarray. Kp is likely an integer. You guessed right that it was "splicing", but most people call this slicing. It is slicing on rows, and the second : after the comma refers to all columns.

Sp = S[:Kp,:]

Sp is a subarray of S with all rows up to (but not including) Kp.

Sc = S[Kp:,:]

Sc is a subarray of S with all rows from Kp to the end (inclusive).

Upvotes: 1

Related Questions