forked from ujjwalkarn/DataSciencePython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_commands.py
More file actions
Latest commit
80 lines (50 loc) · 1.33 KB
/
Copy pathbasic_commands.py
File metadata and controls
80 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
>>>a= ['a', 'b', 'c', 'd', 'e']
>>>forindex, iteminenumerate(a): printindex, item
...
0a
1b
2c
3d
4e
#convert a list to string:
list1= ['1', '2', '3']
str1=''.join(list1)
Orifthelistisofintegers, converttheelementsbeforejoiningthem.
list1= [1, 2, 3]
str1=''.join(str(e) foreinlist1)
#FIND method
str.find(str2, beg=0end=len(string))
Parameters
str2--Thisspecifiesthestringtobesearched.
beg--Thisisthestartingindex, bydefaultits0.
end--Thisistheendingindex, bydefaultitsequaltothelenghtofthestring.
ReturnValue
Thismethodreturnsindexiffoundand-1otherwise.
str1="this is string example....wow!!!";
str2="exam";
printstr1.find(str2);
printstr1.find(str2, 10);
printstr1.find(str2, 40);
#15
#15
#-1
#2D LIST PYTHON
# Creates a list containing 5 lists initialized to 0
Matrix= [[0forxinrange(5)] forxinrange(5)]
Youcannowadditemstothelist:
Matrix[0][0] =1
Matrix[4][0] =5
printMatrix[0][0] # prints 1
printMatrix[4][0] # prints 5
ifyouhaveasimpletwo-dimensionallistlikethis:
A= [[1,2,3,4],
[5,6,7,8]]
thenyoucanextractacolumnlikethis:
defcolumn(matrix, i):
return [row[i] forrowinmatrix]
Extractingthesecondcolumn (index1):
>>>column(A, 1)
[2, 6]
Oralternatively, simply:
>>> [row[1] forrowinA]
[2, 6]