我已经计算了具有多个记录的列(String)中的单词和字符的数量,并存储在两个不同的列中。我想按降序排序单词数量(标记)。 例如:
CITY OF AMSTERDAM,3,17
token_map = {}
words = []
with open('test.csv', 'r') as fopen:
lines = fopen.readlines()
for line in lines:
line = str.strip(line)
words_in_line = str.split(line, " ")
token_map[line] = "" + str(len(words_in_line)) +","+ str(len(line))
words.append(words_in_line)
print words
print token_map
答案 0 :(得分:0)
运行示例会产生很大的不同。我编写了一些数据,然后将toString()
函数应用到sorted
并按升序获取其项目。如果您需要,可以添加dict
。
reverse=True
您选择存储数据的方式似乎很奇怪。如果您使用计数作为第一项的token_map = {}
words = []
lines = ['I am the very model\n', 'of the modern\n', 'major general\n']
for line in lines:
line = str.strip(line)
words_in_line = str.split(line, " ")
token_map[line] = "" + str(len(words_in_line)) +","+ str(len(line))
words.append(words_in_line)
print words
print token_map
for item in sorted(token_map.iteritems(), key=lambda item: int(item[1].split(',')[0])):
print item
,则排序更直接。
tuple