-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures.py
More file actions
161 lines (111 loc) · 2.91 KB
/
Copy pathdata_structures.py
File metadata and controls
161 lines (111 loc) · 2.91 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
methods of list objects:
append, insert, extend, remove, pop, extend, clear, index, count, sort, reverse, copy
"""
fruits =[ "kiwi", "blueberry", "pear", "pineapple", "pear", "melon", "muskmelon", "banana", "kiwi"]
print(fruits.count("kiwi"))
fruits.reverse()
print(fruits)
print(fruits.pop())
fruits.append("grape")
print(fruits)
print(fruits.sort())
fruits.index("pear")
stack = [3, 4, 5]
stack.pop()
print(stack)
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Garry")
queue.append("Graham")
print(queue)
queue.popleft( )
print(queue)
queue.popleft()
squares = list(map(lambda x: x**2, range(10)))
print(squares)
squares = [x**2 for x in range(10)]
combs = [ ]
for x in [1, 2, 3]:
for y in [3, 1, 4]:
if x != y:
combs.append((x, y))
print(combs)
# this is concise and more readable
l = [(x, y) for x in [1, 2, 3] for y in (3, 1, 4) if x != y]
print(l)
print("*" * 50)
vec = [-4, -2, 0, 2, 4]
mbytwo = [x*2 for x in vec]
print(mbytwo)
positivenums = [x for x in vec if x>= 0]
print(positivenums)
# apply a function to all the elements
print([abs(x) for x in vec])
# call a method on each element
freshfruit = [ " banana", " loganberry", " passion fruit"]
c = [weapon.strip() for weapon in freshfruit]
print(c)
max =[ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
print([num for elem in max for num in elem])
from math import pi
print([str(round(pi, i)) for i in range(1, 6)])
matrix = [
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8],
[9, 10, 11, 12]
]
r = [[row[i] for row in matrix ] for i in range (4)]
del matrix[2:4]
print(r)
a = set("abracadaba!")
b = set("alacarazram")
print(a)
print(b)
print(a & b )
print(a - b)
print(a or b)
print(a | b)
print(a ^ b)
#set comprehension
a = { x for x in "abracadaba" if x not in "abc"}
print(a)
# waoh! this is sweeeet! I want more!
# You're doing pretty cool, Arafat. I love this
tel = {"Jack": 4098, "sape": 4139}
tel["guido"] = 4127
print(tel)
print(tel["Jack"])
del tel["sape"]
tel["irv"] = 4127
print(list(tel))
print(sorted(tel))
print("guido" in tel)
print("food" in tel)
dict(sape = 4139, jack = 4098)
x = {x: x**2 for x in (2, 4, 6)}
print(x)
knights = {"gallahad": "the pure", "robin": "the brave"}
for n, m in knights.items():
print(n, m)
for i, v in enumerate(["tic", "tac", "toe"]):
print(i,v)
questions = ["name", "quest", "favourite colour"]
answers = ["lancelot", "the holy grail", "lilac"]
for q, a in zip(questions, answers):
print("what is your {}? It is {}".format(q, a))
for i in reversed(range(1, 10, 2)):
print(i)
basket = ["apple", "orange", "banana", "pear", "apple"]
for i in sorted(set(basket)):
print(i)
import math
raw_data = [56.2, float("NaN"), 51.7, 55.3, 52.5, float("NaN"), 47.8]
filtered_data =[ ]
for value in raw_data:
if not math.isnan(value):
filtered_data.append(value)
print(filtered_data)
string1, string2, string3 = "", "Trondheim", "Hammer Dance"
non_null = string1 or string2 or string3
print(non_null)