一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

1022 Digital Library (30 分)python

 印度阿三17 2021-02-07

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.
Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10?4??) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

Line #1: the 7-digit ID number;
Line #2: the book title -- a string of no more than 80 characters;
Line #3: the author -- a string of no more than 80 characters;
Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
Line #5: the publisher -- a string of no more than 80 characters;
Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:

1: a book title
2: name of an author
3: a key word
4: name of a publisher
5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print Not Found instead.

這道題我使用python來(lái)寫(xiě),和大家學(xué)習(xí)一下,首先我們先補(bǔ)充幾個(gè)知識(shí)點(diǎn)判斷字典的key是否存在我們使用dict.get(key)是返回1,不是返回0

我們還可以直接使用setdefault方法,如果key存在我們就什么都不做,如果key不存在就創(chuàng)建一個(gè)key

要知道value是可以一個(gè)列表,所以我們將符合這個(gè)要搜索的添加到這個(gè)列表中


邏輯分析:
首先我們創(chuàng)建一個(gè)列表字典,及有多個(gè)字典類型的列表,我們將搜索的關(guān)鍵字作為鍵(key)所映射出的對(duì)應(yīng)書(shū)的ID號(hào),這樣我們只要訪問(wèn)關(guān)鍵字就可以得到對(duì)應(yīng)的結(jié)果。因?yàn)槊勘緯?shū)都有六個(gè)標(biāo)簽,所以我們創(chuàng)建五個(gè)字典。代表五種屬性。

當(dāng)我們每一本輸入,就將對(duì)應(yīng)的屬性中添加書(shū)的特性并映射書(shū)名,比如輸入
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011

保存在字典里的數(shù)據(jù)是這樣的
1: a book title
{‘The Testing Book’: [‘1111111’, ‘2222222’], ‘Another Testing Book’: [‘3333333’]}

2: name of an author
{‘Yue Chen’: [‘1111111’, ‘3333333’], ‘CYLL’: [‘2222222’]}

3: a key word
{‘test’: [‘1111111’, ‘3333333’], ‘code’: [‘1111111’, ‘3333333’], ‘debug’: [‘1111111’, ‘2222222’], ‘sort’: [‘1111111’, ‘3333333’], ‘keywords’: [‘1111111’, ‘3333333’, ‘2222222’], ‘book’: [‘2222222’]}

4: name of a publisher
{‘ZUCS Print’: [‘1111111’], ‘ZUCS Print2’: [‘3333333’, ‘2222222’]}

5: a 4-digit number representing the year
{‘2011’: [‘1111111’, ‘2222222’], ‘2012’: [‘3333333’]}

我們處理a key word需要單獨(dú)判斷,因?yàn)橐槐緯?shū)可能有不同的關(guān)鍵詞,不像其他屬性只有唯一個(gè),我們使用split將關(guān)鍵分開(kāi)。

最后我將要輸出的ID號(hào)先排序,再逐個(gè)輸出。

n = int(input(""))

data = [{}, {}, {}, {}, {}]#數(shù)據(jù)存放

for i in range(n):#輸入書(shū)籍
    id = input("")#書(shū)籍的id號(hào)
    for j in range(5):#書(shū)籍對(duì)應(yīng)的屬性
        belong = input("")#書(shū)籍屬性
        if j == 2:#單獨(dú)判斷a key word ,其他屬性對(duì)于書(shū)是唯一的,但寫(xiě)法一樣。
            for keys in belong.split(' '):#a key word分成若干關(guān)鍵字
                try:
                    data[j][keys].append(id)#存在將id添加對(duì)應(yīng)關(guān)鍵字中
                except:
                    data[j].setdefault(keys, [id])#不存在就創(chuàng)建關(guān)鍵字
            continue
        try:
            data[j][belong].append(id)#同上
        except :
            data[j].setdefault(belong,[id])
            
n=int(input(""))
for i in range(n):#查找
    find=input("")
    print(find)
    if(data[int(find[0])-1].get(find[3:])):#如果找到才輸出
        data[int(find[0])-1][find[3:]].sort()#先排序
        for id in data[int(find[0])-1][find[3:]]:#逐個(gè)輸出
            print(id)
    else:#找不到就輸出Not Found
        print("Not Found")

在這里插入圖片描述
希望對(duì)大家有幫助吧

來(lái)源:https://www./content-1-849901.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    国产亚洲欧美一区二区| 麻豆印象传媒在线观看| 麻豆国产精品一区二区三区| 成人精品一区二区三区在线| 中文字幕日韩欧美一区| 日本人妻精品中文字幕不卡乱码 | 日本理论片午夜在线观看| 99久只有精品免费视频播放| 99香蕉精品视频国产版| 国产色第一区不卡高清| 日韩成人高清免费在线| 欧美一区日韩一区日韩一区| 欧美日韩一区二区综合| 国产亚洲二区精品美女久久 | 亚洲天堂一区在线播放| 久热这里只有精品九九| 日韩亚洲精品国产第二页| 熟女中文字幕一区二区三区| 老熟妇乱视频一区二区| 人妻巨大乳一二三区麻豆| 日本国产欧美精品视频| 有坂深雪中文字幕亚洲中文| 国产精品99一区二区三区| 日韩无套内射免费精品| 蜜桃传媒视频麻豆第一区| 国产又色又粗又黄又爽| 国产又粗又硬又大又爽的视频| 黄色美女日本的美女日人| 国产日韩精品欧美综合区| 99久久精品免费精品国产| 国产精品国产亚洲区久久| 日韩国产精品激情一区| 国产精品日韩欧美一区二区 | 国产性情片一区二区三区| 亚洲男女性生活免费视频| 国产女同精品一区二区| 国产日本欧美韩国在线| 欧美日韩国产另类一区二区| 国产又粗又爽又猛又黄的 | 亚洲欧美国产网爆精品| 精品国产av一区二区三区不卡蜜|