`Pip Install Requests
`im Voraus.>>> from requests.structures import CaseInsensitiveDict
>>> cid = CaseInsensitiveDict({'Apple': 'osx', 'NeXT': 'STEP'})
>>> cid
{'Apple': 'osx', 'NeXT': 'STEP'}
>>> cid['apple']
'osx'
>>> cid['APPLE']
'osx'
>>> cid['next']
'STEP'
>>> cid['nEXT']
'STEP'
>>> cid['microsoft']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "〜〜/python3.5/site-packages/requests/structures.py", line 54, in __getitem__
return self._store[key.lower()][1]
KeyError: 'microsoft'
--Ich war sauer ...
has_key
>>> cid.has_key('microsoft')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'CaseInsensitiveDict' object has no attribute 'has_key'
――Wenn es drin ist, ist es okay.
in
>>> 'microsoft' in cid
False
>>> 'apple' in cid
True
>>> 'Apple' in cid
True
--Verwenden Sie ein Modul namens Hash :: Case :: Preserve. ――Es scheint, dass das Verhalten, wenn kein Schlüssel vorhanden ist, dasselbe ist wie das einfache Hash.
cid.pl
use Hash::Case::Preserve;
tie my(%cid),'Hash::Case::Preserve';
$cid{'NeXT'} = 'STEP';
$cid{'Apple'} = 'osx';
print $cid{'next'};
print $cid{'APPLE'};
print $cid{'microsoft'};
print keys %cid
sh-3.2$ perl -l cid.pl
STEP
osx
AppleNeXT
Recommended Posts