oxio oxio - 23 days ago 7
Python Question

Python: Map with class method which requires arguments

I need to call a method for a set of arguments with PyQt5's

QTableWidget
, particularly
PyQt5.QTableWidget.setItem
.

I have this list of strings,

row = ["a","b","c","d"]


and I need to put every element in a column of the table, except for the first. What I tried is

items = [ QTableWidgetItem(r) for r in row[1:] ]
arguments = zip([rowNumber+1]*(len(row)-1), range(1,len(row)), items)
putItem = lambda x:tableWidget.setItem(*x)
import itertools
itertools.starmap(putItem, arguments)


I also tried with map and no zip of the arguments, but nothing seems to work. I know I could do a
for
loop on the arguments, but I wanted the "more elegant" map solution.

The main issue is that no information is displayed in the table, though there are no error reports nor warning nor anything.

Thanks!

Answer

A loop is more elegant although not as terse, IMHO.

When you create a generator (as with starmap), you have to consume it to execute code. For example, pass it to list().

Comments