无法将条件结果附加到新列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将条件结果附加到新列表相关的知识,希望对你有一定的参考价值。
我对Python或任何编程语言都很陌生。
以供参考:
- 分配
- 我的守则
- 题
1.)我的课程的分配是这样的:从文本文件'word.txt'中读取并将第一个文本文件中符合条件的单词写入新的文本文件。例如,如果函数是getListEnd('s','word.txt','word1.txt'),并且标准是ofile中的单词必须以字符's'结尾,那么我的ifile word.txt = [苹果,胡萝卜,奶酪,芥末,问题]将被分类为一个单词word1.txt = [苹果,胡萝卜,问题]
2.)这是我的代码:
mf1 = open("/Users/XXXX/word.txt", "r")
mf2 = open("/Users/XXXX/word2.txt", "w+")
mylist1=[] #list for storing word.txt strings
mylist2=[] #list for storing sorted strings
def sort(c): #function for sorting mylist1 for
for i in mylist1: criteria into my list2
if c==i[-1]:
mylist2.append(i)
def getListEnd(c, ifile, ofile):
for s in mf1: #loop for appending word.txt to list 1
mylist1.append(s)
print(len(mylist1)) #check if mylist1 contains word.txt
sort(c) #append sorted words to list2
with open('word2.txt', 'w+') as mf2: #write into word1.txt list2
for listitem in mylist2:
mf2.write('%s
' % listitem)
getListEnd('s', 'word.txt', 'word2.txt')
3.)所以问题是:为什么每当我尝试追加到mylist2时,排序的单词都没有发生?也就是说,当我检查mylist2的长度时,即使word.txt文件中有很多以s结尾的单词,它也会显示为0。我知道检查myList1时出现的错误是以最后一个字母's结尾的字符,但我不知道是什么,我无法将其附加到mylist2。
您的计划有两个问题:
- 在对输入进行排序之前,您正在写入输出文件
- 您在字符串中包含换行符,因此当您比较实际比较换行符的最后一个字符时。
这是您的程序,其中包含有关更改的注释:
# mf1 = open("word.txt", "r") # Don't do this here. You read it in in your with block anyway.
# mf2 = open("word2.txt", "w+"). # Don't do this here. You write it in your with block anyway
mylist1=[]
mylist2=[]
def sort(c):
for i in mylist1:
if c==i[-1]:
mylist2.append(i)
def getListEnd(c, ifile):
with open(ifile, "r") as mf1:
for s in mf1: # the strings will still contain the newlines if read this way
# so when you check the end of the string you'll actually be comparing
# a newline character.
if "
" in s:
mylist1.append(s[:-1]) # if there's a newline character,
# don't include it when appending to the list
else:
mylist1.append(s)
print("mylist length: ", len(mylist1))
print("mylist1: ", mylist1)
sort(c)
getListEnd('s', 'word.txt') # you don't need to pass it any info about the output file
print("mylist2: ", mylist2)
## Your writing block has to come after you've sorted it! Not before
with open('word2.txt', 'w+') as mf2:
for listitem in mylist2:
mf2.write('%s
' % listitem)
More generally:
要创建仅包含以字母“s”结尾的项目的列表,您只需执行以下操作:
input_list = ['apples', 'carrot', 'johns', 'list']
output = [i for i in input_list if "s" == i[-1]]
print(output)
>> ['apples', 'johns']
线output = [i for i in input_list if "s" == i[-1]
用input_list
迭代你的i
列表并检查每个项目以查看每个字符串i[-1]
的最后一个字符是否是字符's'
。然后它返回一个列表,其中包含与条件匹配的所有项目。一种更具表现力的编码方式是:
for word in input_list:
if word[-1] == "s":
output.append(word)
通过读取和写入文件,在with
操作符中使用python更安全,因为它可以确保您没有未关闭的文件/更安全范围。
A simpler version:
这是我认为你想做的玩具程序:
with open("word.txt", "r") as f:
mylist1 = f.read().split("
") # splits the input file around newlines
mylist2 = [i for i in mylist1 if "s" == i[-1]]
with open("word2.txt", "w") as f:
for word in mylist2:
f.write(f"{word}
") # writes word into the file
如果word.txt是:
john
apples
carrots
lewis
然后word2.txt变成:
apples
carrots
lewis
这是一个例子:
with open('words1.txt', 'w') as f_object:
f_object.write("Apples" + "
")
f_object.write("Rubles" + "
")
f_object.write("Nope" + "
")
f_object.write("Nah" + "
")
f_object.write("Georges" + "
")
f_object.close()
print("Done")
with open('words1.txt', 'r') as f:
lines = f.readlines()
with open('words2.txt', 'w') as f_ob:
for line in lines:
line = line.rstrip()
if line[-1] == 's':
f_ob.write(line + '
')
print("Done")
我们可以简单地指出,如果单词以's'结尾,我们将把它添加到新的.txt文件中。例如,当我们遍历每一行时,我们可以获取字符串中的最后一个索引。 “if”该行的最后一个索引等于s,那么我们显然可以将该行写入该文件。如果不是,它将跳过。 words2.txt文件中的输出将是:
Apples
Rubles
Georges
从带有for x in file
循环的文件中读取行会在每行的末尾为您提供换行符。
因此,i[-1]
是换行符,而不是单词的最后一个字母。
以上是关于无法将条件结果附加到新列表的主要内容,如果未能解决你的问题,请参考以下文章
一旦单击带有 in 片段的回收器列表项,如何将片段意向活动,以及如何获取回收器项目值?