我想找出流行名称在列表中的哪一行,这样我就可以得到该名称的排名(第一,第二,第三等)。
我正在考虑做这样的事情,但是我不知道在达到流行名称后如何停止循环
x=0
for number in myfile:
x+=1
我当前的代码是
print("Enter a name to see if it is a popular girls or boys name.")
name=[]
boyfile=open('BoyNames.txt','r')
boynames=boyfile.read().splitlines()
girlfile=open('GirlNames.txt','r')
girlnames=girlfile.read().splitlines()
while name!='stop':
popularname=input("Enter a name, or enter stop to stop: ")
popularname=popularname.rstrip('\n')
if popularname=='stop':
break
if popularname in boynames:
print("{} is a popular boys name".format(popularname))
else:
print("{} is not a popular boys name".format(popularname))
if popularname in girlnames:
print("{} is a popular girls name".format(popularname))
else:
print("{} is not a popular girls name".format(popularname))
答案 0 :(得分:0)
您是否要在列表中搜索元素? Python的序列类型为此具有方法index
:
print(['Beavis', 'Butt-head', 'Daria'].index('Beavis'))