如何从包含多个键的字典中获取值?

时间:2019-06-02 00:40:07

标签: python dictionary

我有这本字典:

 mydict = {'950001,959070,959071,959072,950908,959073': 'value1', '400856,400857,400858,400859,400860,400861': 'value2', '920100': 'value3', '950107,950109,950108': 'value4'}

我的问题是,如何仅使用value1这样的键之一来获得值950001

换句话说,我想做类似的事情:

mydict['950001']

并打印:

value1

我知道我可以反转键和值,但是在这种情况下,它不适用于我正在处理的脚本。

2 个答案:

答案 0 :(得分:3)

Python中的字典由键值对组成。因此,在您的示例中,一个键为'950001,959070,959071,959072,950908,959073'。在下面的说明中,我将称呼950001959070等“数字”。


方法1:您可以在dict的所有键上编写一个循环,并用str.split在每个逗号,处将每个键分成一个列表。然后,您的列表中包含您的号码,您可以检查所请求的号码n是否在其中:

mydict = {'950001,959070,959071,959072,950908,959073': 'value1', '400856,400857,400858,400859,400860,400861': 'value2', '920100': 'value3', '950107,950109,950108': 'value4'}

n = '950001'
for key in mydict:
    if n in key.split(','):
        print(mydict[key])

哪些印刷品:

value1

方法2:或者,您可以将字典mydict转换为新字典new_dict,以便每个数字都有其自己的密钥:

mydict = {'950001,959070,959071,959072,950908,959073': 'value1', '400856,400857,400858,400859,400860,400861': 'value2', '920100': 'value3', '950107,950109,950108': 'value4'}

new_dict = {}
for key in mydict:
    for new_key in key.split(','):
        new_dict[new_key] = mydict[key]

print(new_dict)

哪些印刷品:

{'950001': 'value1', '959070': 'value1', '959071': 'value1', '959072': 'value1', '950908': 'value1', '959073': 'value1', '400856': 'value2', '400857': 'value2', '400858': 'value2', '400859': 'value2', '400860': 'value2', '400861': 'value2', '920100': 'value3', '950107': 'value4', '950109': 'value4', '950108': 'value4'}

然后,您可以像使用new_dict['950001']所说的那样获取您的价值。


上面的代码着重于清晰度。根据您的用例,如果您愿意,可以编写一个函数和/或使用 dict comprehension 使代码看起来更好:

mydict = {'950001,959070,959071,959072,950908,959073': 'value1', '400856,400857,400858,400859,400860,400861': 'value2', '920100': 'value3', '950107,950109,950108': 'value4'}
new_dict = {nk: v for k, v in mydict.items() for nk in k.split(',')}
print(new_dict['950001'])

答案 1 :(得分:-2)

public class GetUrl {
static Context context;
public GetUrl(Context c) {
    context = c;

}
public String getJSONUrl(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download result..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}