简体   繁体   中英

Rewrite For Loop Using List Comprehension and Conditional Statements

I do not know how to rewrite the following loop using list/dictionary comprehension and conditional statements:

for i, sig_id in enumerate(signal_ids):
    if sig_id in aux_dict.keys():
        aux_dict[sig_id] = signal_values[i]

signal_ids and signal_values lists have same size. First element of one list corresponds to the first of the other list, and so on.

您可以使用以下dict理解来重现此内容

aux_dict = {sig_id : signal_values[i] for i, sig_id in enumerate(signal_ids) if sig_id in aux_dict}

您可以尝试以下方法:

aux_dict = {sig_id: signal_values[i] for i, sig_id in enumerate(signal_ids) if sig_id in aux_dict.keys()}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM