在两个列表中偏移正数和负数,然后在Python中获得其余数字
我有两个列表,包括一系列正数或负数,两个列表可能具有不等的长度,例如:
lst1 = [2, 3, 3, 5, 6, 6, 6, 8, 10]
lst2 = [-1, -2, -2, -3, -6, -10, -11]
我想要得到的结果是:
lst_ofs = [-1, -2, 3, 5, 6, 6, 8, -11]
在两个列表中具有同样绝对值的正数和负数,被数量抵消,有什么简单的方法吗?
I have two lists consisting of a series of positive or negative numbers, and two lists may have unequal lengths, like this:
lst1 = [2, 3, 3, 5, 6, 6, 6, 8, 10]
lst2 = [-1, -2, -2, -3, -6, -10, -11]
The result I want to get is:
lst_ofs = [-1, -2, 3, 5, 6, 6, 8, -11]
Positive and negative numbers with equal absolute value in the two lists are offset by quantity, is there any easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该解决方案将每个数字的正和负实例的数量计算为最大值。必要时在每次迭代上扩展输出列表。
输出:
This solution counts the number of the positive and negative instances of each number up to the maximum. The output list is extended on each iteration as necessary.
Output:
这不是最有效的解决方案,因为循环的
每次删除两个值时启动。但这应该起作用。
This isn't the most efficient solution, because the
for
loop starts over each time two values are removed. But it should work.