计算二维列表的总和

时间:2015-10-12 18:33:44

标签: python list

我需要找到2-D List的总和,但我只能找到第一个列表的总和。有人可以指点我或指导我,我也看到人们使用柜台。我们不允许使用柜台。

firstBorder.hidden = true

我将此作为列表值

def avg(lst):
i = 0 
A = 0 
for item in lst :
   B = lst[i][A]+lst[i][A+1]+lst[i][A+2]
i = i +1
return B

当我执行时我只得到第一个列表的总和,但我需要得到整个列表的总和。

lsit = [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]

3 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些最小的更改。按照评论

var newState = state.updateIn(['widgets'], function (list) {
    return list.push(Immutable.fromJS({
        name: 'Some widget2',
        type: 'List',
        defaultDataSource: 'daily',
        dataSources: {}
    }));
});

var newState1 = state.updateIn(['widgets'], function (list) {
    return list.push(Immutable.fromJS({
        name: 'Some widget2',
        type: 'List',
        defaultDataSource: 'daily',
        dataSources: {}
    }));
});

使用内置def avg(lst): i = 0 A = 0 B = 0 # initialize for item in lst : B += lst[i][A]+lst[i][A+1]+lst[i][A+2] # Use += i = i +1 # Inside loop return B 和gen-exp的替代方案是

sum

请注意您使用>>> lsit = [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]] >>> sum(sum(i) for i in lsit) 763 查找总和

答案 1 :(得分:0)

展平列表并使用总和

pom.xml

输出

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my</groupId>
    <artifactId>automation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <testSourceDirectory>src/test/java/com/my/tests</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>

                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <updateReleaseInfo>true</updateReleaseInfo>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                    <groups>com.my.testgroups.AutoTestsGroup</groups>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release-profile</id>
            <activation>
                <property>
                    <name>performRelease</name>
                </property>
            </activation>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.12</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

答案 2 :(得分:0)

您是否只是想在lsit中获取每个元素的总和?这样做......

import numpy as np
lsit = [[95, 92, 86], [66, 75, 54], [89, 72, 100], [34, 0, 0]]
np.sum(lsit)