Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Hashtable won't save list slices I pass to it

This is basic python script that parses a string and retrieves parameters and their values.

import re

link = "met_y=population&fdim_y=patientStatus:7&fdim_y=pregnant:1&scale_y=lin&ind_y=false&rdim=department&idim=department:9:2:4&idim=clinic:93301:91100:93401:41201:41100&ifdim=department&tstart=1190617200000&tend=1220511600000&ind=false&draft"

print link

filters = ''

matches = re.findall("\&?(?P<name>\w+)=(?P<value>(\w|:)+)\&?",link )
for match in matches:
    name = match[0]
    value = match[1]
    selection = value.split(':')

    filters = {}
    print selection[0]
    print selection[1:len(selection)]
    filters[selection[0]] = selection[1:len(selection)]

print filters

The issue here is that the hashtable filters never gets these values. The output of this script is

{'false': []}

What am I doing wrong?

like image 842
Asimov4 Avatar asked Mar 29 '26 16:03

Asimov4


1 Answers

You're recreating filters inside the loop:

filters = {}

This line needs to be placed before the loop, not inside.

Another potential issue is that your input contains duplicate keys (fdim_y and idim). As things stand, your code only keep the last value of each key.

like image 171
NPE Avatar answered Apr 02 '26 14:04

NPE