3 msedict - multiple value dictionary, keeps multiple values per key
4 You can slice, add, sort with dictionaries. The functional operations map, filter, reduce can also be used .
5 The dictionary can also be used like a stack with push and pop. It can be used to Split , reverse and swap keys and values.
8 from seqvaluedict
import seqdict
9 from UserList
import UserList
12 from UserList
import UserList
14 UserList.__init__(self,liste)
19 index = self.parent.values().
index([])
20 del self.
parent[index:index+1]
28 elif type(List)==type({}):
29 for key,value
in List.items():
30 self.__setitem__(key,value)
31 elif List
and not Dict:
32 for key,value
in List:
33 if isinstance(value,MyUserList):
35 self.__setitem__(key,v)
37 self.__setitem__(key,value)
38 elif type(List)==type(Dict)==type([]):
39 for key,value
in map(
None,List,Dict):
40 self.__setitem__(key,value)
42 if isinstance(Dict.values()[0],MyUserList):
48 if type(value)==type([]):
50 self.__setitem__(key,v)
52 self.__setitem__(key,value)
54 if not self.dict.has_key(key):
56 if isinstance(value,MyUserList):
57 self.dict[key] = value
61 values = self.dict[key]
62 if isinstance(value,MyUserList):
67 if not value
in values:
74 self.__setitem__(key,value)
76 start =
max(start,0); stop =
max(stop,0)
78 for key
in newdict.keys():
79 if self.dict.has_key(key):
80 index = self.list.index(key)
81 delindexes.append(index)
90 self.dict[key]=UserList(self)
93 for index
in delindexes:
94 key = self.list[index]
97 self.list[start:stop] = newdict.list[:]
98 self.update(newdict.dict)
100 values =
map(
lambda x:x[:],self.values())
101 return self.__class__(self.list,values)
103 vallist = self.dict.values()
104 return map(
lambda x,y=value:x.count(y),vallist).
count(1)
105 def filter(self,function,filtervalues=0):
106 if filtervalues == 1:
107 dict = self.__class__()
108 for key,values
in self.items():
112 elif filtervalues == 2:
113 dict = self.__class__()
114 for key,values
in self.items():
120 liste=
filter(function,self.list)
124 return self.__class__(liste,dict)
125 def map(self,function,mapvalues=2):
127 dict = self.__class__()
128 for key,values
in self.items():
133 dict = self.__class__()
134 for key,values
in self.items():
139 def pop(self,key='...None',value='...None'):
145 pos = self.list.index(key)
148 return {self.list.pop(pos):tmp}
151 index = val.index(value)
159 index = self[key].
index(value)
161 def sort(self,func1=None,func2=None):
165 apply(self.list.sort,[func1])
167 for value
in self.values():
168 apply(value.sort,[func2])
171 tmp = self.__class__()
172 for key,values
in self.items():
175 self.list,self.dict = tmp.list,tmp.dict
178 def __repr__(self):
return 'mseqdict(\n%s,\n%s)'%(self.list,self.dict)
const T & max(const T &a, const T &b)