Pythonic way to get last index value of enumerate - python

When I enumerate through a list, is there an intended, pythonic way of obtaining the last index value provided?
Something that would get the equivalent of this:
highest = None
for index, value in enumerate(long_list):
# do stuff with index and value
highest = index
return highest
This approach I don't like. It has hundreds of unnecessary variable assignments. Also, it's ugly.
Background: I have an ordered list build with a RDBS and SQLAlchemy, using numbers as indexes in the relation table. Also I store the highest used index number in the list table, for easy appending of new entries (without extra max lookup on relation table). For when things get messed up, for whatever reason, I included a reorg function, that rebuilds indexes starting from 0 (to remove any gaps). I to that by for-enumerate-iterating over the association table. After that I need to count them or max the index, to get my new highest index value for the list table. That kinda bugs me.
Suggestions? Preferably something that works in 2.7.

To get the last index of a list
len(mylist)-1
To get the last element of a list you can simply use a negative index.
mylist[-1]

Related

python logic setting variable to none

Ive done enough research to understand my logic (I believe):
I have python code to set a variable to None so that at the db level it stores the value of this variable to Null.
Logic looks like:
# when properly set to something other than `None` an example value might be: ['6.1 ', 'Medium'
thesev=cve.find("span", class_="label-warning").text
thesevcat=re.split("- ", str(thesev))
if thesevcat is None:
thesevcat=None
else:
#looking to set thesevcat='Medium' for example
thesevcat=thesevcat[1]
sometimes thesevcat is successfully parsed the value, othertimes it cant parse it, so I want to set it to None
However, I keep getting this error:
thesevcat=thesevcat[1]
IndexError: list index out of range
what is going wrong here? Thanks
thesevcat=thesevcat[1]
IndexError: list index out of range
List index out of range is pretty explicit, it means that if thesevcat is in fact a list (which we don't really know seeing your code but I guess it is), it doesn't have a 2nd index, which means it's a list containing in maximum one element (for instance ["medium"] )
Please keep in mind that the first index of a list is 0, so if your list is your_list = ["medium"], to access "medium", you need to write your_list[0]
if you wanted to access the 2nd element of your list, then your list would need to be at least 2 element long, then you'd access it with the index 1
for instance:
your_list = ["medium", "large"]
print(your_list[1])
# would print large

How to find most repeated element in a list Python?

I'm currently using Counter() method for this. But the issue I'm facing is that when there are multiple elements with same number of values I'm getting the out of key value of number which occurs first in the list.
a=[1,3,2,2,3]
coun=Counter(a)
print(coun.most_common(1))
Output: [(3,2)]
a=[1,2,3,2,3]
coun=Counter(a)
print(coun.most_common(1))
Output: [(2,2)]
I want to get the key value which is lower instead of the one that occurs first i.e 2 here irrespective of the order. I could sort the list but I'm considering that sorting can use up a lot of time.
Please help
Sorry for the formatting mess.
Depending on the amount of duplicates you are expecting you could simply check more of the most_common values? Assuming that there's no more than 100 values with exactly the same amount you could simply do:
print(sorted(coun.most_common(100))[0])
You could use a different values for 100 of course. But now the list to sort would be at most 100 tuples, which of course isn't a problem.

Optimizing min function for changing lists

I am dealing with a problem where I need to keep track of the minimum number in a list. However, this list is constantly diminishing, say like from a million elements to a single element. I was looking for a way to avoid checking the minimum value everytime I got a one element smaller list. Like keeping track of the minimum element and if it is removed the next minimum becomes the minimum. I want to accomplish this in linear time.(It should be achievable given the mechanics of the problem)
What I thought of since I started that, I can use collections Counter to count elements in the list. Then I find the minimum(already O(2*n)), and everytime I remove an element, I subtract 1 from the value of the dictionary key. However when the minimum number's count is depleted, I would still require to find the second minimum element so it could replace it.
Please help me find a solution to this. I would say this is an interesting problem.
Let's say your program would take some time to sort that list
a = [10,9,10,8,7,6,5,4,3,2,1,1,1,0] # you're just removing
a = sorted(a) #sort ascending
# then you remove stuff from your list
# but always a[0] is minimum element
min = a[0] #you must be careful, there must be at least one item so check that before
#getting the min
So there is no need for searching it every time

How to use the index of a min value on one list to find the position of a string on another list

I am currently trying write a function to use the position of a min value on one list to reference a string on another list.
I have two list, one with states names and another with floats. I am using the min method to get the minimum value of the float list. The problem is, how I use an index to mark the position of that value then use it to return the state that holds the same position on the other list?
This is the code I am currently using, but it does not go all the way through the list before it returns a value, which is way too soon in the list.
def stateheart_min():
for item in heartdis_flt:
heartcount=0
heartcount+=1
min_index=0
if item == min(heartdis_flt):
min_index=heartcount
return states_fin[min_index:min_index+1]
This is a bit terse to read, but here is an alternative way to do it. You can use min to find the minimum value in values. Then you can use index to find the index at which the minimum occurs in the list. You can then use that returned index to index the correct element from states.
states = ['NY', 'PA', 'CA', 'MI']
values = [15.0, 17.5, 3.5, 25.4]
>>> states[values.index(min(values))]
'CA'
Try this:
index = min(zip(values, range(len(values))))[1]
This first builds a list of pairs, each having a value as first item and its index as second item. So when you find the minimum, the first item still has the main impact (the index will only be taken into account if the values are equal). Taking the index is then done using [1] in the end.
Not entirely sure what you are asking, but a dictionary might be a better choice. Also setting heartcount=0 inside the loop resets the variable back to 0 each iteration. Check your variables inside the loop.

Python Spark split list into sublists divided by the sum of value inside elements

I try to split a list of objects in python into sublists based on the cumulative value of one of the parameters in the object. Let me present it on the example:
I have a list of objects like this:
[{x:1, y:2}, {x:3, y:2}, ..., {x:5, y: 1}]
and I want to divide this list into sub-lists where the total sum of x values inside a sublist will be the same (or roughly the same) so the result could look like this:
[[{x:3, y:1}, {x:3, y:1}, {x:4, y:1}], [{x:2, y:1}, {x:2, y:1}, {x:6, y:1}]]
Where the sum of x'es is equal to 10. Objects I am working with are a little bit more complicated, and my x'es are float values. So I want to aggregate the values from the ordered list, up till the sum of x'es will be >= 10, and then start creating next sub-list.
In my case, the first list of elements is an ordered list, and the summation has to take place on the ordered list.
I done something like this already in C#, where I iterate through all my elements, and keep one counter of "x" value. I sum the value of x for consecutive objects, until it will hit my threshold, and then I create a new sub-list, and restart my counter.
Now I want to reimplement it in python, and next use it with Spark. So I am looking for a little bit more "functional" implementation, maybe something to work nicely with map-reduce framework. I can't figure out another way than the iterative approach.
If you have any suggestions, or possible solutions, I would welcome all constructive comments.

Categories

Resources