Python Forum
Functions to get all latitudes given a list of listing_ids
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions to get all latitudes given a list of listing_ids
#1
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
Reply
#2
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"), ...]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I end up getting this error:
Error:
TypeError Traceback (most recent call last) <ipython-input-96-e3f656560ac7> in <module> ----> 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
Reply
#4
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 <module> 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
Reply
#5
Thank you!! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,022 Nov-21-2019, 10:06 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020