繁体   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