Preface
Goal: Continue Part One
4: Data Structure Using Named Tuple
There is other alternative data structure as well.
Instead of dictionary
, we can use named tuple
.
Named Tuple
The declaration is simple.
import pprint
from collections import namedtuple
Song = namedtuple('MySong', ['title', 'tags'])
song = Song('Cantaloupe Island',
['60s', 'jazz'])
pprint.pprint(song)
With the result as below:
❯ python 12-record.py
MySong(title='Cantaloupe Island', tags=['60s', 'jazz'])
we can examine the object shown in output result above.
The Songs Structure
We can continue our journey to records just using dictionary
.
No need any complex structure.
from pprint import pprint
from collections import namedtuple
Song = namedtuple('MySong', ['title', 'tags'])
songs = [
Song('Cantaloupe Island', ['60s', 'jazz']),
Song('Let It Be', ['60s', 'rock']),
Song('Knockin\' on Heaven\'s Door',
['70s', 'rock']),
Song('Emotion', ['70s', 'pop'])),
Song('The River', None)
]
pprint(songs)
With the result similar as below record:
❯ python 13-songs.py
[MySong(title='Cantaloupe Island', tags=['60s', 'jazz']),
MySong(title='Let It Be', tags=['60s', 'rock']),
MySong(title="Knockin' on Heaven's Door", tags=['70s', 'rock']),
MySong(title='Emotion', tags=['70s', 'pop'])),
MySong(title='The River', tags=None)]
5: Separating Module
Again, we need to reuse the songs record multiple times, so we separate the record structure from logic.
Songs Module
The code can be shown as below:
from collections import namedtuple
Song = namedtuple('MySong', ['title', 'tags'])
songs = [
Song('Cantaloupe Island', ['60s', 'jazz']),
Song('Let It Be', ['60s', 'rock']),
Song('Knockin\' on Heaven\'s Door',
['70s', 'rock']),
Song('Emotion', ['70s', 'pop']),
Song('The River', None)
]
Using Songs Module
Now we can have a very short code.
from pprint import pprint
from MySongs import songs
pprint(songs)
With the result as below.
❯ python 14-module.py
[{'tags': ['60s', 'jazz'], 'title': 'Cantaloupe Island'},
{'tags': ['60s', 'rock'], 'title': 'Let It Be'},
{'tags': ['70s', 'rock'], 'title': "Knockin' on Heaven's Door"},
{'tags': ['70s', 'pop'], 'title': 'Emotion'},
{'title': 'The River'}]
3: Finishing The Task
Extract, Flatten, Unique
Extracting NamedTuple
Using dot to access value
from pprint import pprint
from MyTupleSongs import songs
tagss = [
song.tags for song in songs
if song.tags
]
pprint(tagss)
With the result is,
still list
of list
as shown below.
❯ python 15-extract.py
[['60s', 'jazz'], ['60s', 'rock'], ['70s', 'rock'], ['70s', 'pop'], None]
Unique
Advance List Comprehension
Finally we solve unique
list,
after flattening.
from MyTupleSongs import songs
tags = [
tag for song in songs
if song.tags
for tag in song.tags
]
print(list(set(tags)))
With the result similar as below array:
❯ python 17-unique.py
['pop', 'rock', '60s', '70s', 'jazz']
Also short.
What is Next 🤔?
We have alternative way to do get unique list. And we will also discuss about concurrency.
Consider continue reading [ Python - Playing with Records - Part Three ].