小编MJP*_*MJP的帖子

计算字符串中的特定字符 - Python

试图找出Python中允许用户输入句子的最佳方式,然后除了计算元音数量之外,还计算该句子中的字符数量。我希望输出返回字符总数,加上 A 总数、O 总数、U 总数等。这是我到目前为止的代码:

# prompt for input    
sentence = input('Enter a sentence: ')

# count of the number of a/A occurrences in the sentence
a_count = 0    
# count of the number of e/E occurrences in the sentence
e_count = 0   
# count of the number of i/I occurrences in the sentence      
i_count = 0
# count of the number of o/O occurrences in the sentence         
o_count = 0
# count of the number of u/U occurrences in …
Run Code Online (Sandbox Code Playgroud)

python string character count

4
推荐指数
1
解决办法
1万
查看次数

使用dplyr按年计算比例

我试图计算变量(在这种情况下是国家)在任何给定年份出现的频率.例如:

name <- c('AJ Griffin','Steve Bacon','Kevin Potatoe','Jose Hernandez','Kent Brockman',
                  'Sal Fasno','Kirk Kelly','Wes United','Livan Domingo','Mike Fast')

country <- c('USA', 'USA', 'Canada', 'Dominican Republic', 'Panama', 'Dominican Republic', 'Canada', 'USA', 'Dominican Republic', 'Mexico') 

year <- c('2016', '2016', '2016', '2016', '2016', '2015', '2015', '2015', '2015', '2015') 

country_analysis <-data.frame(name, country, year)
Run Code Online (Sandbox Code Playgroud)

当我使用下面的代码时,我得到整个数据集的国家比例,但我想进一步削减到特定年份.

P <- country_analysis %>% 
  group_by(country) %>%     
  summarise(n=n())%>%
  mutate(freq = round(n / sum(n), 1))
Run Code Online (Sandbox Code Playgroud)

理想情况下,最终结果将包含国家,年份,频率列(即2016年,美国,0.4).任何输入将不胜感激.

r dplyr

0
推荐指数
1
解决办法
72
查看次数

标签 统计

character ×1

count ×1

dplyr ×1

python ×1

r ×1

string ×1