列表内容的平均值

发布于 2024-12-15 03:37:32 字数 391 浏览 0 评论 0原文

我有一个如下所示的列表:

mylist = [('a', [(0,1), (1,2), (2,3)]),
         ('b', [(0,1), (1,2), (2,3)]),
         ('c', [(0,1), (1,2), (2,3)])]

这是我在代码中进行不同操作后得到的。

是否有可能得到如下所示的输出?

newlist = [('a', [0.5, 1.5, 2.5]),
          ('b', [0.5, 1.5, 2.5]),
          ('c', [0.5, 1.5, 2.5])]

输入列表的内容为范围,输出列表是通过取每个范围中两个数字的平均值生成的。谢谢。

I have a list like the following:

mylist = [('a', [(0,1), (1,2), (2,3)]),
         ('b', [(0,1), (1,2), (2,3)]),
         ('c', [(0,1), (1,2), (2,3)])]

which I got after different operations in the code.

Is it possible to get an output like the following?

newlist = [('a', [0.5, 1.5, 2.5]),
          ('b', [0.5, 1.5, 2.5]),
          ('c', [0.5, 1.5, 2.5])]

The input list has the contents as ranges and the output list is generated by taking the average of the two numbers from each range. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-12-22 03:37:32
>>> l = [('a', [(0,1), (1,2), (2,3)]),
... ('b', [(0,1), (1,2), (2,3)]),
... ('c', [(0,1), (1,2), (2,3)])]
>>> newl = [(i[0], [(j[0]+j[1])/2 for j in i[1]]) for i in l]
>>> newl
[('a', [0.5, 1.5, 2.5]), ('b', [0.5, 1.5, 2.5]), ('c', [0.5, 1.5, 2.5])]

这适用于 Python 3;在 Python 2 中,您需要

>>> newl = [(i[0], [(j[0]+j[1])/2.0 for j in i[1]]) for i in l]

这些列表推导式等价于以下更详细的代码:

>>> newl = []
>>> for i in l:
...     temp = (i[0], [])
...     for j in i[1]:
...         temp[1].append((j[0]+j[1])/2.0)
...     newl.append(temp)
>>> l = [('a', [(0,1), (1,2), (2,3)]),
... ('b', [(0,1), (1,2), (2,3)]),
... ('c', [(0,1), (1,2), (2,3)])]
>>> newl = [(i[0], [(j[0]+j[1])/2 for j in i[1]]) for i in l]
>>> newl
[('a', [0.5, 1.5, 2.5]), ('b', [0.5, 1.5, 2.5]), ('c', [0.5, 1.5, 2.5])]

This works in Python 3; in Python 2, you need

>>> newl = [(i[0], [(j[0]+j[1])/2.0 for j in i[1]]) for i in l]

These list comprehensions are equivalent to the following, more verbose code:

>>> newl = []
>>> for i in l:
...     temp = (i[0], [])
...     for j in i[1]:
...         temp[1].append((j[0]+j[1])/2.0)
...     newl.append(temp)
来世叙缘 2024-12-22 03:37:32

列表推导式很好,但我最喜欢的 Python 技术是 map(),它将函数应用于结构的每个元素:

ls = (('a', [(0,1), (1,2), (2,3)]),
      ('b', [(0,1), (1,2), (2,3)]),
      ('c', [(0,1), (1,2), (2,3)]))

for li in ls:
    print(li[0], list(map(lambda x: sum(x)/len(x), li[1])))

正如 Tim 所说,这适用于 Python 3,您需要一个 float(len(x)) 对于 Python 2。

List comprehensions are nice, but my favorite Pythonic technique is map(), which applies a function to every element of a structure:

ls = (('a', [(0,1), (1,2), (2,3)]),
      ('b', [(0,1), (1,2), (2,3)]),
      ('c', [(0,1), (1,2), (2,3)]))

for li in ls:
    print(li[0], list(map(lambda x: sum(x)/len(x), li[1])))

As Tim says, this works for Python 3, you'd need a float(len(x)) for Python 2.

乙白 2024-12-22 03:37:32

要仅使用一个列表(有效的 python 部分),您可以使用列表理解:

numbers = [(0,1), (1,2), (2,3)]

print [sum(x) / float(len(x)) for x in numbers]

这应该让您朝着正确的方向前进,并为您提供:

[0.5, 1.5, 2.5]

To work with just one of your lists (the valid python portion), you could use a list comprehension:

numbers = [(0,1), (1,2), (2,3)]

print [sum(x) / float(len(x)) for x in numbers]

This should get you in the right direction, and gives you:

[0.5, 1.5, 2.5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文