Concatenate two values in a pair of 2 tuples - python-3.x

So I'm trying to create a function where it combines the elements of a tuple.
So for example:
[(1,2),("Hi","Bye")] will become ['12', 'HiBye'] after the function is implemented.
How can I achieve this in Python?

For pairs, you can do:
>>> list(map(lambda pair:"%s%s"%pair, [(1,2),("Hi","Bye")]))
['12', 'HiBye']
or if you want to handle arbitrary tuples, not just pairs:
>>> list(map(lambda l:''.join(map(str, l)), [(1,2),("Hi","Bye")]))
['12', 'HiBye']
>>> list(map(lambda l:''.join(map(str, l)), [(1,2,3),("Hi","Bye","Ciao")]))
['123', 'HiByeCiao']

Try this:
L = [(1, 2), ('Hi', 'Bye')]
L = list(map(lambda t : str(t[0]) + str(t[1]), L))

Using list comprehension:
myList = [(1,2),("Hi","Bye")]
answer = [str(t[0]) + str(t[1]) for t in myList]

Try combining map() and reduce() :
import operator
lst = [("1", "2"), ("c", "d")]
map(lambda x: reduce(operator.concat, x, ""), lst)
The matching items must be concatenatable, i.e. you need to convert them to e.g. strings first. Or include it in the code :
import operator
lst = [(1, 2, 3, 52), ("c", "d")]
map(lambda x: reduce(operator.concat, map(str, x), ""), lst)
Using string.join() is also possible (and probably more pythonic):
lst = [(1, 2, 3, 52), ("c", "d")]
map(lambda x: ''.join(map(str, x)), lst)

Related

How do I show the output instead of <filter object>

L = [9, 2, 1, 3, 4, 5, 6]
x = str(filter(lambda x: x > 30, map(lambda x: x*x, L)))
I know that in order to remove the or whatever at the output, I can add a list() in front or tuple ().
I know the answer for x is [81, 36], but how do I actually produce it at the output? I cant seem to make it work with list() or tuple()
You are converting it to a str.. remove the str()
x = list(filter(lambda x: x > 30, map(lambda x: x*x, L)))
Converting the filter-object to str will only covert it to str:
x = str(filter(lambda x: x > 30, map(lambda x: x*x, L)))
print(type(x)) # <class 'str'>
What you need is to convert it into list instead of str:
L = [9, 2, 1, 3, 4, 5, 6]
x = list(filter(lambda x: x > 30, map(lambda x: x*x, L)))
OUTPUT:
[81, 36]
filter() is an equivalent of defining a generator with the same behavior, not a list. Generators have the property of generating the values not until you call the generator's __next__() member function, what is essentially what happens during iteration.
As other answers stated, you can convert a generator to a list using list() (see here for example), but be aware that generators are one-time-use objects, and trying to use list() a second time will result in an empty list.
The python docs say
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

How to do Groupby and map in one go in python just like we do in scala

we have a list in Scala :
val testList = List("Sita" -> 1, "Sita" -> 2, "Ram" -> 3, "Ram" -> 4, "Shyam" -> 5)
I applied this on list to group the values by key in Scala:
val res = testList.groupBy(_._1).map { case (k, v) => k -> v.map(_._2).sum }
and i got this as a result:
Map(Shyam -> 5, Ram -> 7, Sita -> 3)
I want to do the same in python Please help me out:
testList = [("Sita", 1), ("Sita", 2), ("Ram", 3), ("Ram", 4), ("Shyam", 5)]
Edit: And what if i have a list like this:
testList = [("Sita_English", 1), ("Sita_Maths", 2), ("Ram_English", 3), ("Ram_Maths", 4), ("Shyam_English", 5)]
testList = [("Sita", 1), ("Sita", 2), ("Ram", 3), ("Ram", 4), ("Shyam", 5)]
from itertools import groupby
out = {v: sum(i[1] for i in g) for v, g in groupby(testList, lambda k: k[0])}
print(out)
Prints:
{&apos;Sita&apos;: 3, &apos;Ram&apos;: 7, &apos;Shyam&apos;: 5}
A little explanation:
itertools.groupby() (doc) returns consecutive keys and groups from the iterable. In this case the iterable is tesList. The key function lambda k: k[0] returns first element of the tuple -> so we are grouping along the first element.
The dict comprehension is using this first element as a key and second elements from the group as parameter to the sum() function.
EDIT: Using only map():
testList = [("Sita", 1), ("Sita", 2), ("Ram", 3), ("Ram", 4), ("Shyam", 5)]
from itertools import groupby
from operator import itemgetter
out = dict(map(lambda v:(v[0], sum(map(itemgetter(1), v[1]))), groupby(testList, itemgetter(0))))
print(out)
I think you want to have a solution in functional way:
out = list(map(lambda v:(v[0], sum(map(lambda s: s[1], v[1]))), groupby(testList, key=lambda x: x[0])))

