文本内容为一行一个单词,如下:
some
are
born
great
some
achieve
greatness
and
some
have
greatness
thrust
upon
them
处理后的输出结果如下:
achieve 1
and 1
are 1
born 1
great 1
greatness 2
have 1
some 3
them 1
thrust 1
upon 1
Python代码:
#!/usr/bin/env python
d = {}
f = file('data.txt')
while True :
line = f.readline().strip('\n')
if len(line) == 0 :
break
if line in d.keys() :
d[line] += 1
else :
d[line] = 1
li = sorted(d.items(), key = lambda d : d[0])
for e in li :
print '%-10s %d' % e
附c版代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORDLEN 20
typedef struct _SNode {
char word[WORDLEN]; /* 存放单词 */
int count; /* 计数器 */
struct _SNode *next;
} SNode;
typedef struct _SList {
SNode *first;
} SList;
//Create a empty list
void createList(SList **p)
{
*p = (SList *)malloc(sizeof(SList));
(*p)->first = NULL;
}
//Create a node and set value
SNode *createNode(const char *str)
{
SNode *p = NULL;
p = (SNode *)malloc(sizeof(SNode));
strncpy(p->word, str, WORDLEN); /* 有可能截短单词 */
p->word[WORDLEN - 1] = '\0';
p->count = 1;
p->next = NULL;
return p;
}
void insertNode(SList *listp, char *wordp)
{
SNode *p1 = listp->first;
SNode *p2 = p1;
if (p1 == NULL) { /* list is empty, put the node into first */
listp->first = createNode(wordp);
return;
}
while (p1 != NULL) {
/*单词和当前节点相等,计数器加1*/
if (strcmp(p1->word, wordp) == 0) {
p1->count += 1;
return;
}
/*当前节点大于了读取的单词,将单词插入此节点之前*/
if (strcmp(p1->word, wordp) > 0) {
SNode *nodep = createNode(wordp);
nodep->next = p1;
if (p1 == listp->first) /* 在第一个节点之前插入 */
listp->first = nodep;
else
p2->next = nodep; /* 在非第一个节点之前插入 */
return;
}
p2 = p1;
p1 = p1->next;
}
if (p1 == NULL) { /* 读取的单词比所有节点都大,插入到链表末尾 */
p2->next = createNode(wordp);
}
return;
}
void printList(SList *p)
{
SNode *temp = p->first;
while (temp != NULL) {
printf("%-20s %d\n", temp->word, temp->count);
temp = temp->next;
}
}
void freeList(SList *p)
{
SNode *p1 = p->first;
SNode *p2 = p1;
while (p1 != NULL) {
p2 = p1;
p1 = p1->next;
free(p2);
}
free(p);
}
int main(void)
{
FILE *file;
char wordbuff[WORDLEN];
SList *wordlist;
createList(&wordlist);
file = fopen("data.txt", "r");
while (fgets(wordbuff, WORDLEN, file)) {
wordbuff[strlen(wordbuff) - 1] = '\0'; /* 除去单词最后的换行符 */
insertNode(wordlist, wordbuff);
}
printList(wordlist);
freeList(wordlist);
fclose(file);
return 0;
}