Category:American non-fiction books
Category:Books about physicians
Category:Random House books
Category:2011 non-fiction books
Category:Medical manuals
Category:Neurology booksQ:
Swap two lines in a random element in a list?
I have a list of results that looks like this:
p = ['0/1/2011','1/2/2011','1/3/2011','1/4/2011','1/5/2011','2/1/2011']
I want to keep track of how many times a certain date shows up, so I have done this:
count = []
for i in p:
if i[0] == '0':
count.append(1)
else:
count.append(0)
print count
This outputs this:
[1, 1, 1, 1, 1, 0]
Is there a way I can just get the number of each element, instead of keeping track of it? I want to get something like this:
[3, 1, 0, 1, 1, 0]
I tried
for i in p:
if i[0] == '0':
i = i[1:3]
count = count[-1] + count[-2]
else:
count = count[-2]
but this won't work.
Thanks!
A:
When you make a loop and assign a value to a variable inside that loop, you are not modifying the list in any way, you are just overwriting that variable. If you want to modify the original list, you have to do that inside the loop.
for i in p:
if i[0] == '0':
i = i[1:3]
count = count[-1] + count[-2]
else:
count = count[-2]
should become:
for i in p:
if i[ be359ba680
Related links:
Komentar