2016-02-18 53 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