【问题标题】:Create a histogram in Python在 Python 中创建直方图
【发布时间】:2022-01-21 03:18:20
【问题描述】:

我正在尝试在 python 中创建直方图

我的功能是下一个:

        def hist_hor(diccionario):
            dict={}
            
            for e in diccionario:
                if e  in dict:
                    dict[e] += '*'
                else: 
                    dict[e]= '*'
            for i in sorted(dict):
        
                print(f'{i}: {dict[i]}')
    
    histograma_horizontal(d)

它应该是这样的:

a: *****
b: **********
c: ************
d: ***********
e: ***************
f: ********************
g: ***************
h: *********
i: *******
j: **

但我的函数看起来像这样:

a: *
b: *
c: *
d: *
e: *
f: *
g: *
h: *
i: *
j: *

另外,有谁知道用这种方式来表示它吗?:

          *        
          *        
          *        
          *        
        * * *      
        * * *      
        * * *      
    *   * * *      
    * * * * *      
  * * * * * *      
  * * * * * * *    
  * * * * * * *    
  * * * * * * * *  
  * * * * * * * *  
* * * * * * * * *  
* * * * * * * * *  
* * * * * * * * *  
* * * * * * * * * *
* * * * * * * * * *

【问题讨论】:

  • 什么是字典?请分享一下
  • 这对于defaultdict来说似乎是一个不错的用途。

标签: python histogram


【解决方案1】:

您可以在不创建新字典的情况下完成第一部分(记得使用end 来防止换行):

注意 - 在 Ubuntu 20.04 中测试,使用 Python 3.8。

def hist_hor(diccionario):
    for key, value in diccionario.items():
        print(f"{key}: ", end="")
        for _ in range(value):
            print("*", end="")
        print()


diccionario = {"a": 5, "b": 10, "c": 12, "d": 11, "e": 15,
               "f": 20, "g": 15, "h": 9, "i": 7, "j": 2, }

hist_hor(diccionario)

输出:

a: *****
b: **********
c: ************
d: ***********
e: ***************
f: ********************
g: ***************
h: *********
i: *******
j: **

但如果您需要创建新字典,请使用update 而不是尝试操作索引:

def hist_hor(diccionario):
    # Do not use dict as a variable name; it shadows a builtin type
    new_dicc = {}
    for key, value in diccionario.items():
        temp = ""
        for _ in range(value):
            temp += "*"
        new_dicc.update({key: temp})
    return new_dicc

d = hist_hor(diccionario)
for key, value in d.items():
    print(f"{key}: {value}")

输出将是相同的。

对于第二部分,首先获取最大值并创建一个反向循环。遍历这些值,如果该值大于或等于循环索引,则打印一个星号:

def hist_hor(diccionario):
    m = max(diccionario.values())
    for i in range(m, 0, -1):
        for x in diccionario.values():
            if x >= i:
                print("* ", end="")
            else:
                print("  ", end="")
        print()

输出:

          *         
          *         
          *         
          *         
          *         
        * * *       
        * * *       
        * * *       
    *   * * *       
    * * * * *       
  * * * * * *       
  * * * * * * *     
  * * * * * * *     
  * * * * * * * *   
  * * * * * * * *   
* * * * * * * * *   
* * * * * * * * *   
* * * * * * * * *   
* * * * * * * * * * 
* * * * * * * * * * 

【讨论】:

  • 非常感谢@Rob,如果我想实现排序的功能,我会怎么做?
  • 有几种方法。您可以使用operator.itemgettercollections.OrderedDict,也可以翻转值和键:new_dicc = dict(sorted((value, key) for (key, value) in new_dicc.items())) 最简单的方法是使用带有 lambda 函数的sortednew_dicc = dict(sorted(new_dicc.items(), key=lambda item: item[1]))
  • 非常感谢您的建议@Rob
  • 非常感谢您的建议@Rob
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
  • 2011-10-30
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2021-02-19
相关资源
最近更新 更多