Skip to content
Advertisement

Tag: list

Python save arbitrarily nested list to CSV

I have a list that is composed of strings, integers, and floats, and nested lists of strings, integer, and floats. Here is an example I want each item of the list written to a line in a CSV file. So, given the above data, the file would look like this: There are lots of resources about how to write a

Nested List of Lists to Single List of tuples

I’m trying to turn a nested list of lists (number of lists can be 2 lists +) into a single list of tuples. The list looks something like this: And I would like for it to be like this: I know that if you do zip(list1, list2), it becomes a list of tuple. But how do I go about doing

Change multiple items in a list at one time in Python

Can I change multiple items in a list at one time in Python? Question1: For example,my list is I want to the third and fifth item become 99.I know I can do it by However, is there any easier way to do this? Question2:in the situation,my target value is[99,98], my index is [2,4],so my result would be [0,0,99,0,98]. Is there

Accessing elements of multi-dimensional list, given a list of indexes

I have a multidimensional list F, holding elements of some type. So, if for example the rank is 4, then the elements of F can be accessed by something like F[a][b][c][d]. Given a list L=[a,b,c,d], I would like to access F[a][b][c][d]. My problem is that my rank is going to be changing, so I cannot just have F[L[0]][L[1]][L[2]][L[3]]. Ideally, I

Sum slices of consecutive values in a NumPy array

Let’s say I have a numpy array a containing 10 values. Just an example situation here, although I would like to repeat the same for an array with length 100. I would like to sum the first 5 values followed by the second 5 values and so on and store them in a new empty list say b. So b

Max and Min value for each colum of one Dataframe

Give this dataframe ‘x’: How I could get a list of pairs with the min and max of each column? The result would be: Answer You could define a function and call apply passing the function name, this will create a df with min and max as the index names: If you insist on a list of lists we can

How to save a list to a file and read it as a list type?

Say I have the list score = [1,2,3,4,5] and it gets changed while my program is running. How could I save it to a file so that next time the program is run I can access the changed list as a list type? I have tried: But this results in the elements in the list being strings not integers. Answer

Python: reduce (list of strings) -> string

I expected something like ‘a.b.c.d’ or ‘abcd’. Somehow, can’t explain the results. There are similar questions here, but not quite like this one. Answer The result of executing the function passed as the first parameter, will be the first parameter to that function in the next iteration. So, your code works like this When x, y are ‘alfa’ and ‘bravo’

Python pandas apply function if a column value is not NULL

I have a dataframe (in Python 2.7, pandas 0.15.0): I want to apply a simple function for rows that does not contain NULL values in a specific column. My function is as simple as possible: And my apply code is the following: It works perfectly. If I want to check column ‘B’ for NULL values the pd.notnull() works perfectly as

Advertisement