Worked Exercise- Tuples and Sorting

دوره: Python Data Structures / فصل: Chapter Ten- Tuples / درس 2

Worked Exercise- Tuples and Sorting

توضیح مختصر

And now, in this little bit of lesson, we're going to talk about some tuples, and we're going to create a list of the most common words, and find out how to sort a dictionary by the values instead of by the key. And then goes through the words and does the idiom of using dictionary get to maintain the counters and we print it out at the very end. Now, instead of printing this out, we are going to, let me do this in a couple of steps, make a new tuple and I'll just call it newt equals (v,k).

  • زمان مطالعه 10 دقیقه
  • سطح متوسط

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

Hello, and welcome to Python for Everybody. I’m Charles Severance. And now, in this little bit of lesson, we’re going to talk about some tuples, and we’re going to create a list of the most common words, and find out how to sort a dictionary by the values instead of by the key. We’re going to use the clown.txt file and the intro.txt file. And I’m going to start with the code from Exercise 9 that I just did from Chapter 9. It’s not actually from the exercises, but it’s very similar to them. And I’m going to make a copy and I’m going to keep it in the same folder. I’m going to keep it in the ex 09 folder. And just call it ex 10, because this code is going to do much of the same stuff and it’s going to read these same files. And so I’ve got myself Exercise 10, Exercise 9 is still here, Exercise 10 is now what I’m editing, Exercise 10. But I’m in the Exercise 9 folder. So in Exercise 9 we looked for the most common word but we want to find the five most common words which is going to require us to sort. So I’m going to get rid of that code because it’s not really how we’re going to do it. There we manually looped through it and found the maximum. And so I’m going to just run this. cd Desktop/py4e/ex 09 and if I do an ls you see that I’ve got ex intro.txt so I’ll run python3 ex and run the clown data and we see that we see the dictionary and it’s properly making it in this code right here. That doesn’t change. It reads the file, reads all the lines, goes through and splits it into words. And then goes through the words and does the idiom of using dictionary get to maintain the counters and we print it out at the very end. So the new code we’re going to write is down here. Okay? So let’s first do a few things. If I can say x is equal to the dictionary.items. And this gives us basically a list print x, this gives us a list of the key-value pairs. This prints out the dictionary but, if we do it this way and use items it gives us the key-value pairs. Okay, and so that’s what we got. Key-value pairs. Now, we can sort this based on the value because tuples can be compared. This can be compared with this and because d is lower than r, then this one is lower, this whole, this ran tuple comes after the down tuple. So we can sort this whole thing. And I’ll do this by just putting the word sorted here and say give me a sorted version of that. Now, it’s going to do it based on the order of the tuples. This is going to be higher precedence than this. So if I print it this way, run it again, you’ll see that it’s sorted. And now is after, and, car, it’s in alphabetical order by key. And so we could actually print the first five, up to and not including five, by adding a list on the slice of this, a list slice here. And so that will show you only the first five, right? Except that that’s not what we’re trying to do. We really want to sort by this. Okay? So we have this mechanism that can take a list and sort it based on the tuple values. If we could create a list where it was 1 comma after, instead of after comma 1, and make it the exact same thing, then we could actually then sort it, and it would be fine. Okay? So let me show you a couple of ways at least one way to do that, okay? Get rid of this. We going to hand construct a list. And just call it tmp equals give me a new list. tmp equals new list. And then for k,v in the di.items. And I’ll just start by printing k,v. So we see, and this is where it’s really nice to do these with the clown code first and then only do to your test on bigger file later. And so it’s pretty much the same thing, we are going through in key-value order, which is dictionary order, which is not sorted at all, okay? Now, instead of printing this out, we are going to, let me do this in a couple of steps, make a new tuple and I’ll just call it newt equals (v,k). Okay, so I’m saying make a new tuple. This is like a new tuple with two items in it, and I’m going to make the value the key. Okay, so then I’m going to say tmp.append(newt). New tuple. So, I’m going to end up with a list of tuples, comment this one out, and under then when I’m done here I’m going to print tmp. So if I run clown.txt. You see what happens in tmp. It’s still, well let’s print tmp twice. I mean, it’s not sorted, it’s flipped, let’s print it. That’s okay. That’s the flipped one. Okay, so it’s flipped and all we did is we made it, instead of car, 3 it’s 3, car. But now we have a list. Okay? So now it’s flipped, and now we can sort that. We can say tmp equals sorted(tmp). So it says take tmp and sort it, and give it back to me. And now I’m going to say print(‘Sorted’,tmp). Okay, so here’s the first print. When we flip it, we’ve got 2 tent. But it’s not sorted at all. But after we sorted it, it’s sorted by tuple and the lowest is 1 after, so you’ll notice that 1 is the same as 1 so it checked the second item in the tuple. So down comes before after. Fell becomes after down. Intro on alphabetical order, but now we get the 2’s. So all the 1’s sort there. And then the 2’s come here, but then within the 2’s it sorts in alphabetical order because like a string if the first character matches, then it looks to the second character, and then we see oh, here we go, the 3’s, and then the one we actually wanted, the highest one, is the 7. And so one of the things we can do is we can say, you’ll notice that we want the highest one, not the lowest one. So we can just tell this, with this parameter, reverse equals true. And we just say, hey sorted, do this backwards, do it from highest to lowest, rather than lowest to highest. And now our sorted ones says 7 the, etc, okay? And so we want the first five. We can say up to but not including 5. So this is now the top five. So the sorted one is that’s the top five. If there is, it’s a tie we’re going to go in reverse alphabetical order. but let’s not worry about that too much for now. So it makes a flipped list then it sorts the flipped list. Now if I just wanted it to print it out nicer I could loop through this new list, I could say for v,k, remember this is a flipped list so the sensible thing is. What’s coming out if this list each tuple is value, key in tmp. And I’m only going to go up to, up to but not including 5, so the first five. And so I’m pulling them back out as value-key because that’s what they are. They’re value-key. See, value-key, value-key, value-key. So v is going to go through these and k is going to go through these, and then I’m just going to print(k,v). So this is kind of my flipping backwards, because I want to see them this way. And the is the most common one, car 3. And so t’s just going through this up to the fifth one and then printing them out. Okay? So let me comment this out. Let me comment that out and just delete this. So we have a dictionary. Let me comment out the dictionary. We have a dictionary, we make a list, and then we make these reversed tuples where we have the value first and the key second. We’re setting it up so the sort’s going to work. And then once it’s sorted, we have to flip them back. So we flip them for sorting from key-value to value-key for sorting. We do the sort. Then we flip them back with key-value and print them out. And it works fine, so let’s try our big file, intro.txt. And there you go, those are the five most common words in intro.txt. So you might ask yourself, why did we use tuples? We probably could’ve really used lists for this. But tuples are more efficient than lists and you notice that we weren’t going to modify. We did modify the temp list, it’s a list of tuples, but the tuples within the list we weren’t going to modify. And so we tend not to make lists if we can get away with using tuples. And so that’s why we made this flipped tuple thing, okay? So I hope that was useful to you, I hope to see you on the net.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.