Course  5. List

Simple Lists (1dim)

Definition - Elements

A={1,2,a,hello,x^2+1,0,{Q,W},Sin[θ],2}
Β={}

Length[A]  (* μήκος λίστας *)
A[[3]]  (* το 3o-στοιχείο της λίστας *)
A[[{3,5}]]

Position[A,x^2+1]  
k=%[[1,1]]

First[A]
Last[A]

Change position

B=RotateLeft[A]
B=RotateRight[A]

B=Reverse[A]  (* αντιστροφή σειράς στοιχείων *)
B=Sort[A]

B=Sort[{-1,2,4,-8,-3,5}]
B=Sort[{-1,2,4,-8,-3,5},Abs[#1]<Abs[#2]&]

Extract elements - Subsets

Print["My List = ",A]
B0=Rest[A]
B1=Drop[A,2]
B2=Drop[A,-2]

Print["My List = ",A]
B3=Take[A,3]
B4=Take[A,-3]
B5=Take[A,{4,7}]

Print["My List = ",A]
Delete[A,2]
Delete[A,-2]
Delete[A,{{3},{4},{6}}]

Insert elements

Print["My List = ",A]
B0=Append[A,1000]
B1=Prepend[B0,w]
B2=Insert[B1,neostoixeio,4]

insert elements in the same list

Print["My List = ",A]
AppendTo[A,1000];
Print["My List = ",A]
PrependTo[A,w];
Print["My List = ",A]

(* Replacements *)
B={a,b,c,d}
B1=ReplacePart[B,xxx,2]
B2=ReplacePart[B,3->yyy]

Sections - Compositions - Maps

A={1,2,a,b,3,c}
B={1,b,x,y}
Join[A,B]
Union[A,B]
Intersection[A,B]
Complement[A,B]

Listable Functions

Attributes[Cos]

A={0,Pi/6,Pi/4,Pi/3,Pi/2}
Cos[A]

Map[Cos,A]
Cos/@ A
N/@ %

List with List elements

A={1,2,a,hello,x^2+1,0,{Q,W},Sin[θ]}
A[[7]]  (* το 7o-στοιχείο της λίστας *)
A[[7,2]]  (* το 2o στοιχείο του 7oυ-στοιχείου της λίστας *)

A={1,2,{a,b},{X,{Y1,Y2},Z},w}
B1=Flatten[A,1]
B2=Flatten[A,2]

A={1,2,3,4,5,6,7,8,9}
Partition[A,2]

B1=Partition[A,3,1]

Length[B1]
Dimensions[B1]

Lists as NxM arrays

A={{1,2},{-1,3},{2,4}}
MatrixForm[A]

At=Transpose[A] //MatrixForm

B={{1,0,1},{2,1,1}} 
MatrixForm[B]

G=A.B 
MatrixForm[G]

Det[G]

H0=A*B
H1=G*G
H2=G.G

Create Lists with  Range and Table

counters

Range[10]
Range[20,30]
Range[20,30,2]

Range[1.5,2.7,0.1]

Lists of values or/and expressions

Table[k,{k,10}]

Table[1+k x^2,{k,-2,2}]

Table[y[i], {i,10}]

Table[{Sin[x],Cos[x]},{x,0,Pi/2,Pi/6}]

two counters

Table[a*b,{a,1,10},{b,1,10}]
MatrixForm[%]

Apply functions to list’s elements

Map[Cos[#1]^3+Sin[#1]^3&,Table[Pi/n,{n,1,10}]] //N

MapThread[Cos[#1]^3+Sin[#2]^3&,Transpose[Table[{x,Pi/2-x},{x,0,2Pi,Pi/4}]]] //N

MapThread[Cos[#1]^2+Cos[#2]^2&,Transpose[Table[{x,Pi/2-x},{x,0,2Pi,Pi/4}]]] //N