有一个任务,我必须输入名称,并在最后打印多少个不同的字符和多少重复。
“你的程序应该用多行输入来读取,直到输入一个空行。然后它应该打印出有多少个唯一的字符被命名,多少个被重复了。”
这是我的结果:
人物:医生
人物:玫瑰
人物:罗里
人物:克拉拉
人物: K-9
人物:大师
人物:博士
人物:艾米
人物:
你指定了8个字符
你重复了一次
这是我的密码:
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个字符。
你重复了一次。
发布于 2019-08-27 13:56:22
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)')结果:
人物:"AA“ 人物:"BB“ 字符:"CC“ 人物:"BB“ 人物:"“ (“你命名”,3,“人物”) (“你重复”,1,“时间”)
主要变动:
if country in count: //changed c = c - b //removedprint('You named',c,'character(s)') //changed
print('You repeated',b,'time(s)') //changed发布于 2019-08-27 14:02:47
你可以把字符和它们的出现数收集成一个小块。(也有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)))发布于 2019-08-27 13:30:35
可以使用以下代码段:
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)*mhttps://stackoverflow.com/questions/57675640
复制相似问题