Python哈希表不会保存我传递给我的列表片段

2024-10-01 02:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这是解析字符串并检索参数及其值的基本python脚本。你知道吗

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

这里的问题是哈希表过滤器永远不会得到这些值。此脚本的输出为

{'false': []}

我做错什么了?你知道吗


Tags: namere脚本falsevaluematchlinkfilters
1条回答
网友
1楼 · 发布于 2024-10-01 02:33:25

您正在循环内重新创建filters

filters = {}

这条线需要放在环路的前面,而不是里面。你知道吗

另一个潜在的问题是,您的输入包含重复的键(fdim_yidim)。从目前的情况来看,代码只保留每个键的最后一个值。你知道吗

相关问题 更多 >