• <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
            數(shù)據(jù)加載中……

            一個Python文本處理程序

            文本內(nèi)容為一行一個單詞,如下:
            some
            are
            born
            great
            some
            achieve
            greatness
            and
            some
            have
            greatness
            thrust
            upon
            them

            處理后的輸出結(jié)果如下:
            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;                       /* 計數(shù)器 */
                
            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) {
                    
            /*單詞和當前節(jié)點相等,計數(shù)器加1*/
                    
            if (strcmp(p1->word, wordp) == 0) {
                        p1
            ->count += 1;
                        
            return;
                    }
                    
            /*當前節(jié)點大于了讀取的單詞,將單詞插入此節(jié)點之前*/
                    
            if (strcmp(p1->word, wordp) > 0) {
                        SNode 
            *nodep = createNode(wordp);

                        nodep
            ->next = p1;
                        
            if (p1 == listp->first) /* 在第一個節(jié)點之前插入 */
                            listp
            ->first = nodep;
                        
            else
                            p2
            ->next = nodep; /* 在非第一個節(jié)點之前插入 */
                        
            return;
                    }

                    p2 
            = p1;
                    p1 
            = p1->next;
                }

                
            if (p1 == NULL) {           /* 讀取的單詞比所有節(jié)點都大,插入到鏈表末尾 */
                    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 閱讀(639) 評論(0)  編輯 收藏 引用 所屬分類: Python

            人妻少妇精品久久| 久久人人爽人人澡人人高潮AV| 中文字幕乱码久久午夜| 久久国产精品无码HDAV| 国产成人久久精品二区三区| 亚洲精品无码久久久久AV麻豆| 亚洲国产精品无码久久98| 九九久久99综合一区二区| 一极黄色视频久久网站| 久久er99热精品一区二区| 蜜臀久久99精品久久久久久| 久久久精品人妻一区二区三区蜜桃| 国产免费久久精品99久久| 性色欲网站人妻丰满中文久久不卡| 国产三级精品久久| 久久天堂AV综合合色蜜桃网 | 精品永久久福利一区二区| 国产AV影片久久久久久| 看久久久久久a级毛片| 亚洲性久久久影院| 国产香蕉97碰碰久久人人| 久久精品国产久精国产| 久久精品aⅴ无码中文字字幕不卡| 国产午夜精品久久久久九九| 国产婷婷成人久久Av免费高清| 久久亚洲AV无码精品色午夜| 国产激情久久久久影院老熟女免费| 久久综合亚洲欧美成人| 久久免费看黄a级毛片| 午夜精品久久久久久影视riav| 国产激情久久久久影院小草 | 国产成人综合久久精品红| 久久夜色撩人精品国产| 99久久国产综合精品网成人影院| 成人国内精品久久久久影院| 国内精品久久久久影院日本| 久久久久人妻精品一区二区三区| 国内精品久久久久影院日本| 久久精品国产亚洲综合色| 久久青青草原国产精品免费| 久久99国产精品久久99|