-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_01_data_structures_lists.py
More file actions
123 lines (95 loc) · 3.09 KB
/
04_01_data_structures_lists.py
File metadata and controls
123 lines (95 loc) · 3.09 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
# Lists
# Basics
foods = ['rice', 'Meat', 'vegetables', 'Eggs']
print(foods)
# Same as foods[len(foods):] = ['butter']
foods.append('butter')
print(foods)
# Same as foods[len(foods):] = ['tomatoes', 'chili sauce']
foods.extend(['tomatoes', 'Chili sauce'])
print(foods)
# Reverse order of elements in the list
foods.reverse()
print(foods)
# Copy the list
copy_of_foods = foods.copy()
print(copy_of_foods)
# Sort in ascending order
foods.sort()
print(foods)
# Sort in descending order without considering lower or upper case
copy_of_foods.sort(key=str.lower, reverse=True)
print(copy_of_foods)
# Using Lists as Stacks
stack_normal = ['+', 4, '*', 7, '-', 3, 6]
stack_error = ['+', 4, '?', 7, '-', 3, 6]
def evaluate(stack):
expression = ''
round = 0
while len(stack) >= 3:
first_operand = stack.pop()
second_operand = stack.pop()
operator = stack.pop()
subexpression = str(first_operand) + ' ' + operator + \
' ' + str(second_operand)
if round == 0:
expression = '(' + subexpression + ')'
else:
expression = '(' + expression + ' ' + operator + \
' ' + str(second_operand) + ')'
round += 1
if operator == '+':
stack.append(first_operand + second_operand)
elif operator == '-':
stack.append(first_operand - second_operand)
elif operator == '*':
stack.append(first_operand * second_operand)
elif operator == '/':
stack.append(first_operand / second_operand)
else:
stack.append('Error [Invalid Operator]: ' + subexpression)
break
result = str(stack.pop())
if 'Error' in result:
return result
else:
return expression + ' = ' + result
print(evaluate(stack_normal))
print(evaluate(stack_error))
# Using List as Queues
from collections import deque
queue = deque(["(", "c", "+", "d", ")"])
print(queue)
queue.append('/')
queue.append('d')
print(queue)
queue.appendleft('*')
queue.appendleft('a')
print(queue)
# List Comprehensions
drinks = [' Beer ', ' Tea', 'Coca Cola ', ' Pepsi', 'Water']
trimmed_drinks = [drink.strip()
for drink in drinks] # trim all trailing spaces
print(drinks)
print(trimmed_drinks)
# filter drinks whose name length is longer that or equal to 5
print([drink for drink in trimmed_drinks if len(drink) >= 5])
foods = ['rice', 'Meat', 'vegetables', 'Eggs']
menus = [(food.upper(), drink.lower())
for food in foods for drink in trimmed_drinks]
print(menus)
vector = [
[1, 2, 3],
['Monday', 'Tuesday', 'Wednesday'],
['Morning', 'Afternoon', 'Night']
]
# [1, 2, 3, 'Monday', 'Tuesday', 'Wednesday', 'Morning', 'Afternoon', 'Night']
flatten_vector = [el for row in vector for el in row]
print(flatten_vector)
# [
# [1, 'Monday', 'Morning'],
# [2, 'Tuesday', 'Afternoon'],
# [3, 'Wednesday', 'Night']
# ]
transposed_vector = [[row[i] for row in vector] for i in range(3)]
print(transposed_vector)