練習題3

設計一個函數,統計任意一串字符串中數字字符的個數
def numCount():python

count = 0

str = raw_input('please input something:')

for i in str:

    if i.isdigit():

        count+=1

print count

numCount()git

2,設計函數,統計任意一串字符串中每一個字母的個數,不區分大小寫app

import stringide

def lettersCount():函數

list1=[]

str = raw_input('please input something:')

for i in str:

    if i.isalpha():

        list1.append(i.lower())

for j in string.lowercase:

    n = list1.count(j)

    if not n:

        continue

    else:

        print '%s 的個數爲%s' %(j,n)

lettersCount()this

2018-01-02 13:16 添加評論 評分設計

0agh353272297 code

一、設計一個函數,統計任意一串字符串中數字字符的個數orm

#!/usr/bin/env pythonutf-8

str1 = raw_input("請輸入字符: ")

def iCount(s):

Count = 0

for i in s:

    if i.isdigit():

        Count += 1

    else:

        pass

print "數字=%d" % (Count)

if name == 'main':

iCount(str)

二、設計函數,統計任意一串字符串中每一個字母的個數,不區分大小寫

#!/usr/bin/env python

-- coding: utf-8 --

@Time : 2018-1-2 11:56

@Author : anChow

@File : 統計字符串個數.py

str1 = raw_input("請輸入字符: ")

def strCount(s):

s =list( ''.join(s.split()))

s1 = set(s)

l = list(s1)

t = {}

for i in range(len(l)):

    num = 0

    for j in reversed(range(len(s))):

        if l == s[j]:

            num = num + 1

            s.pop(j)

    t[l] = str(num) 

return t

if name == 'main':

t = strCount(str1)

print(t)

2018-01-02 17:38 添加評論 評分

0LINUX_A - 不要在最能吃苦的年齡選擇安逸!

#!/usr/bin/env python

-- coding: utf-8 --

@Time : 1/2/2018 11:00 PM

@Author : Zhdya

@File : 0102.py

@Software: PyCharm

習題

1. 設計一個函數,統計任意一串字符串中數字字符的個數

例如:

"adfdfjv1jl;2jlk1j2" 數字個數爲3個

list1 = []

aa = raw_input("pls input sth: ")

for i in aa:

if i in "0123456789":

    list1.append(i)

print "the input number is {}".format(len(list1))

2. 設計函數,統計任意一串字符串中每一個字母的個數,不區分大小寫

例如:

"aaabbbcccaae111"

a 5個

b 3個

c 3個

e 1個

list1 = []

list2 = {}

aa = raw_input("pls input sth: ")

for i in aa:

if i.isalpha():

    list1.append(i)

for i in list1:

list2 = list1.count(i)

for k,v in list2.iteritems():

print " total this character {} is {}".format(k,v)

2018-01-03 15:56 添加評論 評分

0zhouyuyao

  1. 設計一個函數,統計任意一串字符串中數字字符的個數

    例如:

    "adfdfjv1jl;2jlk1j2" 數字個數爲3個

def countNum():

co = 0

s = input("Please input a string : ")

for c in s:

    if c.isdigit():

        co+=1

print("The string have {} number.".format(co))

countNum()

  1. 設計函數,統計任意一串字符串中每一個字母的個數,不區分大小寫

    例如:

    "aaabbbcccaae111"

    a 5個

    b 3個

    c 3個

    e 1個

import re

def countNum():

a={}

# s1=''

s = input("Please input a string : ")

s1=re.sub('[^a-zA-Z]','',s)

a = {i: s.count(i) for i in s1}

print(a)

countNum()

相關文章
相關標籤/搜索