map () function maps

map () function (mapping)

pattern = "abba"
str = "dog cat cat dog"
res=str.split()
print(list(map(pattern.index,pattern)))
print(list(map(res.index,res)))

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/python/8.01/test.py
[0, 1, 1, 0]
[0, 1, 1, 0]

It can be seen according to the map function can print out a string or a list of maps you want, such as aabb will map out the 0011, you can see that this mapping is based on open 4 c according to the index when the index when the string changes another value

pattern = "abbacc"
str = "dog cat cat dog"
res=str.split()
print(list(map(pattern.index,pattern)))
print(list(map(res.index,res)))


C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/python/8.01/test.py
[0, 1, 1, 0, 4, 4]
[0, 1, 1, 0]

Of course, you can get what you want mapped according to other methods

pattern = "abbaccdddd"
print(list(map(pattern.count,pattern)))


C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/python/8.01/test.py
[2, 2, 2, 2, 2, 2, 4, 4, 4, 4]

Be sure to print conversion into a list, because the version after py3.0, the return value is an iterator

Guess you like

Origin www.cnblogs.com/oxtime/p/11311606.html