list mutability

gigs gigs at hi.t-com.hr
Mon Feb 18 19:09:39 CET 2008


hi im having this code

l = [1, 3, 5, 'D', 1, 2, 3, 4, 5, 6, 7, 'A', 'S', 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 'A']

why i need to copy x list? can someone explain me. If i dont copy it i get this 
result:
 >>> took_num_range(l)
[[1, 2, 3, 4, 5, 6, 7], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0], [6, 5, 4, 
3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0]]

but if i copy it i get result as im looking for
 >>> took_num_range(l)
[[1, 2, 3, 4, 5, 6, 7], [9, 8, 7, 6, 5, 4, 3], [8, 7, 6, 5, 4, 3, 2], [7, 6, 5, 
4, 3, 2, 1], [6, 5, 4, 3, 2, 1, 0]]
 >>>

def took_num_range(l):
	j = []
	x = []
	for i in l:
		if type(i) is int and len(x) == 7:
			j.append(x)
			x = x[:]  # im mean here
			x.pop(0)
		if type(i) is int and len(x) < 7:
			x.append(i)
		if type(i) is not int and len(x) == 7:
			j.append(x)
			x = []
		if type(i) is not int and len(x) != 7:
			x = []
	return j


thx



More information about the Python-list mailing list