0

“地址文件”

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

我放在???它旁边的是令人困惑的部分。有人可以澄清一下吗?

4

2 回答 2

1
def line_split(line):
    ls = line.split()
    return [ls[0],' '.join(ls[1:-1]), ls[-1]]

with open(datafile) as fin:
    address_list = [ line_split(line) for line in fin ]
    #address_list = map(line_split,fin) # would also work too.

解释问题标记的行:

for i in range(1, len(parts) - 1):

这会遍历列表中的索引,但会跳过第一个和最后一个索引。一种更惯用的方法是:

for part in parts[1:-1]:

然后您将在循环中稍后替换parts[i]为。part

name += parts[i] + ' ' # <--- ??

这需要name并添加parts[i]到它和一个' '. 换句话说,它与以下任何内容相同:

name = name + parts[i] + ' '
name = "%s%s "%(name,parts[i])
name = "{0}{1} ".format(name,parts[i])

和行:

address.append(parts[-1]) # <---???

将部件列表的最后一部分附加到address列表中。

于 2012-12-09T04:14:19.020 回答
0

也许这会有所帮助:

>>> line = '100 Main Street'
>>> parts = line.split()
>>> name = ''
>>> len(parts)-1
2
>>> for i in range(1,2):
...    print parts[i] + ' '
...    print parts[-1]
...
Main # <-- there is an extra space here after 'Main'
Street
于 2012-12-09T04:46:01.420 回答