我有一个单词词典,其单词为键,重复次数为值。我已将字典转换为列表并为其添加了颜色。它可以很好地打印出来,但是当我尝试以更奇特的格式(例如矩阵)将其打印出来时,颜色不会保留下来。
我在这里尝试了一些建议,例如使用numpy矩阵,matlib,但是它们按原样输出字符串。
def word_count(a_string):
string_dict = {}
for word in a_string.split():
if word in string_dict:
string_dict[word] += 1
else:
string_dict[word] = 1
return string_dict
colors = ['\33[37m','\33[32m','\33[33m','\33[34m','\33[35m','\33[36m','\33[31m' ]
x = """Charles W. Thurston specializes in solar energy, in finance and in technological processes. Key areas of focus for Charles are
bifacial panels and solar tracking. He has been active in the industry for 25 years, living and working in locations ranging from
Brazil to Papua New Guinea. He He He Mi Mi De """
y = (word_count(x))
z = [colors[int(v)] + str(k) for k,v in y.items()]
for a in range(len(z)):
print z[a]
而不是输出
Brazil
Charles
focus
energy,
are
in
我希望它像这样
Brazil Charles focus
energy are in
将单词分配为颜色。