Element-wise appending of 2D lists? [duplicate] - python

This question already has answers here:
Python: merge nested lists
(4 answers)
Closed 9 months ago.
I want to concatenate 2D lists to the end of a list_log, as follows:
list_log = []
list1 = [[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]]
list2 = [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]
list_log.append(list1)
list_log.append(list2)
Desired result:
list_log = [[0.0, 1.0], [1.7, 3.6], [8.4, 13.5], [20.1, 31.5], [29.3, 50.3], [41.8, 64.4], [74.1, 93.3], [61.9, 113.8]]
Actual result: list_log = [[[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]], [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]]
I've also tried getting this result using list comprehension, as follows:
list_log2 = [[[i, j] for i in list1[c] for j in list2[c]] for c in range(8)]
But this gives the following result: list_log2 = [[[0.0, 1.0]], [[1.7, 3.6]], [[8.4, 13.5]], [[20.1, 31.5]], [[29.3, 50.3]], [[41.8, 64.4]], [[74.1, 93.3]], [[61.9, 113.8]]], so with too many brackets.
Also, the example above uses only two lists, but in reality I have thousands of these lists coming in one after the other, and which I need to append to the end of the list_log one-by-one. Because of this I'm reluctant to use list comprehension as shown above, because this basically re-generates the entire log_list2 each time I append a new list, which isn't very efficient. That's why I'm trying to make this happen with .append() instead, as adding one element to the end of a list is computationally much less intensive than re-creating the entire log each time.
So ideally I'd like to make this work with .append() (or similar stuff like .extend()), but I'm open to all suggestions. Thanks in advance!

You can do it using zip() and a list comprehension:
[[*i, *j] for i, j in zip(list1, list2)]
Output:
[[0.0, 1.0],
[1.7, 3.6],
[8.4, 13.5],
[20.1, 31.5],
[29.3, 50.3],
[41.8, 64.4],
[74.1, 93.3],
[61.9, 113.8]]

list1 = [[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]]
list2 = [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]
print([i+j for i,j in zip(list1,list2)])
>>>[[0.0, 1.0], [1.7, 3.6], [8.4, 13.5], [20.1, 31.5], [29.3, 50.3], [41.8, 64.4], [74.1, 93.3], [61.9, 113.8]]

Related

Extracting items from array using variable sliding window in Python [duplicate]

This question already has answers here:
Rolling or sliding window iterator?
(29 answers)
Closed 6 days ago.
I have an array of digits: array = [1.0, 1.0, 2.0, 4.0, 1.0]
I would like to create a function that extracts sequences of digits from the input array and appends to one of two lists depending on defined conditions being met
The first condition f specifies the number of places to look ahead from index i and check if a valid index exists. If true, append array[i] to list1. If false, append to list2.
I have implemented it as follows:
def somefunc(array, f):
list1, list2 = [], []
for i in range(len(array)):
if i + f < len(array):
list1.append(array[i])
else:
list2.append(array[i])
return list1, list2
This functions correctly as follows:
somefunc(array,f=1) returns ([1.0, 1.0, 2.0, 4.0], [1.0])
somefunc(array,f=2) returns ([1.0, 1.0, 2.0], [4.0, 1.0])
somefunc(array,f=3) returns ([1.0, 1.0], [2.0, 4.0, 1.0])
However, I would like to add a second condition to this function, b, that specifies the window length for previous digits to be summed and then appended to the lists according to the f condition above.
The logic is this:
iterate through array and at each index i check if i+f is a valid index.
If true, append the sum of the previous b digits to list1
If false, append the sum of the previous b digits to list2
If the length of window b isn't possible (i.e. b=2 when i=0) continue to next index.
With both f and b conditions implemented. I would expect:
somefunc(array,f=1, b=1) returns ([1.0, 1.0, 2.0, 4.0], [1.0])
somefunc(array,f=1, b=2) returns ([2.0, 3.0, 6.0], [5.0])
somefunc(array,f=2, b=2) returns ([2.0, 3.0], [6.0, 5.0])
My first challenge is implementing the b condition. I cannot seem to figure out how. see edit below
I also wonder if there is a more efficient approach than the iterative method I have begun?
Given only the f condition, I know that the following functions correctly and would bypass the need for iteration:
def somefunc(array, f):
return array[:-f], array[-f:]
However, I again don't know how to implement the b condition in this approach.
Edit
I have managed an iterative solution which implements the f and b conditions:
def somefunc(array, f, b):
list1, list2 = [], []
for i in range(len(array)):
if i >= (b-1):
if i + f < len(array):
list1.append(sum(array[i+1-b: i+1]))
else:
list2.append(sum(array[i+1-b: i+1]))
return list1, list2
However, the indexing syntax feels horrible and I so I am certain there must be a more elegant solution. Also, anything with improved runtime would really be preferable.
I can see two minor improvements you could implement in your code:
def somefunc(array, f, b):
list1, list2 = [], []
size = len(array) # Will only measure the length of the array once
for i in range(b-1, size): # By starting from b-1 you can remove an if statement
if i + f < size: # We use the size here
list1.append(sum(array[i+1-b: i+1]))
else:
list2.append(sum(array[i+1-b: i+1]))
return list1, list2
Edit:
An ever better solution would be to add the new digit and substract the last at each iteration. This way you don't need to redo the whole sum each iteration:
def somefunc(array, f, b):
list1, list2 = [], []
value = 0
size = len(array)
for i in range(b-1, size):
if value != 0:
value = value - array[i-b] + array[i] # Get the last value, add the value at index i and remove the value at index i-b
else:
value = sum(array[i+1-b: i+1])
if i + f < size:
list1.append(value)
else:
list2.append(value)
return list1, list2

