【问题标题】:How to get the last value from the list using python?如何使用python从列表中获取最后一个值?
【发布时间】:2018-03-27 05:59:18
【问题描述】:
data = ['reply': '{"osc":{"version":"1.0"}}']
data1 = ['reply':'{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}']

我只需要使用 python 3.6 从 data 和 data1 中获取“1.0”值。

我怎样才能做到这一点?

【问题讨论】:

  • 我需要从上面的数据中得到“1.0”..我该怎么做
  • data & data1 在 python 中是无效的数据类型,您应该在字典或字符串中发送整个内容。

标签: python json python-3.x list dictionary


【解决方案1】:

修复数据后:

data1 = {'reply':{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}}

只要字典仍然是字典,就继续从字典中获取值:

d = data1
while isinstance(d, dict): 
    d = list(d.values())[0]
print(d)
#1.0

【讨论】:

    【解决方案2】:

    您的 data 和 data1 在 python 中是无效的数据类型,所以我将它们转换为有效的字典。

    from operator import getitem
    data = {'reply': {"osc":{"version":"1.0"}}}
    data1 = {'reply':{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}}
    def get_item(keys, dict_):
        return reduce(getitem, keys, dict_)
    print(get_item(['reply','osc','version'], data))
    print(get_item(['reply','device','network', 'ipv4_dante',"auto"],data1))
    >>>1.0
       1.0
    

    另一种将data和data1作为字符串的方法:

    class GetValue:
    
          def __init__(self, string):
                self.string = string
                self.new_keys  = {}
    
    
         def clean_data(self, data):
            if data[2] == '{':
                get_json_data = len(data) - 3
            else:
                get_json_data = len(data) - 1
            modified_data = [val for val in list(data[data.find(':')+1:get_json_data])
                            if val is not "'" ]
            return json.loads(''.join(modified_data))
    
          def get_recurvise_key(self, data, dict_):
                for key, val in dict_.items():
                    self.new_keys.setdefault(data,[]).append(key)
                    if isinstance(val, dict):
                        self.get_recurvise_key(data, val)
                return self.new_keys.get(data)
    
          def get_value(self):
                get_data = self.clean_data(self.string)
                get_keys = self.get_recurvise_key(self.string,get_data)
                value = reduce(getitem, get_keys, get_data)
                return value
    
    data = """['{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}']"""
    data1 = """['reply':'{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}']"""
    
    obj_data = GetValue(data)
    
    obj_data1 = GetValue(data1)
    
    print(obj_data.get_value(), obj_data1.get_value())
    >>> 1.0 1.0
    

    【讨论】:

    • 我将从我的脚本和这个 ['{"osc":{"version": "1.0"}}'] 我应该得到 "1.0" 值,有时数据会是 ['{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}' ] .在任何情况下,如果数据也在变化,我需要获得“1.0”值。
    【解决方案3】:

    你可以像这样递归地得到这个结果:

    代码:

    def bottom_value(in_data):
        def recurse_to_bottom(a_dict):
            if isinstance(a_dict, dict):
                a_key = list(a_dict)[0]
                return recurse_to_bottom(a_dict[a_key])
            return a_dict
    
        return recurse_to_bottom(json.loads(in_data['reply']))
    

    测试代码:

    import json
    
    data = {'reply': '{"osc":{"version":"1.0"}}'}
    data1 = {'reply':'{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}'}
    
    print(bottom_value(data))
    print(bottom_value(data1))
    

    结果:

    1.0
    1.0
    

    【讨论】:

    • 我将从我的脚本和这个 ['{"osc":{"version": "1.0"}}'] 我应该得到 "1.0" 值,有时数据会是 ['{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}' ] .在任何情况下,如果数据也在变化,我需要获得“1.0”值。
    • @raghavendrat,我做了一个小改动,你可能想再试一次。
    【解决方案4】:

    你可以试试正则表达式:

    import re
    pattern=r'(?<=")[0-9.]+'
    
    data1="""['reply': '{"osc":{"version":"1.0"}}']"""
    data2="""['reply':'{"device":{"network":{"ipv4_dante":{"auto":"1.0"}}}}']"""
    
    def find_value(data):
        return re.findall(pattern,data)[0]
    

    输出:

    print(find_value(data1))
    

    输出:

    1.0
    

    秒:

    print(find_value(data2))
    

    输出

    1.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      相关资源
      最近更新 更多