How to permute arrays based on a predefined list of indexes

Here are my few lines of code
import numpy as np
X = ([1,2,3], [2,4,5], [5,6,7])
y = ([2], [4], [5])
dist= np.random.RandomState(1)
r = dist.permutation(len(y))
Let's say
r= array([0, 2, 1])
Is there a way to permute the elements of X and y so that X and y elements are re-ordered according to indexes in r array i.e. y becomes ([2], [5], [1]) and X becomes ([1,2,3], [5,6,7], [2,4,5]) ? I didn't find out the answer in python doc. Thanks
You can use comprehension with zip:
[i for _, i in sorted(zip(r, X])]
EDIT
When you zip this happens:
zip(r, X)
#((0, [1,2,3]), (2, [2,4,5]), (1, [5,6,7]))
Then you use sorted() this happens:
sorted(zip(r, X])
((0, [1,2,3]), (1, [5,6,7]), (2, [2,4,5]))
When you take only second element from ziped and sorted you get this:
([1,2,3], [5,6,7], [2,4,5])

Python - eliminating common elements from two lists

If i have two lists:
a = [1,2,1,2,4] and b = [1,2,4]
how do i get
a - b = [1,2,4]
such that one element from b removes only one element from a if that element is present in a.
You can use itertools.zip_longest to zip the lists with different length then use a list comprehension :
>>> from itertools import zip_longest
>>> [i for i,j in izip_longest(a,b) if i!=j]
[1, 2, 4]
Demo:
>>> list(izip_longest(a,b))
[(1, 1), (2, 2), (1, 4), (2, None), (4, None)]
a = [1,2,1,2,4]
b = [1,2,4]
c= set(a) & set(b)
d=list(c)
The answer is just a little modification to this topic's answer:
Find non-common elements in lists
and since you cannot iterate a set object:
https://www.daniweb.com/software-development/python/threads/462906/typeerror-set-object-does-not-support-indexing

Finding set difference between two complex dictionaries

I have two dictionaries of the following structure:
a) dict1 = {'a':[ [1,2], [3,4] ], 'b':[ [1,2],[5,6] ]}
b) dict2 = {'a':[ [1,2], [5,6] ], 'b':[ [1,2],[7,8] ]}
I need to find the set difference between each key in the dictionary i.e., dict1['a'] - dict2['a'] should return [3,4]. Any thought is appreciated.
The use of mutable items (such as lists) makes the problem MUCH harder, as it precludes the simple use of Python's set data structure. It may be worth making temporary copies/versions which actually use tuples in lieu of those pesky lists:
def tempaux(d):
return dict((k, set(tuple(x) for x in v))
for k, v in d.iteritems())
Now:
def thedifs(dd1, dd2)
d1 = tempaux(dd1)
d2 = tempaux(dd2)
allkeys = set(d1).update(d2)
empty = set()
difs = []
for k in allkeys:
s1 = d1.get(k, empty)
s2 = d2.get(k, empty)
adif = s1 - s2
if adif: difs.append(adif)
return difs
This assumes actual set difference rather than symmetric difference etc. You can of course turn back the tuples into lists before returns, &c, depending on your exact requirements.
>>> s1 = set([(1,2), (3,4)])
>>> s2 = set([(1,2), (5,6)])
>>> s1 - s2
{(3, 4)}
You've got the wrong data structure for what you're trying to do.
Use this instead.
dict1 = {'a': [(1, 2), (3, 4)], 'b': [(1, 2), (5, 6)]}
dict2 = {'a': [(1, 2), (5, 6)], 'b': [(1, 2), (7, 8)]}
Life is simpler when you try to do set operations on immutable objects like tuples.
This will transform your list-of-lists into a list-of-tuples.
>>> dict( (key,[tuple(v) for v in dict1[key]]) for key in dict1 )
{'a': [(1, 2), (3, 4)], 'b': [(1, 2), (5, 6)]}
Here's the complete solution.
>>> dict1t= dict( (key,[tuple(v) for v in dict1[key]]) for key in dict1 )
>>> dict2t= dict( (key,[tuple(v) for v in dict2[key]]) for key in dict2 )
>>> set(dict1t['a'])-set(dict2t['a'])
set([(3, 4)])
applicable to list or dict or number when a and b share the same structure
c={'a':'1','b':'2'}
d={'a':'10','b':'20'}
e={'x':c,'t':15}
f={'x':d,'t':19}
def diff(a,b):
if isinstance(a, int) and isinstance(b, int):
b = b - a
return b
if isinstance(a, str) and isinstance(b, str):
if a.isdigit() and b.isdigit():
b = str(int(b) - int(a))
return b
else:
b = a
return b
if type(a) is list and type(b) is list:
for i in range(len(a)):
b[i] = diff(a[i],b[i])
return b
if type(a) is dict and type(b) is dict:
for k,v in b.iteritems():
b[k] = diff(a[k],b[k])
return b
print diff(e,f)

Resources