Add 2 list of floats together

How do i Add 2 lists that both have floats in them. Like how do I actually add the numbers?
Ive tried using
sum(list1, list2) but that did not work
I've also tried
list1+list2 but that didn't work either.
This sounds like a time for a list comprehension.
a = [1.0, 2.0, 3.0]
b = [1.1, 2.2, 3.3]
[x+y for x,y in zip(a,b)]
Or for functional programming.
import operator
map(operator.add, a, b)
The zip method will do. It will combine all elements of a particular index. And since all of them are floating points, just add the values
l1=[1.5,3.2,5.0,3.5]
l2=[8.5,4.7,2.5,7.6]
l3=[i+j for i,j in zip(l1,l2)]
print(l3)

Python print nth element from list of lists [duplicate]

This question already has answers here:
How to print column in python array?
(2 answers)
Closed 5 years ago.
I have the following list:
[[50.954818803035948, 55.49664787231189, 8007927.0, 0.0],
[50.630482185654436, 55.133473852776916, 8547795.0, 0.0],
[51.32738085400576, 55.118344981379266, 6600841.0, 0.0],
[49.425931642638567, 55.312890225131163, 7400096.0, 0.0],
[48.593467836476407, 55.073137270550006, 6001334.0, 0.0]]
I want to print the third element from every list. The desired result is:
8007927.0
8547795.0
6600841.0
7400096.0
6001334.0
I tried:
print data[:][2]
but it is not outputting the desired result.
Many way to do this. Here's a simple list way, without an explicit for loop.
tt = [[50.954818803035948, 55.49664787231189, 8007927.0, 0.0], [50.630482185654436, 55.133473852776916, 8547795.0, 0.0], [51.32738085400576, 55.118344981379266, 6600841.0, 0.0], [49.425931642638567, 55.312890225131163, 7400096.0, 0.0], [48.593467836476407, 55.073137270550006, 6001334.0, 0.0]]
print [x[2] for x in tt]
> [8007927.0, 8547795.0, 6600841.0, 7400096.0, 6001334.0]
And making is safe for potentially shorted lists
print [x[2] for x in tt if len(tt) > 3]
More sophisticated output (python 2.7), prints values as newline (\n) seperated
print '\n'.join([str(x[2]) for x in tt])
> 8007927.0
> 8547795.0
> 6600841.0
> 7400096.0
> 6001334.0
Try this:
for item in data:
if len(item) >= 3: # to prevent list out of bound exception.
print(int(item[2]))
map and list comprehensive have been given, I would like to provide two more ways, say d is your list:
With zip:
zip(*d)[2]
With numpy:
>>> import numpy
>>> nd = numpy.array(d)
>>> print(nd[:,2])
[ 8007927., 8547795., 6600841., 7400096., 6001334.]
Maybe you try a map function
In python 3:
list(map(lambda l: l[2], z))
In python 2:
map(lambda l: l[2], z)
In order to print the nth element of every list from a list of lists, you need to first access each list, and then access the nth element in that list.
In practice, it would look something like this
def print_nth_element(listset, n):
for listitem in listset:
print(int(listitem[n])) # Since you want them to be ints
Which could then be called in the form print_nth_element(data, 2) for your case.
The reason your data[:][2] is not yielding correct results is because data[:] returns the entire list of lists as it is, and then executing getting the 3rd element of that same list is just getting the thirst element of the original list. So data[:][2] is practically equivalent to data[2].

Equal elements in list of lists. Delete one

