set()
takes values from the iteration and adds each value separately. You went through int
, which is not iterable at all.
You would need to wrap your individual values in an iterable, for example a list:
list[i][j] = set([list[i][j]])
- {...}
:
list[i][j] = {list[i][j]}
, , , :
def to_sets(l):
return [[{i} for i in row] for row in l]
, enumerate()
:
def to_sets_inplace(l):
for row in l:
for i, value in enumerate(row):
row[i] = {value}
, , . :
>>> def to_sets(l):
... return [[{i} for i in row] for row in l]
...
>>> demo = [[0, 1, 2, 3, 4, 0, 6, 7, 8], [1, 4, 5]]
>>> def to_sets_inplace(l):
... for row in l:
... for i, value in enumerate(row):
... row[i] = {value}
...
>>> to_sets(demo)
[[{0}, {1}, {2}, {3}, {4}, {0}, {6}, {7}, {8}], [{1}, {4}, {5}]]
>>> to_sets_inplace(demo)
>>> demo
[[{0}, {1}, {2}, {3}, {4}, {0}, {6}, {7}, {8}], [{1}, {4}, {5}]]