Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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 是否向类中列表的所有元素添加编号?_Python_Python 2.7 - Fatal编程技术网

Python 是否向类中列表的所有元素添加编号?

Python 是否向类中列表的所有元素添加编号?,python,python-2.7,Python,Python 2.7,因此,我有一个带有列表的类,我想向该列表的所有元素添加一个数字,但不是向类中的每个列表添加一个数字。我试过这个: class class_name: def __init__(self,list_name,other_list): self.list_name = list_name self.other_list = other_list list1 = [1.0,2.0,3.0,4.0] list2 = [4.0,5.0,6.0,7.0]

因此,我有一个带有列表的类,我想向该列表的所有元素添加一个数字,但不是向类中的每个列表添加一个数字。我试过这个:

class class_name:
    def __init__(self,list_name,other_list):
        self.list_name = list_name
        self.other_list = other_list

list1 = [1.0,2.0,3.0,4.0]
list2 = [4.0,5.0,6.0,7.0]    

data = [0]*len(list1)    

for i,(l1,l2) in enumerate(zip(list1,list2)):
    data[i] = class_name(l1,l2)

[(x + 5.0).list_name  for x in data]
这给了我一个错误:

TypeError: unsupported operand type(s) for +: 'instance' and 'float'
编辑:人们似乎不明白我想要什么。在我的实际代码中,这些列表已经添加到了一个类中,在本例中,这些数据是我一直在处理的数据,但是类中专门涉及震级的列表之一需要校准。我通过给列表中的每个元素添加一个数字来校准它。必须对连接到它所在的类的列表执行此操作,因此在将列表放入类之前,我无法编辑该列表

我已经在我的代码中很早就创建了这个类,我需要它保持以前的样子,这样我就可以处理所有的元素。现在,在代码的后面,我想在类中校准这个数量级列表。有办法吗

也许这一尝试更好地说明了我所要做的:

[x.list_name for x in data] = [x.list_name+5  for x in data]
这也不起作用,我得到以下错误:

SyntaxError: can't assign to list comprehension

我只是觉得它能让人们理解我需要什么。

如果您想增加存储为成对列表的两个列表中的一个,这应该可以:

[x.list_name+5.0 for x in class_names]

x不是数字,它是类名对象。您希望从x.list\u名称中检索要增加的内容,然后添加5.0。

查看python的映射函数

编辑:也许试试这个

for class_name in class_names:
    class_name.list_name = map(lambda x: x+5 , class_name.list_name)

首先向实例添加值,然后访问属性


我不确定你的意思,但我创建了一个简单的示例:

class class_name(object):
    def __init__(self,list_name,other_list):
        self.list_name = list_name
        self.other_list = other_list

        self.list1 = []
        self.list2 = []

        self.list1.append(self.list_name)
        self.list2.append(self.other_list)

        print "My List1: ", self.list1
        print "My List2: ", self.list2

def input_data():
    list_one = raw_input("Data for list 1: ")
    list_two = raw_input("Data for list 2: ")

    class_name(list_one, list_two)

if __name__ == '__main__':
    input_data()

这就是您想要的吗?

您认为x+5.0将如何工作?x是一个实例,现在还不清楚你到底想要什么。你能清理你的代码并使用更好的变量名吗?例如,我几乎给了你两次不好的建议,因为我总是忘记class_name是你的类,class_name是某种我不理解其用途的列表。你可以使用lambda而不是add_five@Pynchia添加到我的示例中。没有太多使用lambda,所以不总是首先想到它。这会在将列表放入类之前编辑列表。在我使用的真实代码中,而不是这个成对的代码中,我必须在将列表放入类后编辑它。我想编辑课程列表\名称。@Wilsonwatson你的解释很混乱。你的术语有点不恰当。list\u name不是一个类,而是该类的列表成员。我做了一个编辑,应该在您定义的类的对象实例列表中的每个对象中修改此成员。对不起,我对所有这些都不是很熟悉,这就是为什么我遇到这么多麻烦的原因。我尝试使用您的编辑,得到了以下结果:TypeError:map的参数2必须支持打印我想要的内容的迭代,但我希望实际修改list_名称,以便当我从类中重新打印list_名称时,它会给我修改后的+5.0值,然后将其更改为常规for循环,而不是列表理解。此代码创建一个新列表。您可能想要enumeratex.list_name中的i元素:x.list_name[i]+=5.0或其他什么。
class class_name:
    def __init__(self,list_name,other_list):
        self.list_name = list_name
        self.other_list = other_list

list1 = [1.0,2.0,3.0,4.0]
list2 = [4.0,5.0,6.0,7.0]    

class_names = [0]*len(list1)    

for i,(l1,l2) in enumerate(zip(list1,list2)):
    class_names[i] = class_name(l1,l2)

print [x.list_name+5.0 for x in class_names]
class class_name(object):
    def __init__(self,list_name,other_list):
        self.list_name = list_name
        self.other_list = other_list

        self.list1 = []
        self.list2 = []

        self.list1.append(self.list_name)
        self.list2.append(self.other_list)

        print "My List1: ", self.list1
        print "My List2: ", self.list2

def input_data():
    list_one = raw_input("Data for list 1: ")
    list_two = raw_input("Data for list 2: ")

    class_name(list_one, list_two)

if __name__ == '__main__':
    input_data()