2016-10-07 80 views
0

我創建了一個字典,對速度,溫度和海拔高度值:功能使用字典作爲參數

mach_dict = dict(velocity=[], altitude=[], temperature=[]) 

我使用存儲值在爬升,巡航和降落段飛平原。

mach_dict = {'velocity': [0, 300, 495, 500, 300], 'altitude': [288.15, 288.15, 288.15, 288.15, 288.15], 'temperature': [0, 0, 50, 50, 50]} 

我需要創建一個函數(DEF)返回存儲每一個段馬赫數的字典。

估計Mach我使用的公式:

Mach = velocity/sqrt(1.4 * 286 * (Temperature - altitude * 0.05)) 

任何人可以對幫助嗎?

+0

你能澄清輸出應該是什麼喜歡? – Mureinik

+0

你的意思是你需要計算每個索引到3列表馬赫? –

+1

sqrt在某些情況下爲負數!溫度= 0 => sqrt(負)=>錯誤。 –

回答

0

可以zip在字典中的列表值和使用列表理解計算新的密鑰mach_number

import math 

def compute_mach(velocity, altitude, temperature): 
    return velocity/math.sqrt(1.4*286*(temperature-altitude*0.05)) 

mach_dict['mach_number'] = [compute_mach(v, a, t) for v, a, t in zip(mach_dict['velocity'], 
                     mach_dict['altitude'], 
                     mach_dict['temperature'])] 
0

你會壓縮在一起的3所列出生產velocity, altitude, temperature元組:

mach_dict['mach'] = mach_per_section = [] 
for vel, alt, temp in zip(
     mach_dict['velocity'], mach_dict['altitude'], mach_dict['temperature']): 
    mach = vel/sqrt(1.4 * 286 * (temp - alt * 0.05)) 
    mach_per_section.append(mach) 

不幸的是,您的輸入導致ValueError: math domain error,因爲對於某些您會得到負值1.4 * 286 * (temp - alt * 0.05)

+1

你可以使用'cmath.sqrt' - 馬赫數可以是'複雜的',對吧? :-P – mgilson

+0

@mgilson:that'works'that that it produce [[0j,-3.949843661534238j,(4.146473724484641 + 0j),(4.188357297459233 + 0j),(2.51301437847554 + 0j)] ... ... –

+0

我認爲問題實際上是'0'溫度 - 進入絕對0非常棘手,拉下... – mgilson

0

從技術上講,這是修改傳入的字典,並且return是不必要的。

from math import sqrt 

def func(d): 
    machs = [] 
    for v, a, t in zip(d['velocity', d['altitude'], d['temperature']): 
     mach = v/sqrt(1.4 * 286 * (t - a * 0.05)) 
     machs.append(mach) 
    d['mach'] = machs 
    return d 
0

你也可以使用熊貓和numpy的做到這一點,以及

import pandas as pd 
import numpy as np 



def compute(mach_dict): 
    df = pd.DataFrame.from_dict(mach_dict) 
    r = df.velocity/np.sqrt(1.4 * 286 * (df.temperature - df.altitude * 0.05)) 
    return list(r) 

mach_dict={'velocity':[0, 300, 495, 500, 300],'altitude':[288.15, 288.15, 288.15, 288.15, 288.15],'temperature':[0, 0, 50, 50, 50]} 
print(compute(mach_dict)) 

這將處理-ve情況下,它會給你的NaN