Try To Print Map as str/list/join without succeess - python

def intersection(list_1, list_2):
return map(lambda list0 : list(dict.fromkeys(list0)), [x for x in list_1 if x in list_2 ])
print(intersection([5, 5, 6, 6, 7, 7], [1, 5, 9, 5, 6]))
In lambda the func that deletes duplicate and in [ ] the list (from list comprehension) the transfered to the func by map.
When I try to turn it into a list / str / join this error accourced.
What can I do and why is this happening ?
I added one example run that suppose to print [5,6] but returns error.
Be noticed that the list itself ([ ]) reutrns alone: [5, 5, 6, 6] because of the duplicates in the original lists.
Also if I change the code from one line to block og code it works:
def intersection(list_1, list_2):
my_list = [x for x in list_1 if x in list_2]
small_func = lambda list0 : list(set(list0))
return small_func(my_list)
returns: [5,6]
Thanks !

I don't think you need map here. Your idea was right to convert to dictionary and than convert back to list but you have to do that only once.
def intersection(list_1, list_2):
return list(dict.fromkeys([x for x in list_1 if x in list_2]))
print (intersection([5, 5, 6, 6, 7, 7], [1, 5, 9, 5, 6]))

Related

Recursion on nested list

I am trying to use recursion on a nested list. Here I am trying to get the innermost element.
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
list1 = input_list
def sublist1(list, sublist=[]):
if len(list) == 0:
return sublist
else:
sublist.append(list)
sublist1(list[1:], sublist)
print(sublist)
sublist1(list1)
The output I am getting is this:
[[1, 2, 3, 4, [5, 6, 7, [8, 9, [10]]]], [2, 3, 4, [5, 6, 7, [8, 9, [10]]]], [3, 4, [5, 6, 7, [8, 9, [10]]]], [4, [5, 6, 7, [8, 9, [10]]]], [[5, 6, 7, [8, 9, [10]]]]]
I tried changing the index but it's not giving me the expected output [10].
Any help would be appreciated.
You can make a recursive call only if any item in the current given list is a list; otherwise it means the current list is already the innermost list, so you can return the list as is:
def inner_most(lst):
for i in lst:
if isinstance(i, list):
return inner_most(i)
return lst
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
print(inner_most(input_list))
This outputs:
[10]
Demo: https://replit.com/#blhsing/SwelteringFlakyDividend
This will answer your nested travels
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
list1 = input_list
def sublist1(list):
if(len(list) > 1):
for l in list:
#print(l)
# print(type(l))
if type(l) == type([]) :
return sublist1(l)
else:
continue
return list
print(sublist1(list1))
print(list1[-1][-1][-1])
This would get you [10]

Recursive method to zip list?

I have got a nested list of list that looks like the following,
list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
However, I would like to find out a method to concatenate the first index of each list with the first index of the other list.
list1 = [[1,4,7,10],[2,5,8,11],[3,6,9,12]]
I have tried doing list comprehension by using the following code
list1 = [[list1[j][i] for j in range(len(list1)) ] for i in range(len(list1[0])) ]
# gives me
# list1 = [[1,4,7,10],[2,5,8,11],[3,6,9,12]]
However, i was hoping alternative methods to achieve the same results, hopefully something that is simpler and more elegant.
Thanks in advance.
zip is a built-in method and does not require outside packages:
>>> list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>>> print([list(x) for x in zip(*list1)])
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
Notice the *list1! This is needed since list1 is a nested list, so the * unpacks that list's elements to the zip method to zip together. Then, since zip returns a list of tuples we simply convert them to lists (as per your request)
A possible recursion solution can utilize a generator:
def r_zip(d):
yield [i[0] for i in d]
if d[0][1:]:
yield from r_zip([i[1:] for i in d])
print(list(r_zip(list1)))
Output:
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
x = min([len(list1[i]) for i in range(len(list1))])
[[i[j] for i in list1] for j in range(x)]
Or try using:
>>> list1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>>> list(map(list, zip(*list1)))
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
>>>

Ordered list from elements of a 2 dimensional list?

