lists, uppercase?

Philip Swartzleonard starx at pacbell.net
Sun Feb 10 15:16:09 EST 2002


maximilianscherr || Wed 30 Jan 2002 01:49:28p:

> just two questions this time:
> 
> how can i change a list like this
> [1, 2, 3, 4]
> to
> [4, 3, 2, 1]
> 
> how can i get to know if a string is totally uppercase?

Well, you can't really have upper or lower case with just numbers, but i 
think you want a list that's sorted highest-to-lowest. To do this, just run 
both sort and reverse on the list:

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> x = [4,1,3,2]
>>> x.sort()
>>> x
[1, 2, 3, 4]
>>> x.reverse()
>>> x
[4, 3, 2, 1]
 
After these two steps, you can be sure that the list is in decending order, 
if you want to check that without re-sorting it, you could use a function 
like this (basically 'if any item is less than (i.e. not >=) the next item, 
return false, else return true'):

>>> def is_descending( in_list ):
	for i in range(len(in_list) - 1):
		if in_list[i] < in_list[i+1]:
			return 0
	return 1

>>> is_descending(x)
1
>>> is_descending([1,2,3,4])
0
>>> is_descending([4,3,1,2])
0


-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list