简体   繁体   English

如何对字典中的项目进行排序并显示最大的键和值?

[英]How can I sort items in a dictionary and display the largest key and value?

I wrote a program that reads 10 numbers from the input and at the end prints the number that has the largest number of divisors of the first number along with the number of divisors of the first number in the output.我编写了一个程序,它从输入中读取 10 个数字,最后打印出 output 中第一个数字的除数最多的数字以及第一个数字的除数。 If several have this mode, print the largest of them.如果有几个具有此模式,则打印其中最大的一个。

The input I give to the program:我给程序的输入:

1854
9875
3567
2568
2984
5428
6487
7982
3485
2157
import operator
number_list= []
divisor_prime_list = []
dwq = {}
sd = []
for i in range(10):
    n = int(input())
    number_list.append(n)
    i = 2
    a = {}
    factors = []
    while i*i<=n :
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
         
    if n > 1:
        factors.append(n)
        fu = list(set(factors))
        divisor_prime_list.append(len(fu))
for d in number_list:
    for iq in divisor_prime_list:
        dwq[d] = iq
        divisor_prime_list.remove(iq)
        break
b = reversed(sorted(dwq.items(),key=operator.itemgetter(0)))
for k,v in b:
    print(k,v)
    break

But I had trouble displaying the output但我无法显示 output

My code output is incorrect:我的代码 output 不正确:

9875 2

And the correct output:以及正确的 output:

7982 3

Please help me edit the code so that it shows me the correct output请帮我编辑代码,以便它显示正确的 output

I would recommend writing little testable building blocks for your problem, in the form of functions.我建议以函数的形式为您的问题编写一些可测试的构建块

For example:例如:

from math import gcd
from itertools import combinations

def prime_factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return factors

def factor_count(x):
    primes = prime_factors(x)
    return len([t for n in range(1, len(primes) + 1)
                for t in set(combinations(primes, n))])

Application:应用:

a = 1854
b = [9875, 3567, 2568, 2984, 5428, 6487, 7982, 3485, 2157]

ndiv = [factor_count(gcd(a, x)) for x in b]
max([(n, x) for n, x in zip(ndiv, b)])

# out:
(3, 2568)

If you must take user input etc., then:如果您必须接受用户输入等,那么:

values = [int(input()) for _ in range(10)]
a, b = values[0], values[1:]

# and then use the above

Supplement:补充:

# while not immediately needed in your question, you may like:
from functools import reduce

def prod(a):
    return reduce(lambda x, y: x * y, a)

def factors(x):
    primes = prime_factors(x)
    for n in range(1, len(primes) + 1):
        for t in set(combinations(primes, n)):
            yield prod(t)

Example:例子:

>>> prime_factors(20)
[2, 2, 5]

>>> sorted(factors(20))
[2, 4, 5, 10, 20]

>>> factor_count(20)
5

# list of each pair under consideration, along with common divisors:
>>> [(a, x, sorted(factors(gcd(a, x)))) for x in b]
[(1854, 9875, []),
 (1854, 3567, [3]),
 (1854, 2568, [2, 3, 6]),
 (1854, 2984, [2]),
 (1854, 5428, [2]),
 (1854, 6487, []),
 (1854, 7982, [2]),
 (1854, 3485, []),
 (1854, 2157, [3])]

Here is your code fixed:这是您修复的代码:

number_list = []
divisor_prime_list = []
dwq = []
sd = []
for j in range(10):
    n = int(input())
    number_list.append(n)
    i = 2
    a = {}
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)

    if n > 1:
        factors.append(n)

    fu = set(factors)
    divisor_prime_list.append(len(fu))

for d, p in zip(number_list, divisor_prime_list):
    dwq.append((p, d))

b = max(dwq)
print(b[1], b[0])

The loop variable i of the outer for was the same as the one you used to find the primes.外部for的循环变量i与您用来查找素数的变量相同。

You forgot to dedent at fu = list(set(factors))你忘了在fu = list(set(factors))

I haven't been able to understand your intentions after for d in number_list , so I rewrote that heavily.for d in number_list之后我无法理解你的意图,所以我重写了很多。 All you need to do is collect the pairs of numbers and their divisor count.您需要做的就是收集成对的数字及其除数。 If you put the divisor count first, all you need to do is find the max of the pairs.如果你把除数放在第一位,你需要做的就是找到对的最大值。 Remember that tuples are sorted (by default) by their first element, then by their second, and so on.请记住,元组(默认情况下)按其第一个元素排序,然后按其第二个元素排序,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM