「地址文件」在這種情況下文件處理如何工作?
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
那些我放在旁邊???
它是令人困惑的部分。有人能澄清他們嗎?
是list'這裏你想要的結果了'地址? – mgilson
是的,我不知道這個過程如何工作 –
對不起,我是這個網站的新手。我會盡力跟隨。 –