如何计算Python中数组的平均值?

时间:2019-05-30 11:46:47

标签: python python-3.x

说我们有以下内容:

for (ushort i = 43001; i < 65535; i += 2)
{
   if (!dict.ContainsKey(i) && !dict.ConainsKey(i + 1))
   {
       return //whatever;
   }
}

如何计算数组的平均值并将其放入新数组中?如下所示:

a = [(R1, G1, B1),
     (R2, G2, B2),
     (R3, G3, B3)]

其中a = [(R, B, G)] RGBR1R2等的平均值。

3 个答案:

答案 0 :(得分:3)

可以理解:

import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';

import { VDataTable, VProgressLinear } from "vuetify-tsx";
import { VDataTable as Test } from 'vuetify/lib';



let c = 0;

@Component
export default class RadarAdminPage extends Vue {


    headers = [
        {
            text: "name",
            align: "left",
            sortable: true,
            value: "name"
        },
        {
            text: "Queue Name",
            value: "queue"
        }
    ]

    search = null;
    searched = [] as Array<any>;
    items = [ ] as Array<any>;

    addItem() {
        this.items.push({
            id: c++,
            name: "Paxon Lotterington",
            email: "plotteringtoni@netvibes.com",
            gender: "Female",
            title: "Marketing Assistant"
        });

        this.searched = this.items;
    }
    searchOnTable() {
        console.log("a");
    }
    created() {
        this.searched = this.items;
    }
    render() {
        console.log(this)
        return (
            <div class="row">
                <div class="col-12">

                    <VDataTable itemKey="id" slot="items:props" items={this.searched} headers={this.headers} scopedSlots={{
                        items: function (prop) {

                            return [
                                <td>{prop}</td>,
                                <td class="text-xs-right">vb</td>
                            ]
                        }
                    } as any}>
                        <VProgressLinear slot="progress" color="blue" indeterminate={true} />

                    </VDataTable>

                    <div>
                        <md-table value={this.searched} md-sort="name" md-sort-order="asc" md-card md-fixed-header scopedSlots={
                            {
                                "md-table-empty-state": (prop)=> {
                                    return (
                                        <md-table-empty-state
                                            md-label="No users found" md-description="`No user found for this '${search}' query. Try a different search term or create a new user.`">
                                            <md-button class="md-primary md-raised" onClick={this.addItem}>Create New User</md-button>
                                        </md-table-empty-state>
                                    )
                                },
                                "md-table-row": function (prop) {

                                    return (
                                        <md-table-row slot="md-table-row" >
                                            <md-table-cell md-label="ID" md-sort-by="id" md-numeric>{prop.item.id}</md-table-cell>
                                            <md-table-cell md-label="Name" md-sort-by="name">{prop.item.name}</md-table-cell>
                                            <md-table-cell md-label="Email" md-sort-by="email"> {prop.item.email}</md-table-cell>
                                            <md-table-cell md-label="Gender" md-sort-by="gender"> {prop.item.gender}</md-table-cell>
                                            <md-table-cell md-label="Job Title" md-sort-by="title"> {prop.item.title}</md-table-cell>
                                        </md-table-row>
                                    )
                                }
                            }
                        }>
                            <md-table-toolbar>
                                <div class="md-toolbar-section-start">
                                    <h1 class="md-title">Users</h1>
                                </div>

                                <md-field md-clearable class="md-toolbar-section-end">
                                    <md-input placeholder="Search by name..." vModel={this.search} onInput={this.searchOnTable} />
                                </md-field>
                            </md-table-toolbar>


                        </md-table>
                    </div>


                </div >
            </div >
        )
    }
}

例如:

avgs = [sum(vals)/len(a) for vals in zip(*a)]

答案 1 :(得分:1)

import numpy as np

x = np.random.rand(3,3)
y = x.mean(axis=0)
print(y)

答案 2 :(得分:0)

我会使用numpy

import numpy as np
a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
arr = np.array(a)
means = means = np.mean(arr, axis=0)
print(means)

# prints array([4., 5., 6.])

如果您要以格式(即仅包含一个元组的列表)获取结果,则可以

result = [tuple(means)]