Skip to content
Snippets Groups Projects
Commit 4dca6ac7 authored by Ashwin Maran's avatar Ashwin Maran
Browse files

lectures 23, 24, 25

parent 7735eabd
No related branches found
No related tags found
No related merge requests found
Showing
with 5515 additions and 0 deletions
File added
File added
%% Cell type:markdown id:2e3d10aa-07d4-47ff-a27d-b6170b3890de tags:
# Problem A
%% Cell type:code id:7c56f9e4 tags:
``` python
words = ["pineapple", "mango", "quince", "blueberry", "orange"]
```
%% Cell type:markdown id:811de0bb-38ae-4b8b-b5f4-a870290ba1e0 tags:
#### 1. Use comprehension to create a list of the words that contain "o"
%% Cell type:code id:817f579f tags:
``` python
[w for w in words if "o" in w]
```
%% Output
['mango', 'orange']
%% Cell type:markdown id:6f25ea46-e7d7-4951-a52d-434cfc5c4640 tags:
#### 2. Use comprehension to create a list of words that have a length > 7
%% Cell type:code id:4dd182dd tags:
``` python
[x for x in words if len(x) > 7]
```
%% Output
['pineapple', 'blueberry']
%% Cell type:markdown id:1ca08482-6770-4967-9bc2-4133660fc8d9 tags:
#### 3. Use comprehension to create a list of integers that represent the length of each word
%% Cell type:code id:d3712c7a tags:
``` python
[len(word) for word in words]
```
%% Output
[9, 5, 6, 9, 6]
%% Cell type:markdown id:d39da88d-addb-4925-bdec-faeefe35ae4d tags:
#### 4. Use comprehension to create a list of words that end with "e"
%% Cell type:code id:6126ab6e tags:
``` python
[w for w in words if w.endswith("e")]
```
%% Output
['pineapple', 'quince', 'orange']
%% Cell type:markdown id:0e58c9c2-7022-4e1f-9f04-dfddc4f2a2ec tags:
# Problem B
%% Cell type:code id:bcbab6a8 tags:
``` python
heart_rates = {"Micah": [67, 59, 84, 88],
"Briana": [59, 73, 67, 80, 79],
"Jaren": [67, 84, 71, 68 , 70]}
```
%% Cell type:markdown id:ad83760f-67f6-4ec5-92cf-489bdab95d0d tags:
#### 1. Use comprehension to create a list of the names
%% Cell type:code id:14983b5f tags:
``` python
[p for p in heart_rates]
```
%% Output
['Micah', 'Briana', 'Jaren']
%% Cell type:markdown id:9de2905b-d411-4923-9383-b6f983d1e4ab tags:
#### 2. Use comprehension to create a dictionary where the key is the same key, but the value is the min of each list
%% Cell type:code id:f080b33a tags:
``` python
{p: min(heart_rates[p]) for p in heart_rates}
```
%% Output
{'Micah': 59, 'Briana': 59, 'Jaren': 67}
%% Cell type:markdown id:3edb9d88-544c-4913-9966-a01285483c8e tags:
#### 3. Use comprehension to create a dictionary where the key is the same key, but the value is the average of each list
%% Cell type:code id:52198994 tags:
``` python
{p: sum(heart_rates[p]) / len(heart_rates[p]) for p in heart_rates}
```
%% Output
{'Micah': 74.5, 'Briana': 71.6, 'Jaren': 72.0}
%% Cell type:markdown id:26f48b37-27dc-4fa6-86f6-21ac3d29c902 tags:
# Problem C
%% Cell type:code id:b4c58448 tags:
``` python
player_stats = [
{"name": "Rina", "goals": 17, "position": "Midfield"},
{"name": "Charlie", "goals": 6, "position": "Defender"},
{"name": "Heather", "goals": 20, "position": "Midfield"}
]
```
%% Cell type:markdown id:54425847-6823-496d-b8aa-ebc31528f87a tags:
#### 1. Use comprehension to create a list of names of people who scored >10 goals
%% Cell type:code id:cc6b8524 tags:
``` python
[person['name'] for person in player_stats if person['goals'] > 10]
```
%% Output
['Rina', 'Heather']
%% Cell type:markdown id:be5fb26a-a6ba-4754-9ca9-0d650ef228ee tags:
#### 2. Use comprehension to create a list of all unique positions
%% Cell type:code id:58abdf9e tags:
``` python
list({person['position'] for person in player_stats})
```
%% Output
['Midfield', 'Defender']
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment