list - using a sub-string to identify file names in python -


i attempting combine files based on similarity in file name. have list of file names , based in list of identifiers (sub-strings) combine files.

i using:

    in identifylist:         print("\n".join(s s in filelist if i.lower() in s.lower())) 

when run script files printed in correct order. cannot manage use product of statement printing. if assign variable , print variable list of:

    <generator object <genexpr> @ longnumber> 

i looked how use generators , found confusing.

any ideas? cheers

i suspect you're getting generator objects somewhere because you're using generator expressions when want using list comprehensions.

a list comprehension looks [some_expression(variable) variable in some_iterable if some_condition(variable)]. produces list, explicit loop does:

result = [] variable in some_iterable:     if some_condition(variable):         result.append(some_expression(variable)) 

the advantage list comprehension on 1 line , can done anywhere expression allowed (like in function call) without creating variables.

a generator expression similar, replaces list iterable generator object, yields values 1 one needed in iteration. can big advantage if there lot of values produced , don't want keep them in memory @ same time, or if don't care keeping list around.

the syntax generator expression replace list comprehension's [] square brackets parentheses (or nothing if generator expression argument function, since function call's parentheses job already).

in code, call str.join using generator expression (only) argument. if want else file names match condition (rather joining them together), may want use list comprehension instead:

for in identifylist:     matches = [s s in filelist if i.lower() in s.lower())]     # list here! 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -