如何使用 python 计算字母出现的次数?
How do I count the amount of times a letter appears using python?
我正在计算某个字母在我的列表中出现了多少次。但是,每当我使用计数函数并输入我想要计算的字母时 return = 0
这是代码:
lab7 = ['Euclid','Archimedes','Newton','Descartes','Fermat','Turing','Euler','Einstein','Boole','Fibonacci', 'Nash']
print(lab7[1]) #display longest name - a
print(lab7[10]) #display shortest name - b
c = [ word[0] for word in lab7]
#display str that consists of 1st letter from each name in list - c
print(c)
d = [ word[-1] for word in lab7]
#display str that consists of last letter from each name in list - d
print(d)
**x = input('Enter letter you would like to count here')
lab7.count('x')
e = lab7.count('x')
print(e)**
这是无效的代码部分。我不断收到 ->
Archimedes
Nash
['E', 'A', 'N', 'D', 'F', 'T', 'E', 'E', 'B', 'F', 'N']
['d', 's', 'n', 's', 't', 'g', 'r', 'n', 'e', 'i', 'h']
Enter letter you would like to count here s
0
作为我的输出。
如果你想计算给定字符在 list
中所有单词中出现的次数,那么你可以尝试:
input_char = input('Enter letter you would like to count here')
print "".join(lab7).count(input_char)
如果您希望逻辑不区分大小写,您可以使用 .lower()
将输入字符转换为小写
你首先将list
的所有元素连接起来得到一个统一的字符串,然后使用count
方法得到给定字符的出现。
lst = ['Euclid', 'Archimedes', 'Newton', 'Descartes', 'Fermat',
'Turing', 'Euler', 'Einstein', 'Boole', 'Fibonacci', 'Nash']
letter = input("Enter the letter you would like to count: ")
count = "".join(lst).lower().count(letter)
print(count)
这将连接列表中包含的所有单词并生成单个字符串。然后字符串将被降低以计算大写和小写字母(例如 A
等于 a
)。如果不应该平等对待大小写字母,可以去掉.lower()
。
检查输入是否只有一个字母:
lst = ['Euclid', 'Archimedes', 'Newton', 'Descartes', 'Fermat',
'Turing', 'Euler', 'Einstein', 'Boole', 'Fibonacci', 'Nash']
letter = input("Enter the letter you would like to count: ")
while not letter.isalpha() or len(letter) != 1:
letter = input("Not a single letter. Try again: ")
print("".join(lst).lower().count(letter))
@ZdaR 的解决方案最好只计算一次字母。如果你想在同一个字符串中多次获取字母,使用 collection.Counter
会更快。示例:
from collections import Counter
counter = Counter("".join(lab7))
while True:
input_char = input('Enter letter you would like to count here')
print counter[input_char]
你也可以使用 sum + 生成器:
letter = letter.lower()
count = sum(w.lower().count(letter) for w in lab7)
我正在计算某个字母在我的列表中出现了多少次。但是,每当我使用计数函数并输入我想要计算的字母时 return = 0
这是代码:
lab7 = ['Euclid','Archimedes','Newton','Descartes','Fermat','Turing','Euler','Einstein','Boole','Fibonacci', 'Nash']
print(lab7[1]) #display longest name - a
print(lab7[10]) #display shortest name - b
c = [ word[0] for word in lab7]
#display str that consists of 1st letter from each name in list - c
print(c)
d = [ word[-1] for word in lab7]
#display str that consists of last letter from each name in list - d
print(d)
**x = input('Enter letter you would like to count here')
lab7.count('x')
e = lab7.count('x')
print(e)**
这是无效的代码部分。我不断收到 ->
Archimedes
Nash
['E', 'A', 'N', 'D', 'F', 'T', 'E', 'E', 'B', 'F', 'N']
['d', 's', 'n', 's', 't', 'g', 'r', 'n', 'e', 'i', 'h']
Enter letter you would like to count here s
0
作为我的输出。
如果你想计算给定字符在 list
中所有单词中出现的次数,那么你可以尝试:
input_char = input('Enter letter you would like to count here')
print "".join(lab7).count(input_char)
如果您希望逻辑不区分大小写,您可以使用 .lower()
你首先将list
的所有元素连接起来得到一个统一的字符串,然后使用count
方法得到给定字符的出现。
lst = ['Euclid', 'Archimedes', 'Newton', 'Descartes', 'Fermat',
'Turing', 'Euler', 'Einstein', 'Boole', 'Fibonacci', 'Nash']
letter = input("Enter the letter you would like to count: ")
count = "".join(lst).lower().count(letter)
print(count)
这将连接列表中包含的所有单词并生成单个字符串。然后字符串将被降低以计算大写和小写字母(例如 A
等于 a
)。如果不应该平等对待大小写字母,可以去掉.lower()
。
检查输入是否只有一个字母:
lst = ['Euclid', 'Archimedes', 'Newton', 'Descartes', 'Fermat',
'Turing', 'Euler', 'Einstein', 'Boole', 'Fibonacci', 'Nash']
letter = input("Enter the letter you would like to count: ")
while not letter.isalpha() or len(letter) != 1:
letter = input("Not a single letter. Try again: ")
print("".join(lst).lower().count(letter))
@ZdaR 的解决方案最好只计算一次字母。如果你想在同一个字符串中多次获取字母,使用 collection.Counter
会更快。示例:
from collections import Counter
counter = Counter("".join(lab7))
while True:
input_char = input('Enter letter you would like to count here')
print counter[input_char]
你也可以使用 sum + 生成器:
letter = letter.lower()
count = sum(w.lower().count(letter) for w in lab7)