Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Functions to get all latitudes given a list of listing_ids Rae Programmer named Tim Posts: 5 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #1 Jun-14-2019, 11:50 AM (This post was last modified: Jun-14-2019, 11:51 AM by Rae. Edited 1 time in total.) Hi all! Would really appreciate some help here. This is part of my homework (and I am still super new to Python so I need some help). So I have a set of data, and I'm supposed to get all altitudes given a list of listing. Here's my code so far but I have no idea what I'm doing wrong so would appreciate some help! I was given the following hints and I did my code shown after that. Hint Create a results list Extract the latitude and listing id of each row Check listing id exists within the all listings If true, append the latitude into the results list Return results list Input: airbnb_data as data, a list of listing_ids Return: A list of latitudes ```def get_all_latitude(airbnb_data): results = [] for row in range(airbnb_data): listing_id = row[1] latitude = row[17] if listing_id in airbnb_data: results.append(row) return results``` DeaD_EyE Giant Foot Posts: 863 Threads: 5 Joined: May 2017 Reputation: 72 Likes received: 185 #2 Jun-14-2019, 12:45 PM What is your problem? We have no information about input format, which can differ. If the indices are all right and if there are no rows inside your dataset, which have lesser cols, then it should work. You have to recognize, that indexing begins with 0. If you don't know on which position is what, you could use enumerate. ```def get_one_row(airbnb_data): first_row = airbnb_data[0] # if airbnb is already a list with rows # if it's text, use the method splitlines on it and then take the first element. for idx, column in enumerate(first_row.split()): print(idx, column)```Then you see [(0, "your_column 1"), (1, "your_column 2"), ...] My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Rae Programmer named Tim Posts: 5 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #3 Jun-14-2019, 01:08 PM (This post was last modified: Jun-14-2019, 01:17 PM by Yoriz. Edited 1 time in total. Edit Reason: Added error tags ) I end up getting this error: ``````Error: TypeError Traceback (most recent call last) in ----> 1 get_all_latitude(airbnb_data, ['10350448','13507262','13642646']) TypeError: get_all_latitude() takes 1 positional argument but 2 were given``````Oof I haven't learnt enumerate so I'm going to have to check about that Yoriz Posts: 921 Threads: 22 Joined: Sep 2016 Reputation: 76 Likes received: 216 #4 Jun-14-2019, 01:24 PM (This post was last modified: Jun-14-2019, 01:33 PM by Yoriz. Edited 1 time in total.) Your function has one parameter, so you can pass it one argument like as follows. ```def get_all_latitude(parameter1): print(parameter1) get_all_latitude('first argument')`````Output:first argument``but you are trying to pass it two arguments and getting an error like below ```def get_all_latitude(parameter1): print(parameter1) get_all_latitude('first argument', 'second argument')`````````Error:Traceback (most recent call last): File "c:/Users/Dave/Documents/VS Code WorkSpaces/Python Forum/general/forumpost2.py", line 32, in get_all_latitude('first argument', 'second argument') TypeError: get_all_latitude() takes 1 positional argument but 2 were given``````You should either only be passing one argument or add another parameter to your function like below ```def get_all_latitude(parameter1, parameter2): print(parameter1, parameter2) get_all_latitude('first argument', 'second argument')`````Output:first argument second argument`` Rae Programmer named Tim Posts: 5 Threads: 2 Joined: May 2019 Reputation: 0 Likes received: 0 #5 Jun-14-2019, 02:07 PM Thank you!! :) « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post making a dictionary from a list, one key with multiple values in a list within a list rhai 4 502 Oct-24-2018, 06:40 PM Last Post: LeSchakal

Forum Jump:

Users browsing this thread: 1 Guest(s)