Syntax question
Dear Group, It's really rookie question, but I'm currently helping my wife in some python-cases, where I'm non-python developer and some of syntax-diffs make me a bit confused. Could anyone give some light on line, as following: ds = d[:] ### where 'd' is an array Let me guess, is it a declaration of two-dimension array? Thanks a lot for help and all the best, Przemek M. Zawada -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax question
On 2 Cze, 19:56, geremy condra debat...@gmail.com wrote: On Wed, Jun 2, 2010 at 10:40 AM, pmz przemek.zaw...@gmail.com wrote: Dear Group, It's really rookie question, but I'm currently helping my wife in some python-cases, where I'm non-python developer and some of syntax-diffs make me a bit confused. Could anyone give some light on line, as following: ds = d[:] ### where 'd' is an array I'm guessing you mean that d is a list. The square braces with the colon is python's slicing notation, so if I say [1,2,3,4][0] I get a 1 back, and if I say [1,2,3,4][1:4] I get [2,3,4]. Python also allows a shorthand in slicing, which is that if the first index is not provided, then it assumes 0, and that if the second index is not provided, it assumes the end of the list. Thus, [1,2,3,4][:2] would give me [1,2] and [1,2,3,4][2:] would give me [3, 4]. Here, neither has been provided, so the slice simply takes the items in the list from beginning to end and returns them- [1,2,3,4][:] gives [1,2,3,4]. The reason someone would want to do this is because lists are mutable data structures. If you fire up your terminal you can try the following example: a = [1,2,3,4] b = a c = [:] b[0] = 5 b [5,2,3,4] # here's the issue a [5,2,3,4] # and the resolution c [1,2,3,4] Hope this helps. Geremy Condra Thank you for such fast answer! I quite catch, but: As I see, the d[:] is equal to sentence get the d array from the first to the last element? :) P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax question
On 2 Cze, 20:07, Matteo Landi landima...@gmail.com wrote: Anyway I suggest you to use a syntax like: b = list(a) in order to copy a list, it should be better than slicing. On Wed, Jun 2, 2010 at 7:56 PM, geremy condra debat...@gmail.com wrote: On Wed, Jun 2, 2010 at 10:40 AM, pmz przemek.zaw...@gmail.com wrote: Dear Group, It's really rookie question, but I'm currently helping my wife in some python-cases, where I'm non-python developer and some of syntax-diffs make me a bit confused. Could anyone give some light on line, as following: ds = d[:] ### where 'd' is an array I'm guessing you mean that d is a list. The square braces with the colon is python's slicing notation, so if I say [1,2,3,4][0] I get a 1 back, and if I say [1,2,3,4][1:4] I get [2,3,4]. Python also allows a shorthand in slicing, which is that if the first index is not provided, then it assumes 0, and that if the second index is not provided, it assumes the end of the list. Thus, [1,2,3,4][:2] would give me [1,2] and [1,2,3,4][2:] would give me [3, 4]. Here, neither has been provided, so the slice simply takes the items in the list from beginning to end and returns them- [1,2,3,4][:] gives [1,2,3,4]. The reason someone would want to do this is because lists are mutable data structures. If you fire up your terminal you can try the following example: a = [1,2,3,4] b = a c = [:] b[0] = 5 b [5,2,3,4] # here's the issue a [5,2,3,4] # and the resolution c [1,2,3,4] Hope this helps. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list -- Matteo Landihttp://www.matteolandi.net/ In fact, that ain't my syntax, I'd rather use C++ for that project, because that's my world is not Python, but thank you anyway for help - I see that Python also has many fans and friends online :) I'll try help her using your explanations. THANK you again and all the best, Przemek M. Zawada -- http://mail.python.org/mailman/listinfo/python-list