Python-循环外的平均值

时间:2019-09-12 17:14:53

标签: python numpy

我有2个NumPy数组,如下所示:

import numpy as np
a = np.array([1, 2, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8])
b = np.array([2, 1, 1, 9, 9, 9, 7, 5, 4, 8, 6, 5, 4, 4, 7, 2, 1, 1, 9, 9])

和2个常数:

c = 6
d = 3

我每次都从数组a中提取一个数组:

    a中的
  • 个元素小于c,连续2倍以上,

  • b中的元素小于d,连续两次以上。

为此,我使用以下代码:

mask = ~((a < c) & (b < d))
split_indices = np.where(mask)[0]
for subarray in np.split(a, split_indices + 1):
    if len(subarray) > 2:
        x=(subarray[:-1])
        print(x)

现在,我每次满足条件时都需要计算a - x的累计和。为此,我在最后一个if循环中添加了以下代码:

xx = np.cumsum(sum(c) - x))

输出137

我的问题是我需要知道所有xx的平均值。因此,通过这个最小的示例,结果值将为10。如果我将命令np.mean(xx)放入循环内甚至循环外,它将只计算最后一个xx的平均值,而不是每次xx的平均值。环。有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

import React from 'react';
import{View,SafeAreaView,Text,Image,ScrollView} from 'react-native';
import Icons from 'react-native-vector-icons/Ionicons'; 
import { createStackNavigator} from 'react-navigation-stack';  
import {createAppContainer} from 'react-navigation'

import ListofHome from '../component/props/Index'
import Navbar from '../navigation/Navbar'
import Detail from './Details'
import { TouchableOpacity } from 'react-native-gesture-handler';


class Home extends React.Component{
    render(){
        return(
            <View style={{
                flex:1,
                backgroundColor: '#f5fcff',
            }}>
                <Navbar/>

            <ScrollView style={{flex:1}}>
                <View style={{marginTop:3,paddingHorizontal:20,flexDirection:'row',justifyContent:'space-between'}}>
                    <Text style={{fontWeight:'800',color:'red'}}>
                        JOB POSTS
                    </Text>
                    <Text>View more..</Text>
                </View>
                <TouchableOpacity onPress={() => {
            /* 1. Navigate to the Details route with params */
            this.props.navigation.navigate('Details', {
              itemId: 86,
              otherParam: 'anything you want here',
            });
          }}>
                    <ListofHome
                    name="The list of master plans about me that is"/>
                </TouchableOpacity>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>
                <ListofHome name="About your page that is do"/>

            </ScrollView>

            </View>


        )
    }
}
const AppNavigator = createStackNavigator(  
    {  
       First:{
         screen:Detail,
       }

    } 
  );  
  export default (Home);