Blog Archive

Thursday, June 21, 2012

Finding and Modifying Elements in a List


Finding Things in a List

1.To see check to see if something is in a list:

Heroes=['Thor','Iron Man','Black Widow','Hulk','Iron Man']
#Let's see if Hawkeye is on the list
if'Hawkeye' in Heroes:
   print 'He is on the list'
else:
   print 'No love for archers :('
>>> 
No love for archers :(
 

2.To find index of an element in a list by name:

Heroes=['Thor','Iron Man','Black Widow','Hulk','Iron Man']
#finds index of first occurrence of 'Iron Man'
print Heroes.index('Iron Man')
>>> 
1 
 

Modifying Things in a List

List Comprehensions: [f(x) for x in S if P(x)]

1. How to Map Functions to a Values in a List

#I want to create a list of doubled values
values = [2, 3, 9, 5]

# way 1
dblvals=[num*2 for num in values]
print dblvals
>>> 
[4, 6, 18, 10]

# way 2
dbl=lambda x:x*2
dblvals2=map(dbl,values)
print dblvals2
>>> 
[4, 6, 18, 10]

 

2. Filtering Elements in List

values = [2, 3, 9, 5]
#I want to create a list of values less than 8
less8vals=[num for num in values if num<8]
print less8vals
>>> 
[2, 3, 5]
 

3. Conditionally Replace Elements in List

stuff=[a if a==0 else 2 for a in [0,1,0,3]]
>>> stuff
[0, 2, 0, 2]
 
or we can move the conditional outside the list comprehension-
def check(x):
    if x>3 and x<5:
        return 'True'
    else:
        return 'False'
nums=[1,2,3,4,5,6]
mynums=[num for num in nums if check(num)=='True']
>>> mynums
[4]
 

4. Putting Lists Together and Taking them Apart

#Pulling out Column of Data
#------------------------------------------------
xyvals=[[1,200],[2,300],[3,400]]
xv=[coords[0] for coords in xyvals]
yv=[coords[1] for coords in xyvals]
>>> xv
[1, 2, 3]
>>> yv
[200, 300, 400]
>>> 


#Putting Together Columns of Data
#------------------------------------------------
newcoord=[[xv[i],yv[i]] for i in range(0,len(xv))]
>>> newcoord
[[1, 200], [2, 300], [3, 400]]
 

5. Deleting Items From A List

sets=[[1,'A'],[2,'Q'],[3,'C'],[4,'Q'],[5,'E'],[2,'A']]

#p: pair 
#pc: pair counter

pc=0
for p in sets:
    if p[1]=='Q':
        del sets[pc]
    pc=pc+1
>>> sets
[[1, 'A'], [3, 'C'], [5, 'E'], [2, 'A']]

No comments:

Post a Comment