-1

I'm now to Python and having problems to understand the following line

working_docs = [map(lambda (sid, sent): (sid, [sent]), d) for d in tokenized_docs]

I had googled "map" and "lambda", but this line doesn't conform. What is it trying to do? Can you please translate it into simple code?

tokenized_docs is a list of list of tuples and have the value as below: enter image description here

Zheng Xie
  • 415
  • 3
  • 13

4 Answers4

3

Python 3 does not allow tuple unpacking in the argument list of a lambda expression. You need lambda x: (x[0], [x[1]]) instead.

In Python 2, the given code would have created a list of lists. map, however, is now a class rather than a function, so you'll want to explicitly create a list from the resulting map instance.

working_docs = [list(map(lambda x: (x[0], [x[1]]), d)) for d in tokenized_docs]

However, dropping both map and the lambda expression in favor of a list comprehension would be simpler.

working_docs = [[(sid, [sent]) for sid, sent in d] for d in tokenized_docs]
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you. This code is from Github, it is written in Python 2 and now I want to run in Python 3. Your translation created errors later in the code when working_docs is used: for doc in working_docs: for sent_id, phrases in doc: (error in this line: "TypeError: 'int' object is not iterable") – Zheng Xie Nov 19 '19 at 16:56
  • Ah, ok, so it appears the intent was to create a list of lists, so my first point doesn't apply. I'll update. – chepner Nov 19 '19 at 16:58
2

Break it down:

working docs = [f(d) for d in tokenized_docs]

This simple creates a new list with f applied to every d in toeknized_docs.

map(g, itr)

This applies the function g to every element in itr, producing an iterable. list(map(g, itr) is equivalent to [g(i) for i in itr].

lambda (sid, sent): (sid, [sent])

This takes in a tuple and returns that same tuple but with its second element as a singleton list

I think this is much easier to see as

def f(tup):
    x, y = tup
    return x, [y]

return [[f(t) for t in d]           # this is a list in both python 2 and 3
        for d in tokenized_docs]
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
2

Without map and list comprehension could be written as:

working_docs = []
for td in tokenized_docs:
    wd = []
    for (sid, sent) in td:
        wd.append((sid, [sent]))
    working_docs.append(wd)
rivamarco
  • 719
  • 8
  • 23
1

Its not clear what is the format of tokenised_list.

Irrespective of that ,

if you still want to use map and lambda in such formats I think the following example can make you clear:

Example 1:

td=[(1,'akshay'),(2,'laksshay')]
wd=[[x,y] for  x,y in td]

Output : [[1, 'akshay'], [2, 'laksshay']]

Example 2:

td=[(1,'akshay'),(2,'laksshay')]
wd=[(d[0],[d[1]]) for d in td]

Output: [(1, ['akshay']), (2, ['laksshay'])]

Example 3:

td=[(1,'akshay'),(2,'laksshay')]
list=[]
for i in td:
a,b=[j for j in i]
list.append(lambda a,b:[a,[b]])

Output: list is a list of two lambda function objects( the lambda functions that will convert for example (1,'akshay') to [1,['akshay']].

wd=[map(lambda x,y:[x,[y]],td) for x,y in td]

Output: wd is now a list of two map objects.

Map function has the advantage of saving your memory. Otherwise you can use the third example in your own way.

I hope these examples will help you to understand and it will be better if you kindly give a better view of tokenised list.

To get a easy and more clear view of map function kindly look at :

How map can be written in an easier way?

And to speak in ground level lambda function can be replaced with def keyword which we use for defining user defined functions in python. In lambda we write the same thing in one line. Now obviously you cannot make every user defined functions as lambda. So it depends upon the situation.

Thank you.