【发布时间】:2018-12-06 08:29:50
【问题描述】:
number=[]
name=[]
null=[]
fh = open("foo.txt","r")
for line in fh:
words=line.split()
for word in words:
if(word=="number"):
number.append(word+1)
print(word)
word=word+2
if(word=="name"):
name.append(word+1)
word=word+2
else:
null.append(word+1)
word=word+2
print("number " " instances " " name " " instances " " null " " instances ")
print(number, len(number), name, len(name), null, len(null) )
fh.close()
这是我的最小 Python 代码。我的目标是按列打印特征(例如名称)及其实例数。我的测试文件(foo.txt)的顺序如下
name Mathew
null has
number 4
null dogs
null and
null a
null cat
我知道我的代码不正确。特别是在 append 语句和 increment 语句期间。我的问题是: 一种。正确的说法是什么? 湾。我会怎么做才能得到一个 coumnwise 输出,特别是如果有大量的单词,换句话说,我可以换成一列吗?
预期输出
number instances name instances null instances
4 1 Mathew 1 has, dogs, 5
and, a, cat
这里绝对是初学者。
【问题讨论】:
-
我给出的文本文件示例是 foo.txt 。很抱歉在那里不清楚。将编辑它。
-
感谢您的建议。会的。
标签: python printing append output