• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            鍵盤上的舞者

            My Email: marckywu@gmail.com
            隨筆 - 19, 文章 - 0, 評論 - 3, 引用 - 0
            數據加載中……

            一個Python文本處理程序

            文本內容為一行一個單詞,如下:
            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

            = {}

            = 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)
            {
                
            *= (SList *)malloc(sizeof(SList));
                (
            *p)->first = NULL;
            }
            //Create a node and set value
            SNode *createNode(const char *str)
            {
                SNode 
            *= 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;
            }
                
                

                    

            posted on 2009-07-22 16:33 Marcky 閱讀(645) 評論(0)  編輯 收藏 引用 所屬分類: Python

            亚洲成av人片不卡无码久久| 嫩草影院久久99| 一级a性色生活片久久无| 欧美一级久久久久久久大片| 欧美亚洲日本久久精品| 亚洲国产精品成人久久| 久久亚洲国产欧洲精品一| 久久午夜福利电影| 色欲av伊人久久大香线蕉影院| 精品熟女少妇av免费久久| 天天做夜夜做久久做狠狠| 色婷婷综合久久久中文字幕| 久久er国产精品免费观看8| 久久人人爽爽爽人久久久| 久久国产香蕉一区精品| 色8久久人人97超碰香蕉987| 久久WWW免费人成—看片| 久久亚洲日韩精品一区二区三区| 99国内精品久久久久久久| 欧美大香线蕉线伊人久久| 伊人情人综合成人久久网小说| 久久精品无码专区免费青青| 国产精品久久久香蕉| 久久一区二区三区免费| 一级做a爱片久久毛片| 久久精品一本到99热免费| 久久99久久99精品免视看动漫| 国产亚洲精午夜久久久久久 | 久久综合久久美利坚合众国| 久久这里只有精品首页| 精品久久人妻av中文字幕| 色欲综合久久躁天天躁蜜桃| 久久精品无码一区二区WWW| 无码精品久久一区二区三区| 99久久精品免费看国产免费| 91久久成人免费| 国产精品欧美亚洲韩国日本久久| 久久精品一区二区| 66精品综合久久久久久久| 国产精品久久久99| 久久成人国产精品一区二区|