0

我有以下元组列表:

>>> list_of_tuples = [(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4), (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1), (-1, -2), (0, -2)]

我想要以下内容:

>>> addvalues(list_of_tuples)
[(2, 0), (2, -1), (3, -1), (3, -2), (3, -3), (3, -4), (2, -4), (2, -3), (1, -3), (0, -3), (0, -2), (0, -1), (1, -1), (1, -2), (2, -2)]
#original
>>> list_of_tuples
[(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4), (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1), (-1, -2), (0, -2)]

对于list_of_tuples 中的元组,元组[0] 中的最小值是-2,我想为每个元组[0] 添加一个值,直到最小值为0,你是怎么做到的?(通常最低值不需要是-2)

4

6 回答 6

3

可能有更好的方法,但这很有效。找到最小的项目。如果小于 0,则将其添加到每个元组中的第一项。

list_of_tuples = [(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4), (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1), (-1, -2), (0, -2)]
to_add = min(list_of_tuples)[0]
if to_add < 0:
    list_of_tuples = [(a[0]-to_add, a[1]) for a in list_of_tuples]

# [(2, 0), (2, -1), (3, -1), (3, -2), (3, -3), (3, -4), (2, -4), (2, -3), (1, -3), (0, -3), (0, -2), (0, -1), (1, -1), (1, -2), (2, -2)]
于 2012-12-03T23:07:22.930 回答
0

您可以执行以下操作:-

list_of_tuples = [(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4), (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1), (-1, -2), (0, -2)]

# get the min of x
minx = min(x for x, y in list_of_tuples)

# create the new list
new_tuples = [(x - minx, y) for x, y in list_of_tuples]

# do a test
assert new_tuples == [(2, 0), (2, -1), (3, -1), (3, -2), (3, -3), (3, -4), (2, -4), (2, -3), (1, -3), (0, -3), (0, -2), (0, -1), (1, -1), (1, -2), (2, -2)]
print("Passed")
于 2012-12-03T23:12:42.760 回答
0

你需要找到最小值然后减去它。

min_ = min(l, key=lambda x:x[0])[0]

这样,它首先将每个元素传递给函数“key”,然后找到最小值。由于传递的函数返回元组的第一个元素,您可以看到它为什么起作用。(我展示了如何使用 key 参数;在这种情况下你不需要它,因为 min 已经首先检查了元组的第一个元素。但是,如果你想考虑第二个,或者他们的产品,那就是这样去)

修改列表,如果我理解正确你想要做什么,你可以做

[(x[0] + min_, x[1]) for x in l]

这称为列表推导,相当于

for x in l:
    y = (x[0] + min, x[1])
    new_list.append(y)
于 2012-12-03T23:13:57.630 回答
0

如果你需要处理正数/负数的东西,你可以试试这个。它在元组列表中找到最小的 x 值,如果该值小于 0,则取绝对值并将所有数字增加该数字。如果最小数字大于 0,则将数字增加 0(即不改变任何内容)。返回的结果是更新后的元组列表。

In [65]: def addvalues(l):
    min_x = min(l, key=lambda x: x[0])[0] # x[1] to work with the second element
    min_x = abs(min_x) if min_x < 0 else 0
    return [tuple((x+min_x, y)) for x, y in l]
   ....:

In [69]: addvalues(list_of_tuples)
Out[69]:
[(2, 0),
 (2, -1),
 (3, -1),
 (3, -2),
 (3, -3),
 (3, -4),
 (2, -4),
 (2, -3),
 (1, -3),
 (0, -3),
 (0, -2),
 (0, -1),
 (1, -1),
 (1, -2),
 (2, -2)]
于 2012-12-03T23:15:19.180 回答
0

另一种方法:

to_add = 0 - min(list_of_tuples)[0]
new_list = [(i[0] + to_add, i[1]) for i in list_of_tuples]
于 2012-12-03T23:10:21.100 回答
-1
In [52]: list_of_tuples = [(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4), (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1), (-1, -2), (0, -2)]

In [53]: to_add = min(list_of_tuples, key=operator.itemgetter(0))[0] *-1

In [54]: to_add
Out[54]: 2

In [55]: [tuple([t[0]+2,t[1]]) for t in list_of_tuples]
Out[55]: 
[(2, 0),
 (2, -1),
 (3, -1),
 (3, -2),
 (3, -3),
 (3, -4),
 (2, -4),
 (2, -3),
 (1, -3),
 (0, -3),
 (0, -2),
 (0, -1),
 (1, -1),
 (1, -2),
 (2, -2)]

In [56]: list_of_tuples # input unchanged
Out[56]: 
[(0, 0),
 (0, -1),
 (1, -1),
 (1, -2),
 (1, -3),
 (1, -4),
 (0, -4),
 (0, -3),
 (-1, -3),
 (-2, -3),
 (-2, -2),
 (-2, -1),
 (-1, -1),
 (-1, -2),
 (0, -2)]

希望这可以帮助

于 2012-12-03T23:06:50.680 回答