Python:计算长度为3的姓氏,并将其分配给;答案是「;变量

Python:计算长度为3的姓氏,并将其分配给;答案是「;变量,python,python-3.x,for-loop,Python,Python 3.x,For Loop,新手Python用户在这里 infile = open('last.txt','r' ) content = infile.read() wordList = content.split() count = 0 for i in wordList: if '.' not in i and len(i) == 3: count += 1 answer = count print(answer) 我有一个文件last.txt,我从中提取信息,它显示了美国最常见的姓氏,以

新手Python用户在这里

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
我有一个文件last.txt,我从中提取信息,它显示了美国最常见的姓氏,以及整数/小数形式的百分比频率。我希望能够检索三个字母长的姓氏数量并将其分配给answer变量,但我遇到了一些困难。我的尝试代码显示如下所示:

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if i != str:
        count += 0
    elif i == len(3):
        count += 1
    else:
        count += 0
answer = count
print(answer)
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
每当我执行代码时,结果都是“0”,这是不正确的,因为.txt文件中有多个3个字符长的名称

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
我正在使用的文本文件可以下载

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
编辑:

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
下面是上面链接的文件的前几行。请注意,这些列由选项卡分隔

SMITH   0.88085
JOHNSON 0.68844
WILLIAMS    0.56866
BROWN   0.51162
JONES   0.50517
MILLER  0.41807
DAVIS   0.39751
GARCIA  0.31817
RODRIGUEZ   0.29813
WILSON  0.29027
MARTINEZ    0.28732
ANDERSON    0.28262
TAYLOR  0.26704
THOMAS  0.26345
HERNANDEZ   0.26185
MOORE   0.259
MARTIN  0.24937
JACKSON 0.24693
THOMPSON    0.23887
WHITE   0.23707
LOPEZ   0.2304
LEE 0.22459
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
你需要:

elif len(i) == 3:
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)

这将检查
i
的长度是否等于3。

如果没有可复制的数据,这是很难做到的。但是首先,如果你要做的只是count+=0,不要使用条件语句,因为这与什么都不做是一样的。所以,你什么都不要做!同样,len(3)应该产生一个错误。“int”对象没有len()!你应该改为做len(i)==3。因此:

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if len(i) == 3:
        count += 1
answer = count
print(answer)
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)

如果i!=str:始终计算为True,因为以下代码打印为True:

i = "ABC"
print i != str
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
顺便说一句,如果您想测试变量i是否是字符串,请使用
isinstance(i,str)
但您不需要在程序中进行测试

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)
这是您修改过的代码:

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()
count = 0 
for i in wordList:
  if i == len(3):
    count += 1
print(count)  # this will print how many times a 3-letter word was encountered
infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)

要获取名称列表,请在输入
for
循环之前附加
answer=[]
,然后如果我不在answer:answer中,则添加
。将(i)
附加在
计数+=1之后(在同一级别上)。您也可以在打印变量
count

后执行
print(answer)
,尝试以下代码:

infile = open('last.txt','r' )
content = infile.read()
wordList = content.split()

count = 0

for i in wordList:
    if '.' not in i and len(i) == 3:
        count += 1
answer = count
print(answer)

使用
如果len(i)==3
,而不是
如果i==len(3)
。这是一个程序的工作版本(76字节,一行):
将open('last.txt','r')作为f:print(sum(i==len(3)表示f.read().split()中的i))