计算 python grok 中的字符数
Counting Characters in python grok
有一个任务,我必须输入名字,最后打印出多少个不同的字符和多少个重复字符。
'Your program should read in multiple lines of input, until a blank line is entered. Then it should print out how many unique characters were named, and how many were repeated.'
这是我的结果:
人物:医生
人物:玫瑰
角色:罗里
角色:克拉拉
人物:K-9
角色:大师
角色:博士
人物:艾米
字符:
您命名了 8 个字符
您重复了 1 次
这是我的代码:
count = []
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if a == country:
b = b + 1
else:
c = c + 1
c = c - b
count.sort()
print('You named',c,'character(s)')
print('You repeated',a,'time(s)')
应该说:
角色:博士
人物:玫瑰
角色:罗里
角色:克拉拉
人物:K-9
角色:大师
角色:博士
人物:艾米
字符:
您命名了 7 个字符。
您重复了 1 次。
可以使用以下代码段:
n<-ncol(data)
m<-mean(rowMeans(data))
exp_val<-mean(apply(data,1,var))
v<-var(rowMeans(data))-mean(apply(data,1,var))/n
z<-n/(n+exp_val/v)
premiums<-Z*rowMeans(data)+(1-z)*m
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if country in count:
b = b + 1
else:
c = c + 1
count.sort()
print('You named',c,'character(s)')
print('You repeated',b,'time(s)')
结果:
Character: "AA"
Character: "BB"
Character: "CC"
Character: "BB"
Character: ""
('You named', 3, 'character(s)')
('You repeated', 1, 'time(s)')
主要变化:
if country in count: //changed
c = c - b //removed
print('You named',c,'character(s)') //changed
print('You repeated',b,'time(s)') //changed
您可以将字符和它们的出现次数收集到字典中。 (还有 collections.Counter()
是为此而设计的,但为了简单起见,一个普通的 dict 就可以了。)
character_counts = {}
while True:
character = input("Character: ")
if not character: # Empty line?
break # Quit the loop
# Get the current count for the character, or 0 if not found, increment with one,
# assign back to the dict.
character_counts[character] = character_counts.get(character, 0) + 1
# Get a list of characters who occur more than once.
repeated_characters = [
character
for (character, count) in character_counts.items()
if count > 1
]
print("You named {} character(s)".format(len(character_counts)))
print("You repeated {} time(s)".format(len(repeated_characters)))
有一个任务,我必须输入名字,最后打印出多少个不同的字符和多少个重复字符。
'Your program should read in multiple lines of input, until a blank line is entered. Then it should print out how many unique characters were named, and how many were repeated.'
这是我的结果:
人物:医生
人物:玫瑰
角色:罗里
角色:克拉拉
人物:K-9
角色:大师
角色:博士
人物:艾米
字符:
您命名了 8 个字符
您重复了 1 次
这是我的代码:
count = []
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if a == country:
b = b + 1
else:
c = c + 1
c = c - b
count.sort()
print('You named',c,'character(s)')
print('You repeated',a,'time(s)')
应该说:
角色:博士
人物:玫瑰
角色:罗里
角色:克拉拉
人物:K-9
角色:大师
角色:博士
人物:艾米
字符:
您命名了 7 个字符。
您重复了 1 次。
可以使用以下代码段:
n<-ncol(data)
m<-mean(rowMeans(data))
exp_val<-mean(apply(data,1,var))
v<-var(rowMeans(data))-mean(apply(data,1,var))/n
z<-n/(n+exp_val/v)
premiums<-Z*rowMeans(data)+(1-z)*m
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if country in count:
b = b + 1
else:
c = c + 1
count.sort()
print('You named',c,'character(s)')
print('You repeated',b,'time(s)')
结果:
Character: "AA"
Character: "BB"
Character: "CC"
Character: "BB"
Character: ""
('You named', 3, 'character(s)')
('You repeated', 1, 'time(s)')
主要变化:
if country in count: //changed
c = c - b //removed
print('You named',c,'character(s)') //changed
print('You repeated',b,'time(s)') //changed
您可以将字符和它们的出现次数收集到字典中。 (还有 collections.Counter()
是为此而设计的,但为了简单起见,一个普通的 dict 就可以了。)
character_counts = {}
while True:
character = input("Character: ")
if not character: # Empty line?
break # Quit the loop
# Get the current count for the character, or 0 if not found, increment with one,
# assign back to the dict.
character_counts[character] = character_counts.get(character, 0) + 1
# Get a list of characters who occur more than once.
repeated_characters = [
character
for (character, count) in character_counts.items()
if count > 1
]
print("You named {} character(s)".format(len(character_counts)))
print("You repeated {} time(s)".format(len(repeated_characters)))