MartinGGWW MartinGGWW - 1 month ago 5x
Python Question

What does this python codes mean?

What does this python codes mean? New to python. THX!

benchmark_sets_list = [
'%s: %s' %
(set_name, benchmark_sets.BENCHMARK_SETS[set_name]['message'])
for set_name in benchmark_sets.BENCHMARK_SETS]


This part...

for set_name in benchmark_sets.BENCHMARK_SETS

...will grab the set names from benchmark_sets.BENCHMARK_SETS and it will keep them one by one into a set_name variable.

After that, it will be able to know the values from this line...

(set_name, benchmark_sets.BENCHMARK_SETS[set_name]['message'])

...because set_name will have a value. That part will return two things, set_name and benchmark_sets.BENCHMARK_SETS[set_name]['message']. Probably those two things will be both strings.

Then, those %s you see in this line...

'%s:  %s' %

...will be replaced by the value of set_name and benchmark_sets.BENCHMARK_SETS[set_name]['message'] respectively. That will generate an string like this one: "foo: bar", being "foo" the value of set_name and "bar" the value of benchmark_sets.BENCHMARK_SETS[set_name]['message'].

In order for you to understand what happened there, this is a simple example:

"%s %s %s" % (first_elem, second_elem, third_elem)

That code will replace the first %s with the value of first_elem The second %s with the value of second_elem, and the third %s with the value of third_elem.

And finally that string will be added to the list which is being constructed. So, at the end you will have an list more or less like this one:

["foo: bar", "wop: wap", "bing: bang"]