簡體   English   中英

嵌套循環未遍歷整個列表

[英]Nested loop not iterating through entire list

我的嵌套循環有問題。

我有一個標題列表和一個關鍵字列表,我想創建一個新列表,其中包含包含關鍵字列表中任何關鍵字的所有標題。

我的代碼如下:

更新了測試數據

test_key_list = ['Argentinian Americans', 'Belizean Americans', 'Chicano Americans', 'Latino Americans', 'Latine', 'Bolivian Americans', 'Boricuas', 'Brazilian Americans', 'Chilean Americans', 'Colombian Americans', 'Costa Rican Americans', 'Costarisences', 'Cuban Americans', 'Dominican Americans', 'Ecuadorian Americans', 'Afro-Hispanics', 'Afro-Latinos', 'Guatemalan Americans', 'Hispanic Americans', 'Hispanos', 'Honduran Americans', 'Mejicano', 'Mexican Americans', 'Nicaraguan Americans', 'Panamanean Americans', 'Paraguayan Americans', 'Peruvian Americans', 'Puerto Rican Americans', 'Salvadoran Americans', 'Tejano', 'Uruguayan Americans', 'Venezuelan Americans', 'Argentinians', 'Belizeans', 'Chicanos', 'Latin Americans', 'Chicanas', 'Bolivians', 'Chicanx', 'Brazilians', 'Chileans', 'Colombians', 'Costa Ricans', 'Latinos', 'Cubans', 'Dominicans', 'Ecuadorians', 'Latinas', 'Afro-Latinas', 'Guatemalans', 'Hispanics', 'Latinx', 'Hondurans', 'Mexicano', 'Mexicans', 'Nicaraguans', 'Panamaneans', 'Paraguayans', 'Peruvians', 'Puerto Ricans', 'Salvadorans', 'Texano', 'Uruguayans', 'Venezuelans', 'Argentinos', 'Belizeanos', 'Bolivianos', 'Puerto Ricans', 'Brasileños', 'Chilenos', 'Colombianos', 'Costarricences', 'Costarricences', 'Cubanos', 'Dominicanos', 'Ecuatorianos', 'Guatemaltecos', 'Mexican Americans', 'Hondureños', 'Nicaragüenses', 'Panameños', 'Paraguayos', 'Peruanos', 'Puertorriqueños', 'Salvadoreños', 'Uruguayos', 'Venezolanos', 'latinx', 'latina', 'latino', 'latine', 'hispanic', 'hispanos']
test_title_list = ['The University library "Antonio Machado Ruiz" and its support for the teaching and educational process in the forestry career at the university of Granma, Cuba', 'The right to information and the right of information', 'Relational dimension of social capital in public libraries: A case study', 'Diagnosis of information literacy skills of professionals from the National Library of Cuba', 'Unravelling the basic concepts and intents of misbehavior in post-truth society', "Notes on Suzanne Briet and her “Qu'est-ce que la documentation?”", 'Critical spaces of social responsibility for Digital Humanities', 'Undergraduate research: Evaluation of its quality through theses', 'Information literacy, bastion in the post-truth era', 'Research on archival science, library science and information science in Colombia: 2007-2017', 'Approach to the Social Epistemology as theoretical project for Library Science', 'Web 2.0 in the Nordic libraries', 'Information system: Conceptual and methodological approach', 'Written information and the iconic representation of death in art', 'Professional perspectives in cloud computing environments', 'Information science in Uruguay (2013-2017): Research lines and academic output', 'Application and improvement of graph analysis in financial intelligence reports', 'Traditional, digital, on-demand and self-surfaced print publishing. Four models of book publishing requiring different evaluations', 'Cuban research in Information Sciences: The case of postgraduate studies (2008-2018)', 'Citation networks of Ibero-American journals of Library and Information Science in Scopus', 'Towards an Ibero-American informational thinking', 'Domain analysis on risks and climate in Web of Science', 'The information science in Brazil: Research mapping and institutional outlook', 'Knowledge audit oriented to the main process and human capital. A case study in the National Library of Cuba', 'Publication trends of the journal Cuba (1962-1969): A bibliometric analysis', 'Research on library and information science in Peru: A state of art', 'Information Science in Portugal in the first decades of the 21st century: A preliminary approach to an Ibero-American cartography', 'Impact of emerging Library and Information Science journals on the Web of Science (2017)', 'Procedure proposal to self-manage health knowledge from the web, through mobile devices and computers', "Cuban magazines of the 60's and the teaching of the history of Cuba: Course for school librarians", 'Altmetric study of the social repercussion of open access brazilian scientific journals', 'Libraries journal. Annals of research: Notes on the evolution of structure and content', 'Labor competencies for commercialization in a science, technology and innovation organization: Isotope center', 'Behavior of scientific production on digital marketing indicated in the scopus database, in the period 2016-2019', 'ASCUBI on its 35th anniversary: Development of Library Science', 'Quality evaluation parameters for electronic newsletters of the national medical library of Cuba', 'The research worker and the library.', "The graduate student's use of the subject catalog", 'Regional library centers tomorrow; a symposium', 'COLLEGE and university library statistics', 'Centralized cataloging in college and university libraries.', 'The reference survey as an administrative tool.', "What professional librarians expect from administrators: One librarian's view", 'Library skills, critical thinking, and the teacher-training curriculum', 'A quarter century of Advanced Data Processing in the University Library', 'Cooperation, collection management, and scientific journals', 'The configuration of reference in an electronic environment', 'Pay equity for women in academic libraries: An analysis of ARL salary surveys, 1976/77-1983/84', 'Automating bibliographic research: Identifying American fiction, 1901-1925', 'Special collections: Strategies for support in an era of limited resources','Immigrant rights advocacy as records literacy in Latinx communities','Access to the inter

test = []
for title in title_list:
    for key in key_list:
        if key in title:
            test.append(title)

我在評論中添加了這個,但我想我也會把它放在這里:

我為每個人添加了一個小樣本集,但它似乎工作得很好。 也就是說,我知道,例如,通過搜索原始數據,關鍵字 hispanic 應該在完整數據集中出現 30 多次。 也就是說,當我運行完整的循環時,我得到了所有關鍵字的大約 30 個匹配項。

例如,我正在使用我也擁有的作者分配的關鍵字列表 ( keyword_list ) 來執行此操作。 我將其與其他關鍵字( kw_list )進行比較。

keyword_matches = []
count = 0

for item in keywords_list:
    if "Cuban" in item:
        keyword_matches.append(item)
        count+=1

print(count)

這將打印 6。

keyword_matches = []
count = 0

for item in keywords_list:
    if "Mexican" in item:
        keyword_matches.append(item)
        count+=1

print(count)

這將打印 2。

keyword_matches = []
count = 0

for item in keywords_list:
    if "Latino" in item:
        keyword_matches.append(item)
        count+=1

print(count)

這將打印 15。

我可以繼續搜索不同的關鍵字,並且可以越來越多地進行計數。 也就是說,當我為整個標題列表和所有關鍵字運行循環時,我得到了這個:

keyword_matches = []
count = 0

for item in keywords_list:
    for key in kw_list:
        if key in item:
            keyword_matches.append(item)
            count+=1

print(count)

這將打印 8。與這些關鍵字匹配的文章超過 8 篇。

我發現它沒有遍歷所有標題。 它停在某處。 我創建了另一個列表來附加else值,並且返回的項目比我在初始標題列表中的項目要多得多。

我應該怎么做才能確保列表遍歷列表中的所有標題並將每個標題與關鍵字列表中的每個關鍵字進行比較?

如果key_listlisttuple ,則此代碼是可以的。 如果key_listgenerator ,它可能會崩潰,因為您只能迭代generator一次。 您可以創建key_real_list = list(key_list)並稍后在迭代中使用它(但請注意,如果key_list是生成器,它無論如何都會過期)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM