判断字符串里每个字符的次数(python,shell,perl)

有一个请求,判断字符串里每个字符出现的次数

python
解体思路:列表的count方法

Python 2.6.6 (r266:84292, Aug  9 2016, 06:11:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> var="dsvsdwefsdagafoijkafdgweodfas"#要判断的字符串
>>> varlist=list(var)#列表化字符串
>>> print varlist
['d', 's', 'v', 's', 'd', 'w', 'e', 'f', 's', 'd', 'a', 'g', 'a', 'f', 'o', 'i', 'j', 'k', 'a', 'f', 'd', 'g', 'w', 'e', 'o', 'd', 'f', 'a', 's']
>>> sinlist=[]#要一个空列表
>>> for letter in varlist:#排除重复字符,把不重复的字符装入新列表。
...   if letter not in sinlist:
...     sinlist.append(letter)
... 
>>> print sinlist
['d', 's', 'v', 'w', 'e', 'f', 'a', 'g', 'o', 'i', 'j', 'k']#这是不重复的列表
>>> for letter in sinlist:#在新列表里循环,用list.count的方法确认每个字符在旧列表出现的次数
...   print "letter name is " + letter + " count: " + str(varlist.count(letter))
... 
letter name is d count: 5
letter name is s count: 4
letter name is v count: 1
letter name is w count: 2
letter name is e count: 2
letter name is f count: 4
letter name is a count: 4
letter name is g count: 2
letter name is o count: 2
letter name is i count: 1
letter name is j count: 1
letter name is k count: 1
>>> quit()

shell
解题思路:字符串长度,cut和grep

[myname@mypc Desktop]$ var="dsvsdwefsdagafoijkafdgweodfas"#要判断的字符串
[myname@mypc Desktop]$ endnum=`echo -n $var|wc -c`#计算出字符串长度,记住这里用的是echo -n为了排除换行符
[myname@mypc Desktop]$ echo $endnum
29
[myname@mypc Desktop]$ i=1#赋值一个int变量
[myname@mypc Desktop]$ while [ $i -le $endnum ];do echo $var|cut -c $i;i=`expr $i + 1`;done>tempfile#用一个循环把每个字符打印出来,重定向一个临时文件
[myname@mypc Desktop]$ cat tempfile
d
s
v
s
d
w
e
f
s
d
a
g
a
f
o
i
j
k
a
f
d
g
w
e
o
d
f
a
s
[myname@mypc Desktop]$ i=1
[myname@mypc Desktop]$ while [ $i -le $endnum ];do letter=`echo $var|cut -c $i`;count=`grep $letter tempfile|wc -l`;echo "letter is $letter,count is $count";i=`expr $i + 1`;done|sort -u
letter is a,count is 4
letter is d,count is 5
letter is e,count is 2
letter is f,count is 4
letter is g,count is 2
letter is i,count is 1
letter is j,count is 1
letter is k,count is 1
letter is o,count is 2
letter is s,count is 4
letter is v,count is 1
letter is w,count is 2
#再用一个循环去判断每个字符出现的次数
详细解析一下这个循环

while [ $i -le $endnum ]#当i小于或等于字符串长度的时候循环
do
letter=`echo $var|cut -c $i`#取出字符名字
count=`grep $letter tempfile|wc -l`#用grep取出tempfile里字符出现的行数
echo "letter is $letter,count is $count"#打印出结果
i=`expr $i + 1`#i递增
done|sort -u#sort -u是去重,因为里面有重复的结果

perl
perl我不是很懂,查了半天,对于perl我的评价是好用是好用,但是固有写法中符号用的太多,很容易让初学者看起来像天书
解题思路:字符转数组split,替换s/var/var/g,听说还能用hash解,不会中

[myname@mypc Desktop]$ perl -e 'my $var="dsfesfsavcxgsatewqrsadfsaga";#赋值var
> my @varlist=split("",$var);#转化var为数组varlist
> my @grepaf = grep{++$count{$_}<2} @varlist;#这个网上抄来的去重,原理是循环数组varlist,当计数不重复的时候记录,重复不计入数组grepaf
> foreach $a (@grepaf) #在不重复的数组里循环
> { $count=$var=~s/$a/$a/g;#这个意思是说替换变量a为变量a,然后替换的次数就成了count的值。
> print $a," is ",$count,"\n"; }'#打印结果
d is 2
s is 6
f is 3
e is 2
a is 5
v is 1
c is 1
x is 1
g is 2
t is 1
w is 1
q is 1
r is 1

对比下来,python的方法最简单,shell的最多步骤,perl写的最复杂(perl自带的符号太多了,学习这些符号好累)

    原文作者:六十三63
    原文地址: https://www.jianshu.com/p/d3e3eb354633
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