Skip to main content
bug
Source Link

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys:

def times_so_far(ls, maxValue):
   temp = [0]*maxValue[0]*(maxValue+1)
   out = [0]*len(ls)
   for i in xrange(len(ls)):
      out[i] = temp[ls[i]]
      temp[ls[i]] += 1
   return out

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys:

def times_so_far(ls, maxValue):
   temp = [0]*maxValue
   out = [0]*len(ls)
   for i in xrange(len(ls)):
      out[i] = temp[ls[i]]
      temp[ls[i]] += 1
   return out

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys:

def times_so_far(ls, maxValue):
   temp = [0]*(maxValue+1)
   out = [0]*len(ls)
   for i in xrange(len(ls)):
      out[i] = temp[ls[i]]
      temp[ls[i]] += 1
   return out
Added example of a specialized implementation.
Source Link

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys:

def times_so_far(ls, maxValue):
   temp = [0]*maxValue
   out = [0]*len(ls)
   for i in xrange(len(ls)):
      out[i] = temp[ls[i]]
      temp[ls[i]] += 1
   return out

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys:

def times_so_far(ls, maxValue):
   temp = [0]*maxValue
   out = [0]*len(ls)
   for i in xrange(len(ls)):
      out[i] = temp[ls[i]]
      temp[ls[i]] += 1
   return out
+ Added version of the code which uses list comprehension.
Source Link

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

Maybe using list.count() you can achieve the same with less lines?

x = [1,1,2,3,1,3]

def times_so_far(ls):

    out = [0]*len(ls)
    for i in xrange(len(ls)):
        out[i] = ls[:i].count(ls[i]) 
    return out

This can be written with list comprehension as mentioned by Caridorc, and it removes the first len(ls) call this way (though maybe at the cost of resizing out for every element of the list):

def times_so_far(ls):

    return [ls[:i].count(ls[i]) for i in xrange(len(ls))]
Changed range to xrange
Source Link
Loading
Source Link
Loading