|
1 |
| ->>> a = ['a', 'b', 'c', 'd', 'e'] |
2 |
| ->>> for index, item in enumerate(a): print index, item # enumerate function will generate an index for the item + item it self. |
3 |
| -... |
4 |
| -0 a |
5 |
| -1 b |
6 |
| -2 c |
7 |
| -3 d |
8 |
| -4 e |
9 |
| - |
10 |
| - |
11 |
| - |
| 1 | +a = ['a', 'b', 'c', 'd', 'e'] |
| 2 | +for index, item in enumerate(a): |
| 3 | + print(index, item) # enumerate function will generate an index for the item + item it self. |
12 | 4 |
|
13 | 5 | #convert a list to string:
|
14 | 6 |
|
15 | 7 | list1 = ['1', '2', '3']
|
16 | 8 | str1 = ''.join(list1)
|
17 | 9 |
|
18 |
| -Or if the list is of integers, convert the elements before joining them. |
| 10 | +#Or if the list is of integers, convert the elements before joining them. |
19 | 11 |
|
20 | 12 | list1 = [1, 2, 3]
|
21 | 13 | str1 = ''.join(str(e) for e in list1)
|
22 | 14 |
|
23 | 15 |
|
24 |
| - |
25 | 16 | #FIND method
|
26 | 17 |
|
27 |
| -str.find(str2, beg=0 end=len(string)) |
| 18 | +str.find(str1, beg=0, end=len(str)) |
28 | 19 |
|
| 20 | +''' |
29 | 21 | Parameters
|
30 | 22 | str2 -- This specifies the string to be searched.
|
31 | 23 | beg -- This is the starting index, by default its 0.
|
32 | 24 | end -- This is the ending index, by default its equal to the lenght of the string.
|
33 | 25 |
|
34 | 26 | Return Value
|
35 |
| -This method returns index if found and -1 otherwise. |
| 27 | +This method returns index if found and -1 otherwise.''' |
36 | 28 |
|
37 |
| -str1 = "this is string example....wow!!!"; |
38 |
| -str2 = "exam"; |
| 29 | +str1 = "this is string example....wow!!!" |
| 30 | +str2 = "exam" |
39 | 31 |
|
40 | 32 | # find function will print the position for the first character of the string if it's found!
|
41 |
| -print str1.find(str2); |
42 |
| -print str1.find(str2, 10); |
43 |
| -print str1.find(str2, 40); |
| 33 | +print(str1.find(str2)) |
| 34 | +print( str1.find(str2, 10)) |
| 35 | +print(str1.find(str2, 40)) |
44 | 36 |
|
45 | 37 | #15
|
46 | 38 | #15
|
|
54 | 46 |
|
55 | 47 | # Creates a list containing 5 lists initialized to 0
|
56 | 48 | Matrix = [[0 for x in range(5)] for x in range(5)]
|
57 |
| -You can now add items to the list: |
| 49 | + |
| 50 | +#You can now add items to the list: |
58 | 51 |
|
59 | 52 | Matrix[0][0] = 1
|
60 | 53 | Matrix[4][0] = 5
|
61 | 54 |
|
62 |
| -print Matrix[0][0] # prints 1 |
63 |
| -print Matrix[4][0] # prints 5 |
| 55 | +print(Matrix[0][0]) # prints 1 |
| 56 | +print(Matrix[4][0]) # prints 5 |
64 | 57 |
|
65 | 58 |
|
66 |
| -if you have a simple two-dimensional list like this: |
| 59 | +#if you have a simple two-dimensional list like this: |
67 | 60 |
|
68 | 61 | A = [[1,2,3,4],
|
69 | 62 | [5,6,7,8]]
|
70 |
| -then you can extract a column like this: |
| 63 | + |
| 64 | +#then you can extract a column like this: |
71 | 65 |
|
72 | 66 | def column(matrix, i):
|
73 | 67 | return [row[i] for row in matrix]
|
74 |
| -Extracting the second column (index 1): |
75 | 68 |
|
76 |
| ->>> column(A, 1) |
| 69 | +#Extracting the second column (index 1): |
| 70 | + |
| 71 | +column(A, 1) |
77 | 72 | [2, 6]
|
78 |
| -Or alternatively, simply: |
79 | 73 |
|
80 |
| ->>> [row[1] for row in A] |
| 74 | +#Or alternatively, simply: |
| 75 | + |
| 76 | +[row[1] for row in A] |
81 | 77 | [2, 6]
|
0 commit comments