Untitled

561 days ago by pub

Lots of wikipages:  Lists, Arrays, Creating Lists, Commands on Lists, Operations on Lists and the individual commands

#These are sage lists; the # sign indicates a comment x_d = [18,23,25,35,65,54,34,56,72,19,23,42,18,39,37] # ages of individuals y_d = [202,186,187,180,156,169,174,172,153,199,193,174,198,183,178] # maximum heart rate of each one n=len(x_d) print x_d print 'length of x_d is:',n 
       
[18, 23, 25, 35, 65, 54, 34, 56, 72, 19, 23, 42, 18, 39, 37]
length of x_d is: 15
[18, 23, 25, 35, 65, 54, 34, 56, 72, 19, 23, 42, 18, 39, 37]
length of x_d is: 15
list1=[j for j in range(11)] n=len(list1) print list1 print n print 'The 8th element of list1 is:', list1[7] 
       
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
11
The 8th element of list1 is: 7
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
11
The 8th element of list1 is: 7
# This generates a list of 200 integers randomly generated between 1 and 100 it is a Sage list. a=[int(random()*100+1) for j in range(200)] print a type(a) 
       
[25, 77, 91, 45, 32, 23, 28, 55, 95, 78, 6, 82, 71, 47, 59, 10, 31, 12,
86, 4, 51, 5, 86, 11, 6, 33, 51, 38, 62, 17, 37, 98, 20, 77, 16, 68, 14,
23, 41, 23, 16, 62, 51, 57, 65, 12, 55, 48, 17, 38, 19, 33, 34, 46, 18,
23, 38, 56, 17, 61, 62, 45, 61, 11, 62, 79, 76, 45, 86, 65, 1, 16, 88,
86, 29, 40, 3, 27, 77, 95, 61, 78, 5, 27, 88, 66, 83, 30, 49, 74, 55,
99, 30, 75, 76, 84, 44, 28, 87, 93, 70, 78, 13, 48, 18, 97, 19, 48, 5,
98, 80, 83, 55, 13, 84, 89, 52, 95, 43, 61, 77, 41, 18, 98, 52, 57, 46,
36, 12, 13, 74, 47, 11, 15, 83, 1, 100, 47, 100, 62, 32, 83, 10, 25,
100, 69, 82, 17, 42, 51, 2, 53, 20, 66, 58, 91, 95, 66, 81, 5, 6, 80,
12, 46, 98, 42, 22, 51, 36, 9, 91, 54, 34, 50, 80, 19, 80, 43, 42, 57,
60, 9, 9, 52, 48, 96, 2, 90, 54, 85, 99, 70, 88, 30, 77, 61, 13, 44, 71,
46]
<type 'list'>
[25, 77, 91, 45, 32, 23, 28, 55, 95, 78, 6, 82, 71, 47, 59, 10, 31, 12, 86, 4, 51, 5, 86, 11, 6, 33, 51, 38, 62, 17, 37, 98, 20, 77, 16, 68, 14, 23, 41, 23, 16, 62, 51, 57, 65, 12, 55, 48, 17, 38, 19, 33, 34, 46, 18, 23, 38, 56, 17, 61, 62, 45, 61, 11, 62, 79, 76, 45, 86, 65, 1, 16, 88, 86, 29, 40, 3, 27, 77, 95, 61, 78, 5, 27, 88, 66, 83, 30, 49, 74, 55, 99, 30, 75, 76, 84, 44, 28, 87, 93, 70, 78, 13, 48, 18, 97, 19, 48, 5, 98, 80, 83, 55, 13, 84, 89, 52, 95, 43, 61, 77, 41, 18, 98, 52, 57, 46, 36, 12, 13, 74, 47, 11, 15, 83, 1, 100, 47, 100, 62, 32, 83, 10, 25, 100, 69, 82, 17, 42, 51, 2, 53, 20, 66, 58, 91, 95, 66, 81, 5, 6, 80, 12, 46, 98, 42, 22, 51, 36, 9, 91, 54, 34, 50, 80, 19, 80, 43, 42, 57, 60, 9, 9, 52, 48, 96, 2, 90, 54, 85, 99, 70, 88, 30, 77, 61, 13, 44, 71, 46]
<type 'list'>
len(a) 
       
200
200
#now for NumPy arrays import numpy as np 
       
