Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对list类变量执行映射操作_Python_Python 2.7_Python 3.x_Ipython_Ipython Notebook - Fatal编程技术网

Python 对list类变量执行映射操作

Python 对list类变量执行映射操作,python,python-2.7,python-3.x,ipython,ipython-notebook,Python,Python 2.7,Python 3.x,Ipython,Ipython Notebook,下面的代码返回一个错误: class my_class: def __init__(self): self.l = [(1,2),(3,4)] def funct(self): self.l = list(map(lambda x: x[0], l)) print (l) ob = my_class() ob.funct() TypeError:“int”对象不可下标 但是,如果要将逻辑从类中分离出来,它将按预期工作 l = [(1,2),(3,4)] l =

下面的代码返回一个错误:

class my_class:

def __init__(self):
    self.l = [(1,2),(3,4)]

def funct(self):
    self.l  = list(map(lambda x: x[0], l))
    print (l)

ob = my_class()
ob.funct()
TypeError:“int”对象不可下标

但是,如果要将逻辑从类中分离出来,它将按预期工作

l = [(1,2),(3,4)]
l = list(map(lambda x: x[0], l))
print (l)
[1,3]

请任何人解释一下为什么对类变量执行相同的操作会导致上述错误


另外,我使用的是Python3.4,虽然不认为这很重要,但是如果您实际使用属性self.l,代码就可以工作了:

    def funct(self):
        self.l  = list(map(lambda x: x[0], self.l))
        print(self.l)
输出:

[1, 3]
在某个地方,您在某个地方定义的
l
中或在
self.l
中有一个int,如果未定义l,您将在赋值之前得到一个“UnboundLocalError:局部变量“l”

根据您对输入错误的评论,您在
self.l
中有一个
int
,而不仅仅是元组:

class my_class:
    def __init__(self):
        self.l = [3,(1,2),(3,4)]

    def funct(self):
        l  = list(map(lambda x: x[0],self.l))
        print(self.l)
然后运行示例:

In [2]: ob = my_class()

In [3]: ob.funct()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-e9ee341f0f31> in <module>()
----> 1 ob.funct()

<ipython-input-1-e1a6b2682f9f> in funct(self)
      4 
      5        def funct(self):
----> 6            l  = list(map(lambda x: x[0],self.l))
      7            print(self.l)

<ipython-input-1-e1a6b2682f9f> in <lambda>(x)
      4 
      5        def funct(self):
----> 6            l  = list(map(lambda x: x[0],self.l))
      7            print(self.l)

TypeError: 'int' object is not subscriptable
[2]中的
:ob=my_class()
在[3]中:ob.funct()
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 ob.funct()
功能(自我)
4.
5定义函数(自身):
---->6l=列表(映射(lambda x:x[0],self.l))
7打印(self.l)
in(x)
4.
5定义函数(自身):
---->6l=列表(映射(lambda x:x[0],self.l))
7打印(self.l)
TypeError:“int”对象不可下标

`

很抱歉,输入错误。是的,你是对的-这可能与修改一起工作,这意味着我的主代码有问题。请你删除你的答案,这样我就可以删除我的问题了吗?@Dennis,如果是打字错误,错的是你在
self.l
的某个地方有一个
int
,不仅仅是tuples等等,我会尝试在secPadraic中发布真正的代码,非常感谢你的帮助!!!!这并不是你所说的,但在我的代码中查找了一百次之后,我又查找了一次,发现了这个血腥的错误:)谢谢你的时间和所有的帮助best@Dennis,不用担心,如果你找到了解决方案,你可以回答你自己的问题,只需添加原始代码就可以了,这是我的问题,我把实际代码弄乱了,这个问题对社区没有帮助。请把它拿走。非常感谢。