“地址文件”
100 Main Street
23 Spring Park Road
2012 Sunny Lane
4 Martin Luther King Drive
“地址列表”
[['100', 'Main', 'Street'],
['23', 'Spring Park', 'Road'],
['2012', 'Sunny', 'Lane'],
['4', 'Martin Luther King', 'Drive']]
numbers_file = open("address_file.txt", "r")
def load_addresses(numbers_file):
addresses = [] # <-- Create a list for sublist
for line in numbers_file:
address = [] # <-- Create a sublist
parts = line.split() # <-- split into lists by whitespace
address.append(parts[0]) # <--- I know this will take first elements of the lists and appended (back of the list) to sublist.
name = '' # <--- name to attach such as 'Spring' 'Park' into 'Spring'
for i in range(1, len(parts) - 1): # <--- Why is the range like this? is it because we ignore first element since its already in good form and since its index we -1?
name += parts[i] + ' ' # <--- ??
address.append(name.strip()) # <--- I guess this is to wipe out whitespace front and back
address.append(parts[-1]) # <---???
addresses.append(address) # <--- append the sublist into list
return addresses
我放在???
它旁边的是令人困惑的部分。有人可以澄清一下吗?