简体   繁体   English

将字符串附加到for循环中的列表

[英]Appending strings to a list in for loop

Say I have a text file like that where every new letter starts on a new line: 假设我有一个文本文件,其中每个新字母都以新行开头:

-.H
-.e
-.l
-.l
-.o

I would like to write a code to remove some data from each text line (in the case of example I would like to remove '-.' in front of each letter) and then concatenate the result back together to form: 我想编写一个代码来从每个文本行中删除一些数据(在示例中我想删除每个字母前面的' - 。')然后将结果连接在一起形成:

Hello

What would be a general approach to this kind of problem? 这种问题的一般方法是什么? I thought it could be solved in a following way: 我认为可以通过以下方式解决:

f = open('hello.txt', 'r')
logs = f.readlines()
f.close()
loglist = list(map(str.strip, logs))
newlist = []

for i in range (len(loglist)):
    splitLetter = loglist[i].split('.')
    letter = splitLetter[-1]
    newlist.append(letter)
    word = ''.join(newlist)
    print word

The problem though is that the result is a series of iterations: 但问题是结果是一系列迭代:

H
He
Hel
Hell
Hello

I need only the last result. 我只需要最后的结果。 How do I get that? 我怎么做到的?

The problem with your current code is that you are printing after every iteration. 当前代码的问题在于每次迭代后都要打印。 By moving the print statement outside of the for-loop it will print only the last iteration. 通过移动print语句外的for循环,将只打印最后一次迭代。

f = open('hello.txt', 'r')
logs = f.readlines()
f.close()
loglist = list(map(str.strip, logs))

word = ''.join(l.split('.')[-1] for l in loglist)
print word

Just to make sure this works, I tested it out with a test file with the following text: 为了确保这一点,我用一个带有以下文本的测试文件对其进行了测试:

-.G
-.o
-.o
-.d
-.b
-.y
-.e

and got the following result: 得到以下结果:

Goodbye

Just move the printing outside of the loop: 只需在循环外移动打印:

for i in range (len(loglist)):
    splitLetter = loglist[i].split('.')
    letter = splitLetter[-1]
    newlist.append(letter)

word = ''.join(newlist)
print word
for i in range (len(loglist)):
    splitLetter = loglist[i].split('.')
    letter = splitLetter[-1]
    newlist.append(letter)
word = ''.join(newlist)
print word

or 要么

word = ''
for i in range (len(loglist)):
    splitLetter = loglist[i].split('.')
    letter = splitLetter[-1]
    word += letter
print word
word = ''.join([ l.split('.')[-1] for l in loglist ])

而不是整个for循环应该做。

You are printing the result in every step. 您将在每个步骤中打印结果。 Simply print after the for loop. 只需在for循环后打印即可。

You can use generator expressions for shorter program: 您可以使用生成器表达式来缩短程序:

with open('hello.txt') as logs:
    word = ''.join(l.strip().split('.')[-1] for l in logs)
print word

Using with ... as statement frees you from closing the file. 使用with ... as语句可以使您无法关闭文件。 It will be automatically closed at the end of the block. 它将在块结束时自动关闭。

Then, in one single line: word = ''.join([l.strip()[-1]] for l in logs) 然后,在一行中: word = ''.join([l.strip()[-1]] for l in logs)

You don't need to split the string if you strip the string before. 如果先删除字符串,则无需拆分字符串。 You can be sure the letter will be at the last index. 您可以确定该字母将位于最后一个索引处。

Full code : 完整代码

with open('hello.txt', 'r') as logs:
    word = ''.join([l.strip()[-1]] for l in logs)
print word

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

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