Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map one list to another in python? [duplicate]

Tags:

python

['a','a','b','c','c','c']

to

[2, 2, 1, 3, 3, 3]

and

{'a': 2, 'c': 3, 'b': 1}
like image 849
user288832 Avatar asked Mar 08 '10 14:03

user288832


People also ask

How do I map a list to another list in Python?

The map() function iterates over all elements in a list (or a tuple), applies a function to each and returns a new iterator of the new elements. In this syntax, fn is the name of the function that will call on each element of the list. In fact, you can pass any iterable to the map() function, not just a list or tuple.

How do you map 2 lists in Python?

To map two lists together, we can use the Python zip() function. This function allows us to combine two lists together. We can use one list as the keys for the dictionary and the other as the values. If the lists vary in size, this method will truncate the longer list.

How do I copy one list to another?

Another approach to copying elements is using the addAll method: List<Integer> copy = new ArrayList<>(); copy. addAll(list); It's important to keep in mind whenever using this method that, as with the constructor, the contents of both lists will reference the same objects.


1 Answers

>>> x=['a','a','b','c','c','c']
>>> map(x.count,x)
[2, 2, 1, 3, 3, 3]
>>> dict(zip(x,map(x.count,x)))
{'a': 2, 'c': 3, 'b': 1}
>>>
like image 106
YOU Avatar answered Nov 15 '22 15:11

YOU