Home  >  Q&A  >  body text

python3.5 如何用map做出和zip同样的效果?

如下面这段代码,我想把map和zip做出同样的效果

name=['a','b','c']
age=[10,11,12]
nation=['中国','にほん','Deutsch']
U1=list(zip(name,age,nation))
print(U1)
U2=map(None,name,age,nation)
print(list(U2))

可是显示:

[('a', 10, '中国'), ('b', 11, 'にほん'), ('c', 12, 'Deutsch')]
Traceback (most recent call last):
  File "F:/python/PT/program/nine neijian3.py", line 8, in <module>
    print(list(U2))
TypeError: 'NoneType' object is not callable

但是我去掉map里面的None:

U2=map(name,age,nation)
print(list(U2))
    

显示:

   print(list(U2))
TypeError: 'list' object is not callable`

请各位大神赐教。

大家讲道理大家讲道理2740 days ago700

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:21:16

    map(lambda a,b,c: (a,b,c), name, age, nation)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:21:16

    name=['a','b','c']
    age=[10,11,12]
    nation=['中国','にほん','Deutsch']
    U1=list(zip(name,age,nation))
    print(U1)
    U2 = map(lambda a,b,c: (a,b,c), name, age, nation)
    print(list(U2))

    The first errorNoneType is for None object, so it is normal that it cannot be output.
    NoneType is the type for the None object, which is an object that indicates no value. You cannot add it to strings or other objects.

    The Python map() method doesn’t seem to be how you use it. As far as I know, it should be like this.
    Description
    is very simple. The first parameter receives a function name, and the second parameter receives an iterable object.
    Syntax
    map(f, iterable)
    Basically equals:
    [f(x) for x in iterable]
    Example

    >>> def add100(x):
    ...     return x+100
    ... 
    >>> hh = [11,22,33]
    >>> map(add100,hh)
    [111, 122, 133]

    http://stackoverflow.com/ques...

    reply
    0
  • Cancelreply