Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 can类数据';int';eger可以通过列表引用,然后使用';对于';a';如果';x>;Y_Python_List_Class_For Loop - Fatal编程技术网

Python can类数据';int';eger可以通过列表引用,然后使用';对于';a';如果';x>;Y

Python can类数据';int';eger可以通过列表引用,然后使用';对于';a';如果';x>;Y,python,list,class,for-loop,Python,List,Class,For Loop,我很难通过列表引用类,然后比较所需列表输出的整数 如果我想做python做不到的事情,请举一些正确引用的例子 请参见下面的示例代码: ''' ''我继续为您清理代码。我想我明白了你最后想用逻辑块做什么。希望这是你想要的。附加到mylist的第一个Class1对象被实例化为全零,这将导致所有比较返回false,因此obj.m->0将附加到compare列表 class Class1: def __init__(self, m, x, y, z): self.m = m

我很难通过列表引用类,然后比较所需列表输出的整数

如果我想做python做不到的事情,请举一些正确引用的例子

请参见下面的示例代码:

'''


''

我继续为您清理代码。我想我明白了你最后想用逻辑块做什么。希望这是你想要的。附加到
mylist
的第一个
Class1
对象被实例化为全零,这将导致所有比较返回false,因此
obj.m
->0将附加到
compare
列表

class Class1:
    def __init__(self, m, x, y, z):
        self.m = m
        self.x = x  # int
        self.y = y  # int
        self.z = z  # int


# creating list
mylist = []
compare = []

# appending instances to list
mylist.append(Class1(0, 0, 0, 0))
mylist.append(Class1(2, 3, 4, 5))
mylist.append(Class1(3, 4, 5, 6))

# comparison values
a = 1
b = 2
c = 3

# boolean
# I cleaned up the logic. Hopefully it's easier to understand
for obj in mylist:
    comparison = False
    if obj.x > a or obj.y > b or obj.z > c:
        comparison = True

    if not comparison:
        compare.append(obj.m)
        print("All comparisons were false")
    else:
        print("At least one comparsion is true")

print(compare)

问题是什么?代码不起作用吗?如果是,错误是什么?在for。。。在第节中。我不知道如何引用上面列表中的数据。需要注意的是,您正在覆盖一个内置的
list
在python中是一个保留字,您给它分配了一个空列表。list只是为了向我强调创建了什么。为了得到正确的秩序。列表将不会在实际代码中使用我仍然不明白,您的for循环看起来很好,它工作不正常吗
obj
在循环中迭代时引用每个列表元素,
obj.x
将引用对象的
x
属性。谢谢!正确/错误,需要颠倒,但我会试一试!是的,我翻转了,但在最后的if子句中包含了一个
not
,以否定翻转。我只是发现,当其中一个比较返回true时,比较变量也将为true,那么遵循逻辑就更容易了。不管是哪种方式,我都希望能有所帮助
class Class1:
    def __init__(self, m, x, y, z):
        self.m = m
        self.x = x  # int
        self.y = y  # int
        self.z = z  # int


# creating list
mylist = []
compare = []

# appending instances to list
mylist.append(Class1(0, 0, 0, 0))
mylist.append(Class1(2, 3, 4, 5))
mylist.append(Class1(3, 4, 5, 6))

# comparison values
a = 1
b = 2
c = 3

# boolean
# I cleaned up the logic. Hopefully it's easier to understand
for obj in mylist:
    comparison = False
    if obj.x > a or obj.y > b or obj.z > c:
        comparison = True

    if not comparison:
        compare.append(obj.m)
        print("All comparisons were false")
    else:
        print("At least one comparsion is true")

print(compare)