PyCon Australia 2013

Iteradores y generadores en Python

Lakshman Prasad  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

can you hear me thank you for the introduction my name is Lakshman Prasad I work for McKinsey & Company McKinsey does management consulting I work for a division that works the consultants in order Dell our deliver technology solutions to the end clients

my primary expertise has been in the Python ecosystem and a set of tools although in the last year I have had to significantly work on JavaScript I have never done an intro-level talk particularly on Python particularly coming after a good session with Peter

and it can be hard but when I write such answers about detailed explanations on Stack Overflow I get a lot of very positive feedback so that's what I'm trying to do today yeah and by answering many of those questions I happen to be among the 1.00 5%

of the stack overflow users we're answering all questions explained particularly with regards to the basic Python so I'll try and see what I can do I had a great time of the conference by the way such a nicety well-organized such nice fun since all

of you just had lunch maybe it's a good idea we start with our a little bit of warm-up exercise for those of you that know but don't worry if you don't if you don't get though if you don't know the solutions to what we're discussing

right now the objective is at the end of the session you will be in a position to be able to so let's say we have a dictionary of states map to their capitals we need a dictionary of states map to their capitals first we have states and then we also have

capitals now here so if I want a dictionary that map's States to the capitals how would you get it you have a list of states and you have a list of capitals all right so here's a hint zip of states and capitals gives you a couple of both state as well

as capital so zip is a constructor in Python that takes a stream of pairs sorry a pair of streams and gives you a stream of pairs so if anybody knows I'm sure you learnt a constructor of a dict earlier in Peter session so if you pass a dict constructor

our pair of streams essentially what you get is a dictum that gives us the it's map to the corresponding capitals so similarly let's go to the next question we have a list this is one of the most popular courses Exede coming up we were so very so often

of being asked by a lot of people on various different channels particularly Stack Overflow I have a list of lists I want to do something on all of these how do I go about it what would you do what are the ways in which you can flatten a list or you want to

execute something on every element in multiple lists rather than one of them what would you do there's any solutions well you might have learnt it already but if you don't know Python supports operator overloading therefore some in the case of a list

is moppet to extending a list what you do is you take an empty list and then you sum all of the lists that will give you a list of all the list that you're talking about so those of those of you that are inclined functionally thinking who can think about

map and radios you can of course also use map and ideas also there's really no reason you would have to do such a kind of thing because I think there are several Python alternatives which are imperative so what if you want to do it in map hundred years

there are MapReduce solutions you take a map you pass each and every list to make it a list and operate a lot add is the same as doing plus explicitly so you have five plus four but you want to do it functionally without explicitly mentioning plus what would

you do you import the operator module and you say operator that sum and you apply that in five and four that's exactly what this is doing you mopping and you're doing a MapReduce on all of these lists in order to give you a list of lists this is one

of the way and then we will go through a lot of the details about data tools module but it's a tools module supports one important thing called chain you have a list of iterables you take a chain and to it you pass with all the different lists that you

have individually the star supports the star argument essentially you know gives individual s the four of them as argument scintillator to the chain and then that gives you a list of all of the elements in the in the list of lists similarly this is a new feature

introduced in python 2.6 essentially of what you're doing earlier you are expanding you they reduce the chain you're passing four different lists as arguments but that may not be possible your list of lists may be continuous it may be keep going on

so you don't want to expand it individually into so many different things so python 2.6 in later to smart you'll added a new thing called as it at will change or from iterable so you can pass it on a table so you don't even have to separate all

of these four lists into four lists like you read elsewhere but the most obvious the most pythonic way of doing it as long as the list is only two levels deep would be to use a multiple for loop nested for loop in the form fill is comprehension so you do for

each list and list of lists for H X in each list give it to me in a list so that's about in fact it can go multiple levels you even using this don't worry about it by the end of the session my hope is all of you will be in a position to be able to

[ ... ]

Nota: se han omitido las otras 2.716 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.