Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中解析大列表的提示_Python - Fatal编程技术网

在python中解析大列表的提示

在python中解析大列表的提示,python,Python,我有一个需要解释的数据集,以便重建要分析的信号。为此,我使用了大量if、elif语句。我想知道是否有更优雅的方式来做这件事 这是我的代码: for line in data: if line[:2] == '10': capstart = 1 if capstart == 1: if line[:2] == 'e3': reconstruct(int(line[2:6],16), int(line[6:10],16)) elif line[:2] == '

我有一个需要解释的数据集,以便重建要分析的信号。为此,我使用了大量if、elif语句。我想知道是否有更优雅的方式来做这件事

这是我的代码:

for line in data:
if line[:2] == '10':
    capstart = 1
if capstart == 1:
    if line[:2] == 'e3':
        reconstruct(int(line[2:6],16), int(line[6:10],16))
    elif line[:2] == '02':
        if line[8:] == '20':
            REDoffset = int(line[2:4],16)
            REDledlvl = int(line[4:6],16)
            REDgain = int(line[6:8],16)
        if line[8:] == '10':
            IRoffset = int(line[2:4],16)
            IRledlvl = int(line[4:6],16)
            IRgain = int(line[6:8],16)
    elif line[:2] == '10':
        if line[2:4] == '00':
            pulse_length_lo = int(line[4:],16)
        if line[2:4] == '01':
            pulse_length_hi = int(line[4:],16)
        if line[2:4] == '02':
            pulse_ir_on = int(line[4:],16)
        if line[2:4] == '03':
            pulse_red_on = int(line[4:],16)
        if line[2:4] == '04':
            pulse_led_switch_time = int(line[4:],16)
        if line[2:4] == '05':
            dark_worn_threshold = int(line[4:],16)
        if line[2:4] == '06':
            imu_accel_range = int(line[4:],16)
        if line[2:4] == '07':
            imu_gyro_range = int(line[4:],16)
        if line[2:4] == '08':
            ls_duration = int(line[4:],16)
        if line[2:4] == '09':
            imu_interval = int(line[4:],16)
        if line[2:4] == '0a':
            timestamp_lo = int(line[4:],16)
        if line[2:4] == '0b':
            timestamp_hi = int(line[4:],16)
        if line[2:4] == '0c':
            ADC_MAX = int(line[4:],16)
        if line[2:4] == '0d':
            ADC_offset = int(line[4:],16)
        if line[2:4] == '0e':
            lightsensor_has_red = int(line[4:],16)
        if line[2:4] == '0f':
            IR_worn_current = int(line[4:],16)
        if line[2:4] == '10':
            IR_worn_offset = int(line[4:],16)
        if line[2:4] == '11':
            IR_worn_threshold = int(line[4:],16)
        if line[2:4] == '12':
            accel_num_bits = int(line[4:],16)
        if line[2:4] == '13':
            gyro_num_bits = int(line[4:],16)
        if line[2:4] == 'ff':
            sensor_end_status = line[4:]
    else:
        other.append(line)
请注意,“数据”列表包含大量十六进制数据包,其中第一个字节(字符串中的前两个字符)表示数据包类型,在每个数据包类型中,我需要分离字符串并隔离其中包含的数据


我想你可以指给我看,那太好了!代码可以工作,但我希望它更优雅。

我在这里故意掩盖细节。只看依赖于
行[2:4]
的变量:

从名为
line\u 2\u 4\u lookup
的字典开始,该字典将输入数字映射到变量名称:

line_2_4_lookup = {
             '00':           "pulse_length_lo",
             '01':           "pulse_length_hi",
             '02':           "pulse_ir_on",
             '03':           "pulse_red_on",
             '04':           "pulse_led_switch_time",
             '05':           "dark_worn_threshold",
             '06':           "imu_accel_range",
             '07':           "imu_gyro_range",
             '08':           "ls_duration",
             '09':           "imu_interval",
             '0a':           "timestamp_lo",
             '0b':           "timestamp_hi",
             '0c':           "ADC_MAX",
             '0d':           "ADC_offset",
             '0e':           "lightsensor_has_red",
             '0f':           "IR_worn_current",
             '10':           "IR_worn_offset",
             '11':           "IR_worn_threshold",
             '12':           "accel_num_bits",
             '13':           "gyro_num_bits",
             'ff':           "sensor_end_status"}