I have a 2 dimensional list. Example:
list = [[1,4,7], [2,5,8], [3,6,9]]
I need to make a new list whose elements are the elements of each sub-element preserving the order.
Example:
final_list = [1,2,3,4,5,6,7,8,9]
Q: The new list must take the first element of each list first, then the
second, and so on..
A:
You can use zip and a list comprehension.
list_1 = [1,4,7]
list_2 = [2,5,8]
list_3 = [3,6,9]
print([x for lst in zip(list_1, list_2, list_3) for x in lst])
# outputs > [1, 2, 3, 4, 5, 6, 7, 8, 9]
For your new list, you could do the same, but first unpack the list with a *
new_list = [[1,4,7], [2,5,8], [3,6,9]]
print([x for lst in zip(*new_list) for x in lst])
# outputs > [1, 2, 3, 4, 5, 6, 7, 8, 9]
first transpose the list matrix with zip then chaining all the inner list together with itertools
also rename variable list to lst, not a good practice to use builtin list as variable name
import itertools
lst = [[1,4,7], [2,5,8], [3,6,9]]
final_list = list(itertools.chain.from_iterable(zip(*lst)))
# final_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

python nested list comprehensions

I am learning python and going through their tutorials. I understand list comprehensions and nested lists comprehensions. With the following code, though, I am trying to understand the order of events.
>>> matrix = [
...[1, 2, 3, 4],
...[5, 6, 7, 8],
...[9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4,8,12]]
According to the nested list comprehension, is the first "i" and the second "i" the same variable and do they both increase at the same time? I guess I don't understand how the resulting big list goes from the first sublist [1, 5, 9] to the second sublist [2, 6, 10]
[[row[i] for row in matrix] for i in range(4)]
is equivalent to
my_list = []
for i in range(4):
my_list_2 = []
for row in matrix:
my_list_2.append(row[i])
my_list.append(my_list_2)
is the first "i" and the second "i" the same variable and do they both increase at the same time?
Of course, it is. If it was not the same i, the code would throw an error because one of the two would not be defined.
You may be interested in this question: Understanding nested list comprehension
I made a function in order to do it automatically (sorry for the example, i took it from someone) :
Let's say, you have this example :
# 2-D List
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flatten_matrix = []
for sublist in matrix:
for val in sublist:
flatten_matrix.append(val)
This is my function :
(first, turn the example into a string that you will send to the function)
x = "for sublist in matrix:for val in sublist:flatten_matrix.append(val)"
then the function :
def ComprenhensionizeList(nested_for_loop_str):
splitted_fors = nested_for_loop_str.split(':')
lowest_val = splitted_fors[1].split(' ')[1]
comprehensionizer = '[ '+ lowest_val+' '+splitted_fors[0]+' '+splitted_fors[1]+' ]'
print(comprehensionizer)
and the output :
[ val for sublist in matrix for val in sublist ]

Comparing Two Lists to Find Bigger List

I have two lists and need to compare them by their largest element, if that is tied, their 2nd largest element, and if that is tied, 3rd largest etc iterating to the entire array.
For example:
list1= [0,2,3,6,12]
list2= [1,2,3,6,12]
list3= [1,4,5,8,12]
list4= [1,4,5,9,12]
So list4 > list3 > list2 > list1.
I wrote a function that accomplishes this:
def compare(x,y):
if sorted(x)==sorted(y):
return "Tie"
for index in range(len(x)-1,-1,-1):
if sorted(x)[index]>sorted(y)[index]:
return x
elif sorted(x)[index]<sorted(y)[index]:
return y
I was wondering if there was a much neater and more efficient way of writing the function because it doesn't seem very Pythonic.
Edit: comparing lists by using "<" and ">" will sort the lists from the smallest index to largest index, not largest index to smallest index. Reversed would make ">" and "<" the simplest solutions.
How about this?
>>> list1= [0,2,3,6,12]
>>> list2= [1,2,3,6,12]
>>> list3= [1,4,5,8,12]
>>> list4= [1,4,5,9,12]
>>> def sort_lists_by_maxes(*lists):
return sorted(lists, key=lambda x: sorted(x, reverse=True), reverse=True)
>>> sort_lists_by_maxes(list1, list2, list3, list4)
[[1, 4, 5, 9, 12], [1, 4, 5, 8, 12], [1, 2, 3, 6, 12], [0, 2, 3, 6, 12]]
The lists are compared by their individually sorted values, and you can feed as many lists as you would like into the function as arguments.
sorted of sorted is good, but I'm partial to:
#!/usr/local/cpython-3.3/bin/python
list1 = [0, 2, 3, 6, 12]
list2 = [1, 2, 3, 6, 12]
list3 = [1, 4, 5, 8, 12]
list4 = [1, 4, 5, 9, 12]
metalist = [ list1, list2, list3, list4 ]
for sublist in metalist:
sublist.sort(reverse=True)
metalist.sort(reverse=True)
print(metalist)
It should be faster and take less memory. It's also more clear to novices.

Categories