Your first version is pretty close, so let's improve on that.
What you can do, is use a list comprehension to iterate through and convert each integer into a string. Then call join
on the resulting list of strings.
Something like the following should work:
dictionary = {
0: [2, 5],
1: [1, 4],
2: [3]
}
with open('test.txt', 'w') as f:
for value in dictionary.values():
print(' '.join([str(s) for s in value]), file=f)
Side note: I've replaced f.write
with print
, to avoid manually specifying a newline character.
If you want a trailing space character on each line, you can either do this using f-strings:
print(f"{' '.join([str(s) for s in value])} ", file=f)
or traditional string concatenation:
print(' '.join([str(s) for s in value]) + ' ', file=f)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…