您的位置:首页 > 编程语言 > Python开发

list的相关操作 - 单词倒排 - 统计个数

2016-08-05 15:19 211 查看
#!/usr/bin/python

#---------------单词倒排----------------

def str_reverse(str_src):
# 以空格为分隔符,将各单词取出来存放在list中
str_dst = str_src.split()
# 同理若想以","为分隔符去单词就用 str_dst = str_src.split(",")

        print str_dst

        # 反转list0

        str_dst.reverse()

        print str_dst

        return str_dst

if __name__ == '__main__':

     for str_out in str_reverse(raw_input("please input your sentense:")):

         print str_out,

         

# 另倒排也可利用切片:eg

A = ['abc', 123, 456, 'def']

B = A[::-1]

print B

         

#---------------统计个数----------------

A="a=1, b=2, c=3, cc=4, cc=5"

count = index = 0

while True:
# 在 A 中找 cc

        index = A.find("cc")

        # 若没找到

        if index == -1:

                break

        else:

        # A 取该次出现的位置后面的字符串

                A = A[ index + 1: ]

                count += 1

print "count: %d" % count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python