Mercurial > hg > nsaunier > traffic-intelligence
comparison python/utils.py @ 733:c35e4a4b199d dev
sortbylength
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 11 Aug 2015 12:06:10 -0400 |
| parents | 8d99a9e16644 |
| children | fb60b54e1041 |
comparison
equal
deleted
inserted
replaced
| 732:ad31520e81b5 | 733:c35e4a4b199d |
|---|---|
| 230 D = {} | 230 D = {} |
| 231 for x, y in zip(X,Y): | 231 for x, y in zip(X,Y): |
| 232 D[x]=y | 232 D[x]=y |
| 233 xsorted = sorted(D.keys()) | 233 xsorted = sorted(D.keys()) |
| 234 return xsorted, [D[x] for x in xsorted] | 234 return xsorted, [D[x] for x in xsorted] |
| 235 | |
| 236 def compareLengthForSort(i, j): | |
| 237 if len(i) < len(j): | |
| 238 return -1 | |
| 239 elif len(i) == len(j): | |
| 240 return 0 | |
| 241 else: | |
| 242 return 1 | |
| 243 | |
| 244 def sortByLength(instances, reverse = False): | |
| 245 '''Returns a new list with the instances sorted by length (method __len__) | |
| 246 reverse is passed to sorted''' | |
| 247 return sorted(instances, cmp = compareLengthForSort, reverse = reverse) | |
| 235 | 248 |
| 236 def ceilDecimals(v, nDecimals): | 249 def ceilDecimals(v, nDecimals): |
| 237 '''Rounds the number at the nth decimal | 250 '''Rounds the number at the nth decimal |
| 238 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' | 251 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' |
| 239 tens = 10**nDecimals | 252 tens = 10**nDecimals |
