Python - format and display of a list with lambda - the "list" object does not have the "map" attribute

Trying to format the list so that I can execute it in the request, but getting the "list" object does not have the "map" attribute and, of course, why? Using python 2.6 The list contains a string of names.

params = ",".join(flagged_job_names.map(lambda x: "?")) cursor.execute(sql.format(params), flagged_job_names) 
+5
source share
1 answer

map is a function, not a list method.

 params = ",".join(map(lambda x: "?", flagged_job_names)) 

By the way, you can use a list expression or a generator expression instead of map :

 params = ",".join("?" for x in flagged_job_names) 

But for this particular case, the following are possible:

 params = ",".join(["?"] * len(flagged_job_names)) params = ",".join("?" * len(flagged_job_names)) 

The latter is possible because the string ( ? ) Is single-character.

+10
source

Source: https://habr.com/ru/post/1203721/


All Articles