I have created a list where within it I seek to eliminate only the lists whose first value is greater than the second value of it.
I tried creating a second list with the elements to remove but I think it is not the most optimal way.
#y = []
x = [[1, 4], [1, 6], [2, 5], [2, 7], [4, 8], [6, 5], [6, 7], [2, 6], [3, 7], [5, 8], [6, 4], [7, 5]]
for i in range(len(x)):
if x[i][0] > x[i][1]:
print(x[i])
# y.append(x[i])
Is there an optimal way to achieve this?
I want that when printing on screen you get the following:
[[1, 4], [1, 6], [2, 5], [2, 7], [4, 8], [6, 7], [2, 6], [3, 7], [ 5, 8]]
Best regards,
This should work:
y = [[a,b] for a,b in x if a <= b]
Testing:
>>> x = [[1, 4], [1, 6], [2, 5], [2, 7], [4, 8], [6, 5], [6, 7], [2, 6], [3, 7], [5, 8], [6, 4], [7, 5]]
>>> y = [[a,b] for a,b in x if a < b]
>>> y
[[1, 4], [1, 6], [2, 5], [2, 7], [4, 8], [6, 7], [2, 6], [3, 7], [5, 8]]
>>>
This modifies the original list:
for i, (a, b) in enumerate(x):
if a > b:
del x[i]
Creating a new list:
[v for v in x if v[0] <= v[1]]
Or elimination in place:
for i in range(len(x) - 1, -1, -1): # need to start at the end
if x[i][0] > x[i][1]:
x.pop(i)
>>> filtered = list(filter(lambda f: f[0] < f[1], x))
>>> print(filtered)
This will create a new list with the desired values, using the built in filter(function, iterable) function.
Related
I am figuring to get a method to search the duplicated parts in a 2D array in Python.
Taking the below array for example:
Array1 = [[1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7]]
Array2 = [[5, 8], [5, 7], [5, 6], [5, 5], [5, 4]]
Array3 = [[1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7]]
ArrayN = [......]
the result I would like to get is like this:
Array_result = [[5,7]]
Is there any method that can automatically search the duplicated areas and save the coordinates?
Here is one way to do so, converting lists to set().
data = [
[[1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7]],
[[5, 8], [5, 7], [5, 6], [5, 5], [5, 4]],
[[1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7]],
]
arr_result = set(tuple(x) for x in data[0])
for arr in data[1:]:
arr_result.intersection_update(tuple(x) for x in arr)
print(arr_result) # {(5, 7)}
As each list contains sub-lists, they must be converted into tuples, because lists are unhashable.
We convert the first list to a set(), then for each list in data, we intersect the set and the list using intersection_update().
Using list comprehension:
arr_result = set.intersection(*[{tuple(x) for x in arr} for arr in data])
print(arr_result) # {(5, 7)}
Cubix48 solution using list comprehension should be preferred, use the below just as reference for other problems.
Using collections:
from collections import Counter
array_list = [[str(x) for x in array] for array in [Array1, Array2, Array3]]
c = Counter()
for item in array_list:
c.update(item)
repeated_items = [eval(item) for item in dict(c).keys() if c[item] == len(array_list)]
Let's say I have a list of of list of 2 elements [[1 , 2], ...] in Python. Now I need to get something like this:
[2, 2] [2, 3] [3, 4] [3, 5] [3, 6] [3, 7]
Second element always goes up by 1, first element too, but occurs 2^n (n>=1) times, before going up.
So next will be 8 lists with first element 4, which occurs 8 times, second element of the lists will go up from 8 to 15.
Maybe you can try nested for loops like below
def fn(n, x):
for i in range(2, n+1):
for j in range(2**(i-1), 2**i):
x.append([i, j])
return(x)
or
from math import log2,ceil
def fn(n,x):
[x.append([ceil(log2(i)),i-1]) for i in range(3,2**n+1)]
return(x)
such that
>>> fn(3, [[1,2]])
[[1, 2], [2, 2], [2, 3], [3, 4], [3, 5], [3, 6], [3, 7]]
>>> fn(4, [[1,2]])
[[1, 2], [2, 2], [2, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 8], [4, 9], [4, 10], [4, 11], [4, 12], [4, 13], [4, 14], [4, 15]]
from math import log
initial_list = [[1,2]]
def extend_pattern(initial_list,n):
last_sublist = initial_list[-1]
first_element = last_sublist[0]
for i in range(n):
last_sublist = list(last_sublist)
if i != 0:
last_sublist[1] += 1
last_sublist[0] = first_element + int(log(i+2,2))
initial_list.append(last_sublist)
extend_pattern(initial_list,10)
print(initial_list)
#[[1, 2], [2, 2], [2, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 8], [4, 9], [4, 10], [4, 11]]
Sounds like a fun homework.
If you intend a function with an output like this:
[[1, 1], [2, 2], [2, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 8], [4, 9], [4, 10]]
then this
def populate(n):
left=1
right=1
temp=right*2
output=[]
for i in range(0,n):
output.append([left,right])
right+=1
if(right==temp):
left+=1
temp=right*2
return output
is one of plenty ways to do it.
If you meant starting with a list and n something like this should work:
def populate(inputlist,n):
left=inputlist[len(inputlist)-1][0]+1
right=inputlist[len(inputlist)-1][1]+1
temp=right*2
for i in range(0,n):
inputlist.append([left,right])
right+=1
if(right==temp):
left+=1
temp=right*2
return inputlist
L = [[1, 2, 3], [4, 3, 6], [7, 8, 1]]
I need a recursive function in python to check the matrix element duplicate. The function should return true if "L" has a row with no duplicate of the same element and false otherwise.
As an academic exercise. No recursion needed.
lst = [
[[1, 2, 3], [4, 3, 6], [7, 8, 1]],
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[1, 2, 3], [4, 8, 6], [7, 8, 9]],
[[1, 2, 3]]
]
for ll in lst:
r = len([x for i,x in enumerate(ll) if [1 for y in ll if len(set(x)&set(y))].count(1) > 1]) == 0
print(r, ll)
Output
False [[1, 2, 3], [4, 3, 6], [7, 8, 1]]
True [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
False [[1, 2, 3], [4, 8, 6], [7, 8, 9]]
True [[1, 2, 3]]
--- Recursive version ---
lst = [
[[1, 2, 3], [4, 3, 6], [7, 8, 1]],
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[1, 2, 3], [4, 8, 6], [7, 8, 9]],
[[1, 2, 3]]
]
def chkdup(lst):
if len(lst) == 1: return True # one list always true
for ls in lst[1:]: # compare other lists
if len(set(lst[0]) & set(ls)): return False # dups found
return chkdup(lst[1:]) # recursion, process next list
for ll in lst: # each list in test data
print(chkdup(ll), ll)
Output
False [[1, 2, 3], [4, 3, 6], [7, 8, 1]]
True [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
False [[1, 2, 3], [4, 8, 6], [7, 8, 9]]
True [[1, 2, 3]]
It is ugly, bulky and needs refactoring, but here you go: (*It is recursive, but recursion seems unnecessary.)
def evaluate_matrix(matrix):
def has_duplicate(arr):
for item in arr:
if arr.count(item) >= 2:
return True
return False
#Here is where all that recursion stuff happens
if len(matrix) <= 1:
return has_duplicate(matrix[0])
else:
return bool(has_duplicate(matrix[0]) + evaluate_matrix(matrix[1:]))
is_double = [[1,2,3,4], [1,1,3,4]]
is_not_double = [5,6,7,8], [[9,10,11,12]]
print("Matrix is_double has a duplicate: ", evaluate_matrix(is_double))
print("Matrix is_not_double has a duplicate: ", evaluate_matrix(is_not_double))
I'm setting up a linear connection and I have two list:
a = [1,1,1,2,2,3,3,3,4]
b = [1,3,7,2,3,4,7,8,9]
a[i] related to b[i]
I regrouped b as c:
c = [[1, 3], [7], [2, 3], [4], [7, 8], [9]]
I tried to add the corresponding value of a in every sublist in c to get:
d = [[1, 1, 3], [1, 7], [2 ,2, 3], [3, 4], [3, 7, 8], [4, 9]]
The first value in every sublist of c that originally in b is related to a like c[0][0] = b[0], and add a[0] to c[0], c[1][0] = b[2], so add a[2] to c[1].
If sublist in c and the first value of the sublist = b[i], add a[i] to every sublist.
I got stuck for this.
You could build an iterator from a and take successive slices of it using itertools.islice in order to consume it according to the length of the sublists in c, but only select the first item from each slice:
from itertools import islice
a = [1,1,1,2,2,3,3,3,4]
c = [[1, 3], [7], [2, 3], [4], [7, 8], [9]]
a_ = iter(a)
[[list(islice(a_, len(j)))[0]] + [i for i in j] for j in c]
Output
[[1, 1, 3], [1, 7], [2, 2, 3], [3, 4], [3, 7, 8], [4, 9]]
Another method. Basic Way.
#!/bin/python
a = [1,1,1,2,2,3,3,3,4]
b = [1,3,7,2,3,4,7,8,9]
c = [[1, 3], [7], [2, 3], [4], [7, 8], [9]]
#d = [[1, 1, 3], [1, 7], [2 ,2, 3], [3, 4], [3, 7, 8], [4, 9]]
element_count=0
d=[]
for x in c:
print (a[element_count])
print(x)
d.append([a[element_count]]+x)
element_count+=len(x)
If I have two 2-dimensional lists, how do I append one to the other, such that the final list is still 2-dimensional?
For example:
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
x.append(y)
print x
Prints out:
[[1, 2], [3, 4], [[5, 6], [7, 8]]]
However, I want it to be:
[[1, 2], [3, 4], [5, 6], [7, 8]]
Append makes new items within a list. Extend is what you are looking for:
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
x.extend(y)
print x
result:
[[1, 2], [3, 4], [5, 6], [7, 8]]
Simply add them:
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
z = x + y
print z
Yields
[[1, 2], [3, 4], [5, 6], [7, 8]]