I have a list of lists
list = [[-2.0, 5.0], [-1.0, -3.0], [1.0, 3.0], [2.0, -5.0]]
What I want to do is delete one the elements of same value should I divide first element with second. For example [-2.0, 5.0] = -2/5 and [2.0, -5.0] = -2/5. I want to delete either [-2.0, 5.0] or [2.0, -5.0] since they produce the same value.
Any ideas?
Can i try like this:
Tuple could be a dictionary key, so I converted the list into tuple after changing to abs
value of the list element and keeping the original list as the values.
>>> lis
[[-2.0, 5.0], [-1.0, -3.0], [1.0, 3.0], [2.0, -5.0]]
>>> dict([(tuple([abs(x[0]), abs(x[1])]), x) for x in lis]).values()
[[2.0, -5.0], [1.0, 3.0]]
>>>
Assuming all your values are all floats (so you can always use float division) you can do the following:
my_list = [[-2.0, 5.0], [-1.0, -3.0], [1.0, 3.0], [2.0, -5.0]]
values_seen = []
new_list = []
for x,y in my_list:
if x/y in values_seen:
continue
else:
values_seen.append(x/y)
new_list.append([x,y])
Now the list you want will be stored as new_list. Note that you should avoid writing a value to the keyword list as you have above.
*Clarification, I am assuming that if you have any more than 2 values that return the same ratio (for example [[1,3],[2,6],[3,9]]) you will want to keep only one of these.
If you want to eliminate all equivalent fractions (meaning [-2.0, 5.0] and [4.0, -10.0] are considered equivalent), then the following code would work.
seen = set()
for numerator, denominator in lst:
quotient = numerator / denominator
if quotient not in seen:
seen.add(quotient)
yield numerator, denominator
Otherwise, if you want the final list to contain both [-2.0, 5.0] and [4.0, -10.0]:
seen = set()
for numerator, denominator in lst:
value = (abs(numerator), abs(denominator), sign(numerator)*sign(denominator))
if value not in seen:
seen.add(value)
yield numerator, denominator
If you're writing this in Python, a language that lacks a sign function, you'll either need to use math.copysign or (numerator > 0) ^ (denominator > 0) where ^ is the xor operator.
This code assumes both numerator and denominator are nonzero.
If you really are keeping a list of numerator-denominator number pairs, consider storing the pairs as immutable tuples or better yet, as Python fractions.
I would first get a unique set of ratios using set:
In [1]: lst = [[-2.0, 5.0], [-1.0, -3.0], [1.0, 3.0], [2.0, -5.0]]
In [2]: rs = list(set([ l[0]/l[1] for l in lst]))
And then just filter out the first occurance of the ratios:
In [3]: [ filter(lambda m: m[0]/m[1] == r , lst )[0] for r in rs ]
Out[3]: [[-2.0, 5.0], [-1.0, -3.0]]
In [4]:
Quick and dirty way, since keys in a dictionary are unique.
{num/denom : [num, denom] for (num, denom) in lst}.values()
In general, comparing floats using == is unreliable, it's normally better to check if they're within a tolerance. e.g.
abs(x-y) < tolerance
a more robust way would might look like the following. An else attached to a for loop just means do this unless you exited the loop early. It's quite handy. This version, however, is quadratic rather than linear time.
div = lambda x,y : x/y
unique = []
for j in range(len(lst)):
for i in range(j):
if abs( div(*lst[i])-div(*lst[j]) ) < tolerance:
break
else
unique.append(lst[j])
unique

Convert elements within an array into floats in python

Hi I am quite new to python and what I want to do is simple but I just can't seem to get around it.
I have a simple array as shown below:
A1 = [('1.000000', '4.000000'), ('2.000000', '5.000000'), ('3.000000', '6.000000'), ('1.000000', '4.000000'), ('2.000000', '5.000000'), ('3.000000', '6.000000')]
I want to change all elements within the array into floats so I can do calculations on them (such as sum etc.). The end results should look something like this:
A2 = [(1.000000, 4.000000), (2.000000, 5.000000), (3.000000, 6.000000), (1.000000, 4.000000), (2.000000, 5.000000), (3.000000, 6.000000)]
I have tried the following:
A2 = [float(i) for i in A1]
however I get the error:
TypeError: float() argument must be a string or a number
Could anyone point me towards a solution.
Thanks in advance
Here's one pretty simple way:
>>> [map(float, x) for x in A1]
[[1.0, 4.0], [2.0, 5.0], [3.0, 6.0], [1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
I like it's because it's short (some would say terse) and since using map() makes it not hardcode or be explicit about the expected format of each x, it just says that it assumes A1 to be a list of sequences.
I have no idea how this compares performance-wise to other solutions (such as the more explicit [(float(x), float(y) for (x, y) in A1] seen below).
Each element of A1 is a tuple ('1.000000', '4.000000'). You will have to convert each item of the tuple:
A2 = [(float(i), float(j)) for (i, j) in A1]
You need to iterate over the inner tuples as well.
A2 = [tuple(float(s) for s in i) for i in A1]

Categories