1

I need to change a sign from positive to negative within a tuple that's in a dictionary. So if I have 'position: (3,4)' I need to change it to 'position: (3,-4)'. This is what I have but it's not working.

for k,v in positionD.items():
    v = (v[0],-v[1])
    positionNewD[k] = v
4

2 に答える 2

0

これを試してください (Python >= 2.7 が必要です):

positionNewD = {k: (x, -y) for k, (x, y) in positionD.iteritems()}

古いバージョンの場合:

positionNewD = dict((k, (x, -y)) for k, (x, y) in positionD.iteritems())
于 2012-06-02T03:16:54.220 に答える
0

Python 3 の場合

positionNewD = {k: (x, -y) for k, (x, y) in positionD.items()}

... iteritems() の名前が items() に変更されたため

http://docs.python.org/3.1/whatsnew/3.0.html#views-and-iterators-instead-of-lists

于 2013-06-01T15:03:23.277 に答える