► Write better & cleaner code using Python’s advanced features
In this Python tutorial you’ll discover another basic of functional programming, namely how to use the “reduce()” function to transform data structures.
We’ll take an example data set represented using an immutable data structure from the previous videos in this series, and then we’ll create a “reduced” or “derived” output from that data using Python’s built-in reduce function.
“reduce()” (or “functools.reduce()” on Python 3) is one of the functional programming primitives or building blocks available in Python and it’s useful in a number of contexts.
Later in the video you’ll also see how the reduce function lets you group your data set into arbitrary categories. You’ll also learn about Python’s “defaultdict” class defined in the collections module, as well as some useful helpers in the “itertools” module, like “itertools.groupby”.
This video is part of a series of Python functional programming tutorials that I’m recording so stay tuned for the next instalment.
Be sure to check out these additional articles and Python tutorials if you want to dive deeper into functional programming:
* Immutable Data Structures in Python:
*
* List comprehensions:
FREE COURSE – “5 Thoughts on Mastering Python”
SUBSCRIBE TO THIS CHANNEL:
* * *
► Python Developer MUGS, T-SHIRTS & MORE:
► PythonistaCafe – A peer-to-peer learning community for Python developers:
FREE Python Coding Tutorials & News:
» Python Tutorials:
» Python News on Twitter:
» Weekly Tips for Pythonistas:
» Subscribe to this channel:
Nguồn: https://shaarique.com/
Xem thêm bài viết khác: https://shaarique.com/cong-nghe/
Xem thêm Bài Viết:
- Smart tivi Sony 4K KD-55X9300E – Thông minh đến không ngờ | Điện máy XANH
- Mở hộp TV Samsung 65 inch 65NU7400 – UNBOXING TV Samsung NU7400 65 INCH
- [Hieuhien.vn] Cách học lệnh remote Android Tv Box với remote Tivi
- How to Use your Mobile Phone as a Samsung TV Remote
- MYFREEVIEW : Remote dekoder MYTV rosak? Remote mytv percuma dan mytv advance sama tak?
Joel S
July 3, 2020tonnes of great stuff in this playlist. Thank You!
Can
July 3, 2020In your final version of the reduce function implementation you wrote this line:
lambda acc, val: {**acc, **{val.field: acc[val.field] + [val.name]}}, …
When you could do this:
lambda acc, val: {**acc, val.field: acc[val.field] + [val.name]}, …
Am I missing something?
James Lovering
July 3, 2020Oh that took me a few minutes to sink in how that works.
Hugo Santos
July 3, 2020what tools are you using to develop? the intellisense you get is so clean.
Stanislav Smoltis
July 3, 2020did someone notice that there is no Maria under physics field in scientists_by_field5 snippet? This is because the iterable for itertools.groupby() must be sorted to produce correct result. You can also make it more readable by unpacking groupby() result inside of the dictionary comprehension
scientists_by_field5 = { k : list(map(lambda x: x.name, g)) for k, g in itertools.groupby(sorted(scientists, key=lambda x: x.field), lambda x: x.field) }
Fahrudin Halilović
July 3, 2020Look at this solution with empty dict and lambda function :
scientist_by_field = reduce(
lambda acc, val: acc[val.field].append(val.name) if val.field in acc else acc.update({val.field:[val.name,]}) or acc,
scientists,
{}
)
Lucas Ribeiro
July 3, 2020The defaultdict tip was awesome!
1s9b8b7
July 3, 2020My attempt to get scientists_by_field using lambda:
reduce(lambda acc, val: (acc[val.field].append(val), acc)[1], scientists, defaultdict(list))
I think it's more readable and it should be faster than Dan's solution, since every iteration creates 1 tuple and updates existing dictionary, while he creates 2 new dictionaries. I wish I came up with upacking solution tho.
Night Crawler
July 3, 2020Hey Dan,
As always, great video. Would you please do consider video on search, trees and linked list in future please. I found lot of sources but they are very hard to understand in programming way. Thank you.
Tadas Talaikis
July 3, 2020Excellent.