The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Order of list?
Discuss Order of list? in the Python Programming forum on Dev Shed. Order of list? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 11th, 2012, 10:31 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
|
Order of list?
klskdl
|

November 11th, 2012, 11:51 PM
|
|
Contributing User
|
|
Join Date: Sep 2010
Posts: 64
Time spent in forums: 1 Day 22 h 45 m 54 sec
Reputation Power: 3
|
|
I think you have a few more things wrong than you think  . I looked through the directions and it requires that no duplicates appear in the list and only movies that are at least one customer's top two picks are displayed. What you wrote doesn't meet either of those two requirements or the one you're asking about.
Here's a simple algorithm that gets the job done
python Code:
Original
- python Code |
|
|
|
def get_most_popular(movies_in_stock, customer_list): result = [] for movie in movies_in_stock: requested = False; for customer in customer_list: movies = customer_list[customer] if movie in movies: if movies.index(movie) <= 1: requested = True break if (requested): result.append(movie) print(result)
This loops through each movie in stock. It then checks is that movie is one of the top 2 choices of at least 1 customer. If both of the prior conditions are true, it then adds the movie to the variable 'result'. Now, the output is stored in the order needed so there's no need to sort it, only movies that are one of a customer's top two choices are selected, and no duplicates will occur.
I think I explained everything, but if something is still confusing you just ask and I should respond.
|

November 11th, 2012, 11:56 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
Can you explain why you made the variable requested eqal to True or False?? that part is confusing me,
And i am aware that we weren't supposed to have duplicates, but i didnt exactly know what a duplicate was.. which is what made me think what i did was right
So can you explain those two things to me,
Thank you.
Also, for functions like these, i see that i was supposed to loop through both parameters, and compare them, but wouldnt that give some sort of error?
|

November 12th, 2012, 02:34 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by JonthnC Here's a simple algorithm that gets the job done |
Or even simpler:
python Code:
Original
- python Code |
|
|
|
def get_most_popular(movies_in_stock, customer_list): result = [] for movie in movies_in_stock: for customer, top_movies in customer_list.items(): if movie in top_movies[:2] and movie not in result: result.append(movie) return result
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

November 12th, 2012, 02:40 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
Thank You for all your help..
I also wanted some more help.
I am on another function.. that i think i am ding right, but can you tell me if i am on the right track??? Because i kind of wasnt last time.
For this function i am taking a file.txt and looping through it. then i have to put it in a list of list of strings: Can you let me know if i am on the right track, and if not what am i doing wrong? And does my iff condition need to include something like:
if next_item != '' or next_item != None:
result.append/??
Code:
def build_grocery_list(file):
'''(io.TextIOWrapper) -> list of list of str
The parameter is an open file containing groceries to purchase from a grocery
store with one grocery item per line. To make it easier to shop for items, the groceries
are separated into "grocery groups", and these groups are separated by exactly one
blank line. Download the sample file foods.txt and notice that there is a blank line
between grocery groups. The function returns a list of lists, where each inner list
contains the groceries from one grocery group. Your inner lists are required to be
in the same order as in file, and there should not be any newline characters anywhere
in your list. Your function must work for any file of this format, not just the sample we
have given you.
>>>build_grocery_list(open("foods.txt", "r"))
[['milk', 'cheese', 'cream cheese', 'eggs'], ['bread', 'buns', 'pita']
'''
result = []
counter = 0
string = ' '
for next_item in file:
next_item = next_item.strip()
result.append(next_item)
print(result)
Example of text file =
milk
sugar
candy
sour cream
onions
chips
bananas
apples
oranges
mango
|

November 12th, 2012, 06:18 AM
|
 |
Contributing User
|
|
|
|
Whoever wrote this assignment does good work. Stay with it. We learn that "The parameter is an open file". Usually a great technique. Testing is easier because (as in the example) you can pass TextIOWrapper for testing. Or the source could be somewhere on the internet. The function is more general.
The function comes with a doctest. Learn to run doctests. Learn to write doctests. Write the tests before you write the function body.
python -m doctest your_program.py
runs the doctests.
The assignment says that your function should return a list. Your function doesn't have a return statement. Therefore it returns None. None is not a list. A doctest to ensure you've returned the value:
Code:
'''
>>> a = build_grocery_list(open("foods.txt", "r"))
>>> a == [['milk', 'cheese', 'cream cheese', 'eggs'], ['bread', 'buns', 'pita']
True
'''
(Note also that the doctest you posted is missing a space character in the python prompt.
As for your code, it hasn't got a test for the blank lines between groups, and to make nested lists you'll need a pattern equivalent to
Code:
result=list()
for whatever:
subList = []
result.append(subList)
for whichever:
subList.append(someobject)
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|