在元组列表中使第一个值为正

时间:2012-12-03 23:00:23

标签: python list tuples

我有以下元组列表:

>>> 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)

6 个答案:

答案 0 :(得分: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)]

答案 1 :(得分:0)

另一种方法:

to_add = 0 - min(list_of_tuples)[0]
new_list = [(i[0] + to_add, i[1]) for i in list_of_tuples]

答案 2 :(得分: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")

答案 3 :(得分: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)

答案 4 :(得分: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)]

答案 5 :(得分:-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)]

希望这有帮助