# This generates a list of 200 integers between 1 and 100 = RandomBetween[1,100]; it is a NumPy 1D array. We could have just put "a" inside the brackets. # Notice that there are no commas between elements! b=np.array([int(random()*100+1) for j in range(200)]) print(b) type(b) 
       
[ 11  42 100  69   3  16   5  80  41  39  46  16   4  31  79  58  98  18
  11  82  55  72  49  46  55  90  28  19  98   2  91  27  82  59  49  65
  65  28  74  21  42   4  77  40  30  49  35  48   3  45  27  10  43  80
  11  32  27  28  36  65  96  54  94  34  13  44  48  77  10  52  10   7
  45  30  12  41  16   5  61  35  41  11  92  83  43   2  85  97  72 100
  87  81   6  43  55  74  26  83  92  32  45   8  25   4  27  60  75  28
  94  75  41  48   4  70  25  49  22  42  24  79  34  37  15  52  66  49
  62  40  98  87  72  72  83  87  22  25  42  67   6  32  38  39  63  70
 100  70  83   7  72  92  52  87  73 100  31  51  19  46  98  25  19  91
  61  54  95  94  22  25  57  11  88  61   2   5  19  37   1   1  93  83
  86   1  43  32  53  58  27  43  57  71  82  60  99  45  32  63  98   9
  38  37]
<type 'numpy.ndarray'>
[ 11  42 100  69   3  16   5  80  41  39  46  16   4  31  79  58  98  18
  11  82  55  72  49  46  55  90  28  19  98   2  91  27  82  59  49  65
  65  28  74  21  42   4  77  40  30  49  35  48   3  45  27  10  43  80
  11  32  27  28  36  65  96  54  94  34  13  44  48  77  10  52  10   7
  45  30  12  41  16   5  61  35  41  11  92  83  43   2  85  97  72 100
  87  81   6  43  55  74  26  83  92  32  45   8  25   4  27  60  75  28
  94  75  41  48   4  70  25  49  22  42  24  79  34  37  15  52  66  49
  62  40  98  87  72  72  83  87  22  25  42  67   6  32  38  39  63  70
 100  70  83   7  72  92  52  87  73 100  31  51  19  46  98  25  19  91
  61  54  95  94  22  25  57  11  88  61   2   5  19  37   1   1  93  83
  86   1  43  32  53  58  27  43  57  71  82  60  99  45  32  63  98   9
  38  37]
<type 'numpy.ndarray'>
#The len function here gives an integer result len(b) 
       
200
200
#The shape command is INTERESTING. Notice that it does NOT say (200,1) np.shape(b) 
       
(200,)
(200,)
# This converts the sage list a to numpy 1D array c c=np.array(a) print(c) type(c) 
       
[  6   7  56 100  95  84  25  39  31  98  98  46  71  32  44  39  23  56
  57  10  74  93  71  69  49  81  66   8  31  34  48  52  56  67  34  87
  43  56  42  87   9  91  96  94  73  34  64  12  37  87  24  18  36  40
  74  44  13  33   1  41  65  32  27  76  75  53  38  46  38   8  48  94
  49  24   8  20  14  35  14  38  32   1  16  35  14  74  88   5  14  82
  42  65   3  46  76  78  25  55  40  50  88  19  84  69  39  62  29  88
  27  86  32  62   5  62   3  97   4  77  82  80  71  21  35  95  17  80
  54  41  47  17  17  78  62  36  91  46  42  86  58  50  61  16  78  39
  19  74  66  80  62  98   1  64  20  26  32  40  50  82  45  81  47  59
  88  97  14  48  96  73  89  14  42  52  29  44  17  62  25   2  67  29
  85  11   5  53  46   3  23  11  11  89  80  26  62  49  93  69   5  76
  97  68]
<type 'numpy.ndarray'>
[  6   7  56 100  95  84  25  39  31  98  98  46  71  32  44  39  23  56
  57  10  74  93  71  69  49  81  66   8  31  34  48  52  56  67  34  87
  43  56  42  87   9  91  96  94  73  34  64  12  37  87  24  18  36  40
  74  44  13  33   1  41  65  32  27  76  75  53  38  46  38   8  48  94
  49  24   8  20  14  35  14  38  32   1  16  35  14  74  88   5  14  82
  42  65   3  46  76  78  25  55  40  50  88  19  84  69  39  62  29  88
  27  86  32  62   5  62   3  97   4  77  82  80  71  21  35  95  17  80
  54  41  47  17  17  78  62  36  91  46  42  86  58  50  61  16  78  39
  19  74  66  80  62  98   1  64  20  26  32  40  50  82  45  81  47  59
  88  97  14  48  96  73  89  14  42  52  29  44  17  62  25   2  67  29
  85  11   5  53  46   3  23  11  11  89  80  26  62  49  93  69   5  76
  97  68]
