Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: reduce (list of strings) -> string

>>> import functools as ft
>>> bk = ft.reduce(lambda x, y: x[0] + "." + y[0], ['alfa', 'bravo', 'charlie', 'delta'])
>>> bk
'a.d'
>>> km = ft.reduce(lambda x, y: x + y, [1, 2, 3, 4])
>>> km
10
>>> bk = ft.reduce(lambda x, y: x[0] + y[0], ['alfa', 'bravo', 'charlie', 'delta'])
>>> bk
'ad'
>>>

I expected something like 'a.b.c.d' or 'abcd'. Somehow, can't explain the results. There are similar questions here, but not quite like this one.

like image 552
Alexey Orlov Avatar asked Dec 12 '14 04:12

Alexey Orlov


People also ask

How do you convert a list to a string in python with reduce?

Convert list to string using reduce() in python. functools module in python provides a function reduce(), which accepts a iterable sequence as argument and a function as argument. This function generates a single value from all the items in given iterable sequence.

How do you convert a list of elements to a string in Python?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

What is reduce () in Python?

Python's reduce() is a function that implements a mathematical technique called folding or reduction. reduce() is useful when you need to apply a function to an iterable and reduce it to a single cumulative value.


1 Answers

The result of executing the function passed as the first parameter, will be the first parameter to that function in the next iteration. So, your code works like this

lambda x, y: x[0] + "." + y[0]
  1. When x, y are 'alfa' and 'bravo' respectively, a.b.

  2. Now, x will be a.b and y will be 'charlie', so result will be a.c

  3. Now, x will be a.c and y will be 'delta', so result will be a.d

That is why the result is a.d

To get what you wanted, take all the first characters from all the strings to form a list and join all the elements together with ., like this

print(".".join([item[0] for item in data]))
# a.b.c.d

Note: I won't prefer this way, but for the sake of completeness, you can do it with reduce, like this

data = ['alfa', 'bravo', 'charlie', 'delta']
print(ft.reduce(lambda x, y: x + ("." if x else "") + y[0], data, ""))
# a.b.c.d

Now, the last empty string will be the first value for x in the first iteration. And we use . only if x is not an empty string otherwise we use an empty string, so that the concatenation would give the result you wanted.

like image 178
thefourtheye Avatar answered Sep 19 '22 06:09

thefourtheye