2016-02-18 31 views
2
def muchbetter(x): 
    count_list = [] 
    for char in "abcdefghijklmnopqrstuvwxyz": 
     count_list.append(x.lower().count(char)) 
    return tuple(count_list) 

此功能使星号的塔使得当每个字母出现在一个示例文本(X)的列表,我想要的,就是把这个函数的结果为sortof“塔“的”*“,所以例如,如果(x)='AAAA'列表将显示(4,0,0,0,0,0 ..零字母tat不会出现)我希望它转这一结果成塔,看起来像这样Python中,从列表中

* 
* 
* 
* 

如果(X)= AABBBB我想让它显示是这样的

* 
* 
** 
** 

这样一个2的A和B的塔,等等,如果它说x =(蟒蛇很难),它会使塔的星星等于列表中每个字母的值。

+0

的可能的复制[帕斯卡三角形](http://stackoverflow.com/questions/1740499/pascals -triangle) –

+0

它看起来像你从来没有接受任何问题的答案。当您获得解决问题的答案时,请考虑使用该功能。 – timgeb

回答

0
def muchbetter(x): 
    count_list = [] 
    for char in "abcdefghijklmnopqrstuvwxyz": 
     count_list.append(x.lower().count(char)) 
    return tuple(count_list) 

s = 'This is a sample sentence' 

z = muchbetter(s) 
print(s) 
for i in range(max(z), 0, -1): 
    for j in range(0, 26): 
     if z[j] >= i: 
      print('*', end='') 
     else: 
      print(' ', end='') 
    print('') 
print("abcdefghijklmnopqrstuvwxyz") 

或者类似的东西...

3

类似的东西应该工作使用列表理解zipjoin

def muchbetter(x): 
    count_list = [] 
    for char in "abcdefghijklmnopqrstuvwxyz": 
     count_list.append(x.lower().count(char)) 
    return tuple(count_list) 

def print_stars(x): 
    tup = muchbetter(x) 
    stars = [' '*(max(tup) - s) + '*'*s for s in tup if s != 0] 
    print('\n'.join([''.join(a) for a in list(zip(*stars))])) 


in_str = 'AABBBBCCCCC' 
print_stars(in_str) 
    * 
** 
** 
*** 
*** 

in_str = 'AABBBB' 
print_stars(in_str) 
* 
* 
** 
** 

编辑

如果您想要打印水平塔你可以使用:

def print_hor_stars(x): 
    tup = muchbetter(x) 
    stars = [' '*(max(tup) - s) + '*'*s for s in tup if s != 0] 
    print('\n'.join([''.join(a) for a in stars])) 

in_str = 'AABBBB' 
print_hor_stars(in_str) 
    ** 
**** 

EDIT2

如果你想你的塔是在特定的字母,你可以使用函数:

def print_stars_order(x): 
    tup = muchbetter(x) 
    stars = [' '*(max(tup) - s) + '*'*s for s in tup] 
    print('\n'.join([''.join(a) for a in list(zip(*stars))])) 
    print("abcdefghijklmnopqrstuvwxyz") 

in_str='python is difficult' 
print_stars_order(in_str) 

     *     
    * *   *  
    ** * ** * *** *** * 
abcdefghijklmnopqrstuvwxyz 
+0

我还在想这些塔是水平的还是垂直的...... – albert

+0

@albert现在有两个垂直和水平塔功能 –

+0

如果ABC ... Z轴位于左侧,该怎么办?因此,对于AABBBCCCC,可能会有'**','***'和'****'列相邻。 – albert

0

这里有一种方法。

from string import ascii_lowercase 
from itertools import groupby 

def make_row(group_lists): 
    start_index = ord('a') 
    row = [' '] * 26 
    nonempties = filter(None, group_lists) 
    for group_list in nonempties: 
     char = group_list.pop() 
     row[ord(char) - start_index] = '*' 
    return "".join(row), nonempties 

def histogram(chars): 
    base_str = "==========================\n" + ascii_lowercase + "\n" 
    grouped_chars = [list(y) for x, y in groupby(sorted(chars.lower()))] 
    while grouped_chars: 
     row, grouped_chars = make_row(grouped_chars) 
     base_str = row + "\n" + base_str 
    return base_str 

这是一个针对整数0-9的类似直方图的Haskell代码高尔夫问题的Python修改。 Here's my Haskell code

In [13]: print histogram("BBAAddZzyX") 

** *      * 
** *     *** 
========================== 
abcdefghijklmnopqrstuvwxyz 


In [14]: print histogram("AABBBB") 

*       
*       
**       
**       
========================== 
abcdefghijklmnopqrstuvwxyz 
0
from collections import Counter 
import string 

cnt = Counter([x for x in 'thequicaaaaaaakbrownfoxjumpsoverthelzydogdddd']) 
max_height = max(cnt.values()) 

display = [] 

for step in range(max_height,0,-1): 
    line = [] 
    for letter in string.ascii_lowercase: 
     if cnt[letter] == step: 
      line.append('.') 
     elif cnt[letter] > step: 
      line.append('|') 
     else: 
      line.append(' ') 
    display.append(line) 

for x in display: 
    print(''.join(x)) 
print(string.ascii_lowercase) 

调整了显示一个位为fun--输出如下:

.       
|       
| .      
| |   .   
| |.   |   
| || .  | . ..  
|..||..|......|..|.||..... 
abcdefghijklmnopqrstuvwxyz