Python词典:序列化字符串

时间:2016-08-22 20:08:58

标签: python-2.7

enter image description here

我已经定义了一个名为"值"的python字典。如上所示,它有几行。如果名为" Risk:的键为高,我试图逐个访问每行的键:值对。我试过像:

for i in range(len(rows)-1):
   a = []
   if values["Risk"][i]=='high':
     a.append(values[key][i])

但问题是它只是追加每个值的第一个字符。因此,而不是获得男性'男性'针对关键性别'我得到了M'。我是python的新手,并不是真正理解这里的问题。 有什么建议? 谢谢!

2 个答案:

答案 0 :(得分:0)

花了一段时间后,你应该考虑如何从你正在使用的来源获取数据,因为它们不足以计算它们。我希望能帮到你,但我想如果我知道你的来源是dictrows,我会解决这个问题。如果您想解决此问题并且它对您非常有价值,那么只需在您的问题中添加更多数据。这就是我能写的全部。

答案 1 :(得分:0)

我知道这不是您问题的直接答案,但我认为您遇到困难是因为您存储" loaner"信息。如果你切换到基于类的模型,一切都会变得更容易,更直观,硬编码也更少:

 using Newtonsoft.Json;
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.STSClasses
{
    /// <summary>
    /// This file holds all user defined indicator methods.
    /// </summary>
   public class STSmyTrade
    {
        [JsonProperty("direction")]
        public string direction { get; set; }

        [JsonProperty("entryprice")]
        public double entry { get; set; }

        [JsonProperty("exitprice")]
        public double exit { get; set; }

        [JsonProperty("potentialtarget")]
        public double potentialtarget { get; set; }

        [JsonProperty("entrytime")]
        public DateTime entrytime { get; set; }

        [JsonProperty("exittime")]
        public DateTime exittime { get; set; }

        [JsonProperty("maxfavourable")]
        public double mfe { get; set; }

        [JsonProperty("maxagainst")]
        public double mae { get; set; }


        [JsonProperty("maxagainst1ATR")]
        public double mae1atr { get; set; }

        [JsonProperty("Time1ATR")]
        public DateTime time1atr { get; set; }

        [JsonProperty("maxagainst2ATR")]
        public double mae2atr { get; set; }
        [JsonProperty("Time2ATR")]
       public DateTime time2atr { get; set; }

        [JsonProperty("gains")]
        public double gains { get; set; }

        [JsonProperty("signal")]
        public string signal { get; set; }

        [JsonProperty("instrument")]
        public string instrument { get; set; }

        [JsonProperty("account")]
        public string account { get; set; }
         [JsonProperty("quantity")]
        public int quantity { get; set; }
         [JsonProperty("hitedge")]
        public bool hitedge { get; set; }
         [JsonProperty("RealizedProfitLoss")]
        public double RealizedProfitLoss { get; set; }
        [JsonProperty("CashValue")]
        public double CashValue { get; set; }
        [JsonProperty("BuyingPower")]
        public double BuyingPower { get; set; }
    }
}

    {
      "direction": "Long",
      "entryprice": 1.13211,
      "exitprice": 1.13197,
      "potentialtarget": 0.00025,
      "entrytime": "2016-08-22T13:46:31.7459655-04:00",
      "exittime": "2016-08-22T15:46:22.3955682-04:00",
      "maxfavourable": 0.00055,
      "maxagainst": -0.00035,
      "maxagainst1ATR": -0.00035,
      "Time1ATR": "0001-01-01T00:00:00",
      "maxagainst2ATR": -0.00035,
      "Time2ATR": "0001-01-01T00:00:00",
      "gains": -0.00015,
      "signal": "EnterLongBounce",
      "instrument": "$EURUSD",
      "account": "InteractiveBrokersindicatorbased",
      "quantity": 1,
      "hitedge": false,
      "RealizedProfitLoss": 0.0,
      "CashValue": 0.0,
      "BuyingPower": 0.0
    }');

输出:

class LoanInfo(object):
    __slots__ = ["HairLength","Employed","Age","Student",
                 "PreviouslyDeclined","Risk","FirstLoan","Gender",
                 "TypeOfColateral","LifeInsurance"]
    def __init__(self,HairLength,Employed,Age,Student,PreviouslyDeclined,Risk,FirstLoan,Gender,TypeOfColateral,LifeInsurance):
        self.HairLength = HairLength
        self.Employed = Employed
        self.Age = Age
        self.Student = Student
        self.PreviouslyDeclined = PreviouslyDeclined
        self.Risk = Risk
        self.FirstLoan = FirstLoan
        self.Gender = Gender
        self.TypeOfColateral = TypeOfColateral
        self.LifeInsurance = LifeInsurance


loan_info_1 = LoanInfo('Short', 'Yes', 'Young', 'No', 'No', 'high', 'No', 'Male', 'Car', 'Yes')
loan_info_2 = LoanInfo('Short', 'Yes', 'Young', 'No', 'No', 'high', 'Yes', 'Male', 'Car', 'No')

loaners = [loan_info_1, loan_info_2]
rows = loan_info_1.__slots__ #<-- the slots of the object are the rows you want

high_risk_genders = []
for loaner in loaners:
   if loaner.Risk == 'high':
     high_risk_genders.append(loaner.Gender)

print high_risk_genders

这是处理问题的更加pythonic方式。 python类有很多帮助,比如http://www.tutorialspoint.com/python/python_classes_objects.htm