简体   繁体   English

append 要列出的多个项目,它们之间有一个空格

[英]append multiple items to list with a space between them

I'm trying to write a code that will take multiple strings at specific indexes of a list, and append them to a list as follows我正在尝试编写一个代码,它将在列表的特定索引处获取多个字符串,并将它们 append 放入列表中,如下所示

['Caleb', 'Smith'] ===> ['Caleb Smith']

but I can't figure out how to do that, what I keep getting is但我不知道该怎么做,我一直得到的是

['Caleb', 'Smith'] ===> ['CalebSmith']

code:代码:

with open("student_data.txt", "r") as file1:
    SDN = file1.readlines()
    SD = []
    for line in SDN:
        without_newline = line[:-1]
        SD.append(without_newline)

SDf = []
id = []
first = []
last = []
adress = []

for x in range (0, 10):
    SDf.extend(SD[x].split(' '))

for x in range (0, 80, 8):
    id.append(SDf[x])
    first.append(SDf[x + 1])
    last.append(SDf[x + 2])
    adress.append(SDf[x + 3] + SDf[x + 2])

data for student file:学生档案数据:

125001 John Smith 12 First Road London N1
125002 Alan Smith 35 Second Avenue London N12
125003 Mary Jones 258 Nice Road London E17
125004 Elisabeth Davies 33 Third Avenue London SW19
125005 Mark Henry 45 Lovely Gardens London W8
125006 Philip May 1 Near Street London N1
125007 Jane Gill 7 Fifth Street London EC1
125008 Susan Wright 23 Nowhere Road London N10
125009 Alan Lee 345 Season Street London E1
125010 Olivia Rex 12 Beautiful Road London N12

If you're just trying to concatenate the given strings with a space between them, then you can use the join method on a string (the string that is the delimiter you want to go between the items).如果您只是想将给定的字符串与它们之间的空格连接起来,那么您可以在字符串上使用join方法(该字符串是您想要在项目之间使用 go 的分隔符)。

>>> items = ['Caleb', 'Smith']
>>> " ".join(items)
'Caleb Smith'
>>> "...".join(items)
'Caleb...Smith'

Notice how the output was a string.注意 output 是一个字符串。 If you need the output to be a list containing that one string, then just put the string in a list:如果您需要 output 成为包含该字符串的列表,则只需将该字符串放入列表中:

>>> [" ".join(items)]
['Caleb Smith']
>>> l = ["Caleb", "Smith", "Edward"]
>>> i = 0
>>> j = 1
>>> " ".join(l[i:j+1])
'Caleb Smith'

Explanation:解释:

" ".join() takes a list and joins the elements with the string on which it is used. " ".join()接受一个列表并将元素与使用它的字符串连接起来。

[i:j+1] splices the list by indexes i and j [i:j+1]通过索引ij拼接列表

Let's streamline this:让我们简化一下:

with open("student_data.txt", "r") as file1:
    SDN = file1.readlines()
    SD = []
    for line in SDN:
        without_newline = line[:-1]
        SD.append(without_newline)

With:和:

with open("student_data.txt", "r") as file1:
    SDN = file1.readlines()
    SD = [line.rstrip() for line in SDN]

Now:现在:

SDf = [record.split(' ') for record in SD]

Replaces:取代:

for x in range(0, 10):
    SDf.extend(SD[x].split(' '))

And now this:现在这个:

for x in range (0, 80, 8):
    id.append(SDf[x])
    first.append(SDf[x + 1])
    last.append(SDf[x + 2])
    adress.append(SDf[x + 3] + SDf[x + 2])

Can be:可:

for x in SDf:
    id.append(x[0])
    first.append(x[1])
    last.append(x[2])
    adress.append(' '.join(x[3:]))

This joins the 4th (Python lists are indexed starting at 0 ) to the last element in each list with a space.这将第 4 个(Python 列表从0开始索引)连接到每个列表中带有空格的最后一个元素。 Yielding, for instance: '12 First Road London N1' .屈服,例如: '12 First Road London N1'


Ideally, do not manage aggregate data with "parallel" data structures, but create an aggregate data structure that holds the different pieces of related data in a single object. That is beyond the scope of this question, though.理想情况下,不要使用“并行”数据结构管理聚合数据,而是创建一个聚合数据结构,将不同的相关数据保存在单个 object 中。不过,这超出了这个问题的 scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM