Skip to content
Advertisement

Tag: list

python pandas flatten a dataframe to a list

I have a df like so: I want to flatten the df so it is one continuous list like so: [‘1/2/2014’, ‘a’, ‘6’, ‘z1’, ‘1/2/2014’, ‘a’, ‘3’, ‘z1′,’1/3/2014’, ‘c’, ‘1’, ‘x3’] I can loop through the rows and extend to a list, but is a much easier way to do it? Answer You can use .flatten() on the DataFrame converted

find and update a value of a dictionary in list of dictionaries

How can I find the dictionary with value user7 then update it’s match_sum eg add 3 to the existing 4. I have this, and am not sure if its the best practice to do it. Answer You can also use next(): prints: Note that if default (second argument) is not specified while calling next(), it would raise StopIteration exception: And

Python: Length of longest common subsequence of lists

Is there a built-in function in python which returns a length of longest common subsequence of two lists? I tried to find longest common subsequence and then get length of it but I think there must be a better solution. Answer You can easily retool a Longest Common Subsequence (LCS) into a Length of the Longest Common Subsequence (LLCS): Demo:

Pop multiple items from the beginning and end of a list

Suppose I have a list of items like this: I want to pop two items from the left (i.e. a and b) and two items from the right (i.e. h,i). I want the most concise an clean way to do this. I could do it this way myself: Any other alternatives? Answer From a performance point of view: mylist =

How to get sorted list inside a dictionary with json.dumps()

I have the following problem: having a python dictionary like the following: I would like to obtain an ordered json object such as: At the moment I use the following: But the two list inside my object are not sorted, and I get: probably because the custom JSONEncoder skips a type that already knows how to manage (list). UPDATE Martijn

Python quicksort – one list – swaps

**I need to make a quicksort algorithm but so that it uses only one list and does swaps inside of it. I managed to make it “sort” or position the first element but now i don’t know how to implement the recursion. The biggest problem I’m having is how to recursively work on a part of the list instead of

What is the difference between `sorted(list)` vs `list.sort()`?

list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list. When is one preferred over the other? Which is more efficient? By how much? Can a list be reverted to the unsorted state after list.sort() has been performed? Please use Why do these list operations (methods) return

Wrapping around on a list when list index is out of range

I’m looking for some code improvement, or a pre-built version of what I’ve implemented myself as I think there might be, or should be, a cleaner way to achieve what I want. I’m writing a piece of software to convert guitar tabs into classical notation, I need to convert the number on a tab to it’s corresponding note and it

Advertisement