Map the max function for list of the lists - python

I stack with the following problem, I need to finding maximum between equal positions between lists. Map function works pretty well, but how to make it work for the list of the lists? using map(max,d) gave the max of the every list. The problem is that the number of the lists in the list is variable. Any suggestions are welcome!
Input for the problem is d not an a,b,c, d - is a list of the lists, and the comparison is pairwise per position in the list.
a = [0,1,2,6]
b = [5,1,0,7]
c = [3,8,0,8]
map(max,a,b,c)
# [5,8,2,8]
d = [a,b,c]
map(max,d)
[6,7,8]

a = [0,1,2,6]
b = [5,1,0,7]
c = [3,8,0,8]
print [max(itm) for itm in zip(a, b, c)]
or even shorter:
print map(max, zip(a, b, c))

How about this:
max(map(max,d))

Related

Python tuples: compare and merge without for loops

I have two lists of over 100,000 tuples in each. The first tuple list has two strings in it, the latter has five. Each tuple within the first list has a tuple with a common value in the other list. For example
tuple1 = [('a','1'), ('b','2'), ('c','3')]
tuple2 = [('$$$','a','222','###','HHH'), ('ASA','b','QWER','TY','GFD'), ('aS','3','dsfs','sfs','sfs')]
I have a function that is able to remove redundant tuple values and match on the information that is important:
def match_comment_and_thread_data(tuple1, tuple2):
i = 0
out_thread_tuples = [(b, c, d, e) for a, b, c, d, e in tuple2]
print('Out Thread Tuples Done')
final_list = [x + y for x in tuple2 for y in tuple1 if x[0] == y[0]]
return final_list
which ought to return:
final_list = [('a','1','222','###','HHH'), ('b','2','QWER','TY','GFD'), ('c','3','dsfs','sfs','sfs')]
However, the lists are insanely long. Is there any way to get around the computational time commitment of for loops when comparing and matching tuple values?
By using dictionary, this can be done in O(n)
dict1 = dict(tuple1)
final_list = [(tup[1],dict[tup[1]])+ tup[1:] for tup in tuple2]
tuple1 = [('a','1'), ('b','2'), ('c','3')]
tuple2 = [('$$$','a','222','###','HHH'), ('ASA','b','QWER','TY','GFD'), ('aS','3','dsfs','sfs','sfs')]
def match_comment_and_thread_data(tuple1, tuple2):
i = 0
out_thread_dict = dict([(b, (c, d, e)) for a, b, c, d, e in tuple2])
final_list = [x + out_thread_dict.get(x[0],out_thread_dict.get(x[1])) for x in tuple1]
return final_list
by using a dictionary instead your lookup time is O(1) ... you still have to visit each item in list1 ... but the match is fast... although you need alot more values than 3 to get the benefits

Get a tuple from a list of tuple if the element occurs in a list of string

Hi I am working on a data transforming project. I have a List of tuples:
A = [("someThing",0),("someThingOnce",1),("someThingTwice",2)]
and an another list of string:
B = ["something","somethingonce","somethingagain"]
Now what I want to do is, I want the elements from list A that are present in list B.
The desired output is:
C = [("someThing",0),("someThingOnce",1)]
How can I achieve this in an optimised way since, list B has 7000 elements while list A has at max 20 elements.
I can't use numpy as the lists aren't of the same type, i..e B might contain numbers as well.
The tuple[0] in list A elements might repeat as well.
A list-comprehension is the most efficient solution for this (if A has less elements than B).
>>> A = [("someThing",0),("someThingOnce",1),("someThingTwice",2)]
>>> B = ["something","somethingonce","somethingagain"]
>>> C = [(i, j) for i, j in A if i.lower() in B]
>>> C
[('someThing', 0), ('someThingOnce', 1)]

Python, How to concatenate list as index?

a=[1,2,3,4]
b=[5,6,7,8]
Desired outcome from some sort of concatenation.
c=[[1,2,3,4],[5,6,7,8]]
I need this to iterable as to concatenate a different b to c numerous times whilst retaining [[,,,],[,,,],[,,,],.......]
In python you can append anything you want into a list. So for your example, we will start with an empty list c and append a and b. This method will continue to work for an arbitrary number of lists:
a = [1,2,3,4]
b = [5,6,7,8]
c = []
# Now we append
c.append(a)
c.append(b)
If we wanted to do this manually, or as a once only for fixed numbers of lists, we could just defined c as the list containing a and b like so:
c = [a, b]

Opposite of set.intersection in python?

In Python you can use a.intersection(b) to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference() method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^ operator too, if both a and b are sets:
a ^ b
while set.symmetric_difference() takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b), the union of both sets minus the intersection of both sets.
a={1,2,4,5,6}
b={5,6,4,9}
c=(a^b)&b
print(c) # you got {9}
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list

How to copy data in Python

After entering a command I am given data, that I then transform into a list. Once transformed into a list, how do I copy ALL of the data from that list [A], and save it - so when I enter a command and am given a second list of data [B], I can compare the two; and have data that is the same from the two lists cancel out - so what is not similar between [A] & [B] is output. For example...
List [A]
1
2
3
List [B]
1
2
3
4
Using Python, I now want to compare the two lists to each other, and then output the differences.
Output = 4
Hopefully this makes sense!
You can use set operations.
a = [1,2,3]
b = [1,2,3,4]
print set(b) - set(a)
to output the data in list format you can use the following print statement
print list(set(b) - set(a))
>>> b=[1,2,3,4]
>>> a=[1,2,3]
>>> [x for x in b if x not in a]
[4]
for element in b:
if element in a:
a.remove(element)
This answer will return a list not a set, and should take duplicates into account. That way [1,2,1] - [1,2] returns [1] not [].
Try itertools.izip_longest
import itertools
a = [1,2,3]
b = [1,2,3,4]
[y for x, y in itertools.izip_longest(a, b) if x != y]
# [4]
You could easily modify this further to return a duple for each difference, where the first item in the duple is the position in b and the second item is the value.
[(i, pair[1]) for i, pair in enumerate(itertools.izip_longest(a, b)) if pair[0] != pair[1]]
# [(3, 4)]
For entering the data use a loop:
def enterList():
result = []
while True:
value = raw_input()
if value:
result.append(value)
else:
return result
A = enterList()
B = enterList()
For comparing you can use zip to build pairs and compare each of them:
for a, b in zip(A, B):
if a != b:
print a, "!=", b
This will truncate the comparison at the length of the shorter list; use the solution in another answer given here using itertools.izip_longest() to handle that.

Categories

Resources