Python 如何找到每个数字中有多少个数字

Python 如何找到每个数字中有多少个数字,python,Python,示例从 number= int(input()) 如果我输入87576 结果会是这样的 digit 5 has 1 digit 6 has 1 digit 7 has 2 digit 8 has 1 *我真的不知道如何解决这个问题……救命。。 *Python试试这个: from collections import Counter Counter(str(number)) # Counter({'8': 1, '7': 2, '5': 1, '6': 1}) 试试这个 from coll

示例从

number= int(input()) 
如果我输入87576

结果会是这样的

digit 5 has 1
digit 6 has 1
digit 7 has 2
digit 8 has 1
*我真的不知道如何解决这个问题……救命。。 *Python

试试这个:

from collections import Counter
Counter(str(number))
# Counter({'8': 1, '7': 2, '5': 1, '6': 1})
试试这个

from collections import Counter
number = '87576'
cn = Counter(number)
print(cn)

答案是:

chars = "0123456789"
number = int(input("enter number: "))

for char in chars:
  count = str(number).count(char)
  if count > 0:
    print('digit {} has {}'.format(char,count))

我想你是想问一个数字中每个数字出现的次数

步骤1:将数字转换为整数列表,并维护数字中唯一数字的集合

res = [int(x) for x in str(number)] 
res1 = set(res)
第二步:写一个方法来计算数字在列表中的出现次数

def countX(lst, x): 
return lst.count(x) 
步骤3:为每个数字调用countX方法

for val in res1:
    print('digit {} has {}'.format(val,countX(res,val)))

另一个办法是,

from collections import defaultdict

number = input()

digits_frequency = defaultdict(int)

while number:
    digit = number % 10
    digits_frequency[digit] += 1
    number //= 10

print(digits_frequency)
输出:

defaultdict(<type 'int'>, {8: 1, 5: 1, 6: 1, 7: 2})
digit 5 has 1
digit 6 has 1
digit 7 has 2
digit 8 has 1
时间复杂性:
打开,因为我们正在迭代输入一次

您应该使用“计数器”对数字进行计数,并使用“排序”方法对计数的数字进行排序

from collections import Counter

number= 87576
count_dict = Counter(str(number))

print(dict(sorted(count_dict.items())))
输出:


所以有很多方法。让我们从基本到高级开始

inp = '87576'
dct = {}
for dig in inp:
    if dig not in dct:
        dct[dig] = 1
    else:
        dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第二种方法:

inp = '87576'
dct = {}
for dig in inp:
    dct[dig] = dct.get(dig,0) + 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第三种方法:

inp = '87576'
dct = dict.fromkeys(inp,0)
for dig in inp:
    dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第四种方法:

inp = '87576'
dct = {dig: inp.count(dig) for dig in inp}
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
inp = '87576'
for dig in sorted(set(inp)):
    print(f"digit {dig} has {inp.count(dig)}")
from collections import defaultdict
inp = '87576'
dct = defaultdict(int)
for dig in inp:
    dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第五种方法:

inp = '87576'
dct = {dig: inp.count(dig) for dig in inp}
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
inp = '87576'
for dig in sorted(set(inp)):
    print(f"digit {dig} has {inp.count(dig)}")
from collections import defaultdict
inp = '87576'
dct = defaultdict(int)
for dig in inp:
    dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第六种方法:

inp = '87576'
dct = {dig: inp.count(dig) for dig in inp}
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
inp = '87576'
for dig in sorted(set(inp)):
    print(f"digit {dig} has {inp.count(dig)}")
from collections import defaultdict
inp = '87576'
dct = defaultdict(int)
for dig in inp:
    dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
第七种方法如其他人所述:

from collections import Counter
inp = '87576'
dct = Counter(inp)
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")
输出:

defaultdict(<type 'int'>, {8: 1, 5: 1, 6: 1, 7: 2})
digit 5 has 1
digit 6 has 1
digit 7 has 2
digit 8 has 1
编辑:

我知道你在寻找一个整数的解决方案:

import math
inp = int('87576')
digits = ((inp//10**i)%10 for i in range(math.ceil(math.log(inp,10))))
dct = defaultdict(int)
for dig in digits:
    dct[dig] += 1
for dig in sorted(dct):
    print(f"digit {dig} has {dct[dig]}")

到目前为止你试过什么了吗?问题一点也不清楚。请解释清楚?请清楚地定义您的问题。创建空字典。在字符串“87576”上循环,检查当前字符是否在字典中,如果是,则向其添加1,如果不是,则将该字符添加为键,并将其值指定为1。@SayandipDutta您能给我举个例子吗?听起来像是我正在尝试做的,但我就是不知道如何做……当然,给我一点时间。这很完美,但是否可以将数字输入为数字而不是字符串?就像number=intInputAttakate一样,伙计,第一种方法很好!