extraction with [:]

Messages in english

Modérateur : xcasadmin

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

extraction with [:]

Message par compsystems » jeu. avr. 25, 2019 10:42 pm

Hello BP

a:=["foo","bar","baz","qux","quux","corge"]
a[:4].extend(a[4:]) [enter]
".(a[0:4],extend(a[4 .. -1]))
Error: Invalid dimension"

expected
["foo","bar","baz","qux","quux","corge"]


a[0:6:2] [enter]
["foo", "baz", "quux"] # ok

a[1:6:2] [enter]
["bar", "qux", "corge"] # ok

a[6:0:-2] [enter]
[]
expected
["corge", "qux", "bar"]

a[::-1] [enter]
["corge", "quux", "qux", "baz", "bar", "foo"] # ok

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: extraction with [:]

Message par parisse » sam. avr. 27, 2019 9:55 am

You can only extend a variable containing a list, not a list.
a[6:0:-2] will return a non empty list only if a has size>=7.

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: extraction with [:]

Message par compsystems » lun. avr. 29, 2019 7:32 pm

You can only extend a variable containing a list, not a list
. =(

I wanted a synonym for + python operator a[:4]+(a[4:]) > a[:4].extend(a[4:]), without storing subvariables.

b:=a[:4]
b.extend(a[4:]) [enter]
b:=["foo","bar","baz","qux","quux","corge"]

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: extraction with [:]

Message par compsystems » sam. mai 18, 2019 2:37 pm

The following case works well,
[1].extend([2]) [enter] [1,2]

so the following should work.
a:=[9,8,7]
a[:2].extend(a[2:]) [enter]
[9,8,7]

Répondre