匹配numpy数字化bin列名称到输入列表

时间:2018-05-24 20:00:20

标签: python python-3.x list

我需要将numpy输出的列名转换回更易读的名称。

挑战:经过长时间整理大量数据后,列不会按顺序排列。我似乎无法与所有列名匹配。

数据:

# input bin list
bins_2_bin = [-6. , -5.4, -4.8, -4.2, -3.6, -3. , -2.4, -1.8, -1.2, -0.6,  0. ,
        0.6,  1.2,  1.8,  2.4,  3. ,  3.6,  4.2,  4.8,  5.4,  6. ]

output_column_names = ['[-0.6000000000000005-0.0]', '[-1.2000000000000002--0.6000000000000005]', '[-1.7999999999999998--1.2000000000000002]', '[-2.4000000000000004--1.7999999999999998]', '[-3.0--2.4000000000000004]', '[-3.6--3.0]', '[-4.2--3.6]', '[-4.8--4.2]', '[-5.4--4.8]', '[-6.0--5.4]', '[0.0-0.5999999999999996]', '[0.5999999999999996-1.1999999999999993]', '[1.1999999999999993-1.7999999999999998]', '[1.7999999999999998-2.4000000000000004]', '[2.4000000000000004-3.0]', '[3.0-3.5999999999999996]', '[3.5999999999999996-4.199999999999999]', '[4.199999999999999-4.799999999999999]', '[4.799999999999999-5.4]', '[5.4-6.0]']

desired_names = ['-6.0:-5.4', '-5.4:-4.8', '-4.8:-4.2', '-4.2:-3.6', '-3.6:-3.0', '-3.0:-2.4', '-2.4:-1.8', '-1.8:-1.2', '-1.2:-0.6', '-0.6:0.0', '0.0:0.6', '0.6:1.2', '1.2:1.8', '1.8:2.4', '2.4:3.0', '3.0:3.6', '3.6:4.2', '4.2:4.8', '4.8:5.4', '5.4:6.0']

# my best attempt at creating a dictionary to rename the columns
names = {}

for bad_name in output_columns_names:
    temp = bad_name[1:6].rstrip('-')

    for good_name in desired_names:

        if temp in good_name[1:5]:
            names[bad_name] = good_name

但这错过了一些比赛。我不确定如何改进匹配。

names = {'[-3.0--2.4000000000000004]': '[-3.0:-2.4]', '[-3.6--3.0]': '[-3.6:-3.0]', '[-4.2--3.6]': '[-4.2:-3.6]', '[-4.8--4.2]': '[-4.8:-4.2]', '[-5.4--4.8]': '[-5.4:-4.8]', '[-6.0--5.4]': '[-6.0:-5.4]'}

# missing keys
['[-0.6000000000000005-0.0]', '[-1.2000000000000002--0.6000000000000005]', '[-1.7999999999999998--1.2000000000000002]', '[-2.4000000000000004--1.7999999999999998]', '[0.0-0.5999999999999996]', '[0.5999999999999996-1.1999999999999993]', '[1.1999999999999993-1.7999999999999998]', '[1.7999999999999998-2.4000000000000004]', '[2.4000000000000004-3.0]', '[3.0-3.5999999999999996]', '[3.5999999999999996-4.199999999999999]', '[4.199999999999999-4.799999999999999]', '[4.799999999999999-5.4]', '[5.4-6.0]']

1 个答案:

答案 0 :(得分:0)

在搜索更多并扩大我的思想之后。我发现正则表达式可以让我非常接近。只需要用冒号(':')替换中间' - '。

A'