-2

I need your help, can anyone tells me that what this part of code is doing?

if line[-6:] == '[edit]':
            state = line[:-6]

and

town = line[:(line.index('(')-1)]

the whole code is :


def get_list_of_university_towns():
    '''Returns a DataFrame of towns and the states they are in from the 
    university_towns.txt list. The format of the DataFrame should be:
    DataFrame( [ ["Michigan", "Ann Arbor"], ["Michigan", "Yipsilanti"] ], 
    columns=["State", "RegionName"]  )

    The following cleaning needs to be done:

    1. For "State", removing characters from "[" to the end.
    2. For "RegionName", when applicable, removing every character from " (" to the end.
    3. Depending on how you read the data, you may need to remove newline character '\n'. '''         
    with open('university_towns.txt') as file:
        data = []
        for line in file:
            data.append(line[:-1])

#     print(data)
    state_town = []
    for line in data:
        if line[-6:] == '[edit]':
            state = line[:-6]
#             print(state)
        elif '(' in line:
            town = line[:(line.index('(')-1)]
#             print(town)
            state_town.append([state,town])
        else:
            town = line.rstrip()
            state_town.append([state,town])
#     print(state_town)
    ans = pd.DataFrame(state_town, columns = ['State','RegionName']) 
    return ans
get_list_of_university_towns()
eli
  • 184
  • 2
  • 12
  • `line[-6:]` is just the last 6 characters of `line` (a negative index is relative to the right of the string). – Tom Karzes Apr 13 '20 at 17:17

1 Answers1

1
if line[-6:] == '[edit]':
            state = line[:-6]

Is simply checking if last 6 character of given string are a [edit], if so then this value is assigned to a state var.

town = line[:(line.index('(')-1)]

Here the town var gets the value of a characters of a line that are in front of ( excluding.

EDIT: Lets split this like:

index_to_par = line.index('(')
town = line[:(index_to_par - 1)]

So index_to_par is getting index on line string of a first ( Example if we have string like some string (example) then we get a value of 12 because ( is on a 12 place of this string. Then we assing to a town a slice of this line as line[:(index_to_par - 1)] - we want to have values from begining of the string (as there is no value in front of :) to a our index_to_par - 1 (so all values before first occurence of (). You can read more about string sliceing here ---> https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3

Ishinomori
  • 229
  • 1
  • 6