Python中二维数组 [英] Two-dimensional arrays in Python

查看:97
本文介绍了Python中二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了三个独立的城市的数据和我想保持每组的二维数组的数据,但我拿过去我的code的一部分,循环不断在写东西,从我的两市第一,因为我只有一个一维数组。我应该在哪里设置这些2-D阵列,以保持我的文件组织,我应该使用这样做有什么功能和参数?

阵列应该是(为每个城市3,54,每年数据的)3X54

编辑:所有在下面(即preCIP,TMIN,TMAX)在code初始变量将在他们超过19,000元的,我结束了在$ C平均每年有超过以后开始$ C。

 导入numpy的是NP
进口matplotlib.pyplot如PLT
进口大熊猫作为PD城市= ['Lubbock.txt','Erie.txt','Oslo.txt']
年= np.arange(1960,2014,1)
months_summer =范围(5,8,1)在范围X(0,len个地(市),1):    数据= np.genfromtxt(市[X],skip_header = 2,usecols =(1),DTYPE =('S8'))
    DATA2 = np.genfromtxt(市[X],skip_header = 2,usecols =(2,3,4))    #ONLY GET 1-D阵列时我要求的形状这种变量点之后    日期= pd.DatetimeIndex(数据[:])
    年= dates.year
    月= dates.month
    天= dates.day
    preCIP =数据2 [:0] / 10。
    TMAX = DATA2 [:1] / 10。
    TMIN = data2的[:,2] / 10。    tmaxF =(TMAX *(9./5。))+ 32。
    tminF =(TMIN *(9./5。))+ 32。
    precipinches = preCIP * 0.03937007874    tmax_avg = []    JJA3tmax_avg = []    JJAtmax_avg = []    DJFtmax_avg = []    在多年年:
        toavg = np.where(年==年)
        tmax_avg.append(np.average(TMAX [toavg]))
        在months_summer MO:
            sumtoavg = np.where(月== MO)
            JJA3tmax_avg.append(np.average(TMAX [sumtoavg]))
        JJAtmax_avg.append(np.average(JJA3tmax_avg))
        JJA3tmax_avg = []        dec_this_year =(年==年)及(月== 12)
        jan_next_year =(年==(YR + 1))及(月== 1)
        feb_next_year =(年==(YR + 1))及(月== 2)        wintoavg = np.where(dec_this_year&安培; jan_next_year&安培; feb_next_year)        DJFtmax_avg.append(np.average(TMAX [wintoavg]))
    tmaxmean30 = np.average(tmax_avg [1:31])
    JJAtmaxmean30 = np.average(JJAtmax_avg [1:31])
    DJFtmaxmean30 = np.average(DJFtmax_avg [1:31])#这个是我要绘制数据
    tmax_avg_dep = tmax_avg - tmaxmean30
    JJAtmax_avg_dep = JJAtmax_avg - JJAtmaxmean30
    DJFtmax_avg_dep = DJFtmax_avg - DJFtmaxmean30


解决方案

这行:

 数据[:]

创建的的副本。你需要一个的复制

 进口副本
copy.deepcopy(数据)

从文档:


  

浅层和深层的复制之间的差别是仅用于化合物的对象(即包含其他对象的对象,例如列表或类实例)相关


  
  

      
  • 浅表副本构造一个新的复合对象,然后(尽可能)插入到它在原来找到的对象引用。

  •   
  • 深拷贝构造一种新的化合物对象,然后,递归,插入复制到该原中发现的对象。

  •   

I'm reading in data for three separate cities and I want to keep each set of data in a two-dimensional array, but as I get past a part of my code, loops keep writing over things from my first two cities as I only have a one-dimensional array. Where should I set up these 2-D arrays to keep my files organized and what function and arguments should I use to do so?

Arrays should be 3X54 (3 for each city, 54 for each year of data)

EDIT: All the initial variables in the code below (i.e. precip, tmin, tmax) will have over 19,000 elements in them at the beginning which I end up averaging over each year later in the code.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

city = ['Lubbock.txt','Erie.txt','Oslo.txt']
years = np.arange(1960,2014,1)
months_summer = range(5,8,1)

for x in range(0,len(city),1):

    data = np.genfromtxt(city[x], skip_header=2, usecols=(1), dtype=('S8'))
    data2 = np.genfromtxt(city[x], skip_header=2, usecols=(2,3,4))

    #ONLY GET 1-D ARRAY WHEN I ASK FOR SHAPE OF VARIABLE AFTER THIS POINT

    dates  = pd.DatetimeIndex(data[:])
    year   = dates.year
    month  = dates.month
    day    = dates.day
    precip = data2[:,0]/10.
    tmax   = data2[:,1]/10.
    tmin   = data2[:,2]/10.

    tmaxF  = (tmax*(9./5.))+32.
    tminF  = (tmin*(9./5.))+32.
    precipinches = precip*0.03937007874

    tmax_avg = []

    JJA3tmax_avg = []

    JJAtmax_avg = []

    DJFtmax_avg = []   

    for yr in years: 
        toavg = np.where(year == yr) 
        tmax_avg.append(np.average(tmax[toavg])) 


        for mo in months_summer:
            sumtoavg = np.where(month == mo)
            JJA3tmax_avg.append(np.average(tmax[sumtoavg]))


        JJAtmax_avg.append(np.average(JJA3tmax_avg))
        JJA3tmax_avg = []

        dec_this_year = (year == yr) & (month == 12)
        jan_next_year = (year == (yr+1)) & (month == 1)
        feb_next_year = (year == (yr+1)) & (month == 2)

        wintoavg = np.where(dec_this_year & jan_next_year & feb_next_year)

        DJFtmax_avg.append(np.average(tmax[wintoavg]))


    tmaxmean30 = np.average(tmax_avg[1:31])
    JJAtmaxmean30 = np.average(JJAtmax_avg[1:31])
    DJFtmaxmean30 = np.average(DJFtmax_avg[1:31])

#THIS IS THE DATA THAT I'M PLOTTING
    tmax_avg_dep = tmax_avg - tmaxmean30
    JJAtmax_avg_dep = JJAtmax_avg - JJAtmaxmean30
    DJFtmax_avg_dep = DJFtmax_avg - DJFtmaxmean30

解决方案

This line:

data[:]

creates a shallow copy. You need a deep copy:

import copy
copy.deepcopy(data)

From the docs:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

这篇关于Python中二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