然后,将输入的数字存储在一个名为
input\u values
的dict中,而不是像
gyro\u num\u bits
这样的变量,因此程序将需要参考
input\u values[“gyro\u num\u bits”]

那么解码只是一行:

input_values[line_2_4_lookup[line[2:4]]] = int(line[4:],16)

我们不必为所有这些参数使用大量单独的命名变量,而可以将它们存储在字典中,我将其命名为
params
。我将参数名存储在另一个名为field_names的dict中,并将相关的十六进制代码作为键

field_names = {
    '00': 'pulse_length_lo',
    '01': 'pulse_length_hi',
    '02': 'pulse_ir_on',
    '03': 'pulse_red_on',
    '04': 'pulse_led_switch_time',
    '05': 'dark_worn_threshold',
    '06': 'imu_accel_range',
    '07': 'imu_gyro_range',
    '08': 'ls_duration',
    '09': 'imu_interval',
    '0a': 'timestamp_lo',
    '0b': 'timestamp_hi',
    '0c': 'ADC_MAX',
    '0d': 'ADC_offset',
    '0e': 'lightsensor_has_red',
    '0f': 'IR_worn_current',
    '10': 'IR_worn_offset',
    '11': 'IR_worn_threshold',
    '12': 'accel_num_bits',
    '13': 'gyro_num_bits',
    'ff': 'sensor_end_status',
}

params = {}
capstart = False
for line in data:
    if line[:2] == '10':
        capstart = True
    if capstart:
        if line[:2] == 'e3':
            reconstruct(int(line[2:6], 16), int(line[6:10], 16))
        elif line[:2] == '02':
            if line[8:] == '20':
                params['REDoffset'] = int(line[2:4], 16)
                params['REDledlvl'] = int(line[4:6], 16)
                params['REDgain'] = int(line[6:8], 16)
            elif line[8:] == '10':
                params['IRoffset'] = int(line[2:4], 16)
                params['IRledlvl'] = int(line[4:6], 16)
                params['IRgain'] = int(line[6:8], 16)
        elif line[:2] == '10':
            code = line[2:4] 
            field = field_names[code]
            params[field] = line[4:] if field == 'sensor_end_status' else int(line[4:], 16) 
        else:
            other.append(line)

您尚未向我们提供任何示例数据,因此此代码未经测试,但这应该可以让您开始使用。

这看起来像是一个示例。您的问题还将受益于示例输入和输出,以便人们了解您的数据结构。请注意,发布到code Review的代码必须是一个独立的工作程序,包含相关的示例数据。是的,可以做很多工作来改进代码,但是要正确地执行它,需要对程序的其余部分进行一些修改。但对于初学者来说,您可以存储
行[2:4]
,而不是重新计算20次。您可能还想查看这些线程:我没有提供示例数据,因为我需要一些代码细化,奥尔尼,我的代码可以工作,但很混乱!谢谢你的帮助!伟大的那我就试试这个。。。测试并不是那么重要,因为我只想指出正确的方向来改进代码,字典看起来就是这样。。。我来自C,所以我的想法有点不同@Danf嗯,我喜欢测试我发布的代码,以防我犯了一个愚蠢的错误我想我本可以创造我自己的假数据,但这需要时间。FWIW,在Python中,使用dict在C中使用开关是很常见的。顺便说一句,来自C的这篇文章对您很有帮助:,这篇文章是由经验丰富的Ned Batchelder编写的。它很好地解释了Python的数据模型,它与C使用的传统数据模型有一些重要区别。