<type 'numpy.ndarray'>
var ('x') 
       
x
x
# This counts the elements of the sage list a that are <= to 74; x is just "generic" (place holder) variable) num=len([x for x in a if x<=74]) print num 
       
152
152
# The same command works for numpy 1D array. num=len([x for x in c if x<=74]) print num 
       
152
152

To sort a sage list, use the command sorted. Use the option reverse=1 to reverse the order of the sort (descending).

 

a_s=sorted(a) print a_s 
       
[1, 1, 2, 2, 3, 4, 5, 5, 5, 5, 6, 6, 6, 9, 9, 9, 10, 10, 11, 11, 11, 12,
12, 12, 12, 13, 13, 13, 13, 14, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18,
18, 19, 19, 19, 20, 20, 22, 23, 23, 23, 23, 25, 25, 27, 27, 28, 28, 29,
30, 30, 30, 31, 32, 32, 33, 33, 34, 34, 36, 36, 37, 38, 38, 38, 40, 41,
41, 42, 42, 42, 43, 43, 44, 44, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47,
48, 48, 48, 48, 49, 50, 51, 51, 51, 51, 51, 52, 52, 52, 53, 54, 54, 55,
55, 55, 55, 56, 57, 57, 57, 58, 59, 60, 61, 61, 61, 61, 61, 62, 62, 62,
62, 62, 65, 65, 66, 66, 66, 68, 69, 70, 70, 71, 71, 74, 74, 75, 76, 76,
77, 77, 77, 77, 77, 78, 78, 78, 79, 80, 80, 80, 80, 81, 82, 82, 83, 83,
83, 83, 84, 84, 85, 86, 86, 86, 86, 87, 88, 88, 88, 89, 90, 91, 91, 91,
93, 95, 95, 95, 95, 96, 97, 98, 98, 98, 98, 99, 99, 100, 100, 100]
[1, 1, 2, 2, 3, 4, 5, 5, 5, 5, 6, 6, 6, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 22, 23, 23, 23, 23, 25, 25, 27, 27, 28, 28, 29, 30, 30, 30, 31, 32, 32, 33, 33, 34, 34, 36, 36, 37, 38, 38, 38, 40, 41, 41, 42, 42, 42, 43, 43, 44, 44, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 48, 48, 48, 48, 49, 50, 51, 51, 51, 51, 51, 52, 52, 52, 53, 54, 54, 55, 55, 55, 55, 56, 57, 57, 57, 58, 59, 60, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 65, 65, 66, 66, 66, 68, 69, 70, 70, 71, 71, 74, 74, 75, 76, 76, 77, 77, 77, 77, 77, 78, 78, 78, 79, 80, 80, 80, 80, 81, 82, 82, 83, 83, 83, 83, 84, 84, 85, 86, 86, 86, 86, 87, 88, 88, 88, 89, 90, 91, 91, 91, 93, 95, 95, 95, 95, 96, 97, 98, 98, 98, 98, 99, 99, 100, 100, 100]
#Notice the "shape" of a numpy 1d array. Officially it should be (1,4), but you can use it as either (4,1) or (1,4). list1=[-1, 3, 7, 11] array1=numpy.array(list1) print array1 print numpy.shape(array1) 
       
[-1  3  7 11]
(4,)
[-1  3  7 11]
(4,)
#The sorted command works on a numpy 1D array sorted(array1,reverse=1) 
       
[11, 7, 3, -1]
[11, 7, 3, -1]
A=numpy.array([[1, 4], [2,1], [-3,7]]) print A print A[0], A[2,1] print numpy.shape(A), numpy.ndim(A), type(A), numpy.dtype(A[0,0]) 
       
[[ 1  4]
 [ 2  1]
 [-3  7]]
[1 4] 7
(3, 2) 2 <type 'numpy.ndarray'> int64
[[ 1  4]
 [ 2  1]
 [-3  7]]
[1 4] 7
(3, 2) 2 <type 'numpy.ndarray'> int64