简体   繁体   English

如何通过 class 方法迭代元组列表中每个元组的第一个元素?

[英]How can I iterate the first element of each tuple in a list of tuples through a class method?

I am trying to determine if the first element meets a condition with an if statement, and if it does the next two items of the tuple will be inputted into the class method.我正在尝试使用 if 语句确定第一个元素是否满足条件,如果满足,则元组的下两项将输入到 class 方法中。 so for example:例如:

x = [(345, 1000, 5),(576, 1345, 8), (234, 1175, 3)]

In this instance I want to run the first element of the tuple, and if it means the condition, have the next two numbers be placed in the class method.在这种情况下,我想运行元组的第一个元素,如果它表示条件,则将接下来的两个数字放在 class 方法中。

So far I have attempted to iterate by:到目前为止,我试图通过以下方式进行迭代:

[i[0] for i in x]:
         if self.i >= 300 and self.i <=750:
              self.test1=[y[1] for y in x] 

I have learned this cannot be done, and I cannot seem to get around this issue.我知道这是无法做到的,而且我似乎无法解决这个问题。

After the condition is met the other two elements are supposed to be aggregated into two variable so I can take the average.满足条件后,其他两个元素应该聚合成两个变量,这样我就可以取平均值。


def regional_stats(self):

        [zipcode[0]for zipcode in all_data]:
            if self.zipcode >= 10000 and self.zipcode <= 14999:
                self.NY_sqft = [sqft[1] for sqft in all_data]
                self.NY_avgsqft = statistics.mean(square_footage)
                return self.NY_avg_sqft

I am working with housing data and need the zip codes to stay together with the square footage and bedrooms hence why I cannot simply extract them separately as their own variables.我正在处理住房数据,需要 zip 代码与平方英尺和卧室保持一致,因此我不能简单地将它们作为自己的变量单独提取。 I am trying to aggregate the square footage and number of bedrooms if they are within this range of zip codes如果它们在 zip 代码范围内,我正在尝试汇总卧室的面积和数量

While it's not entirely clear to me what you are trying to do, here is the basic syntax that should get you where you need to be.虽然我并不完全清楚您要做什么,但这里有一些基本语法可以让您到达您需要的地方。

In this case I just printed the average of the tuple that meets the conditions, however you can replace the print() with anything that suits you.在这种情况下,我只打印了满足条件的元组的平均值,但是您可以将 print() 替换为适合您的任何内容。

import numpy as np
x = [(345, 1000, 5),(576, 1345, 8), (234, 1175, 3)]

for t in x:
    if t[0] >= 300 and t[0] <= 750:
        print(np.mean(t))

Output Output

450.0
643.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅迭代元组列表中的第一个元组? - how to iterate ONLY the first tuple in a list of tuples? 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 如何按每个元组中的第一个元素对元组列表进行排序,并选择每个组中最后一个元素最大的元组 - How to sort a list of tuples by the first element in each tuple, and pick the tuple with the largest last element in each group 如何将元组列表转换为 Pandas 数据框,以便每个元组的第一个值代表一列? - How can I transform a list of tuples into a pandas dataframe so that the first value of each tuple represents a column? 如何修改元组列表中元组的每个元素 - How to modify each element of a tuple in a list of tuples 如何将元组列表写入到csv文件中,其中元组的第一个元素是字典? - How can I write a list of tuples to a csv file where the first element of the tuple is a dictionary? 按每个元组的第一个元素对元组列表进行排序-Python 2.7 - Sorting list of tuples by first element of each tuple - Python 2.7 如何遍历元组列表? - How do I iterate through a list of tuples? 如何遍历 2D 列表以获取每个列表中的第一个索引? - How can I iterate through a 2D list to just get the first index within each list? 如何遍历元组列表并检查每个列表的特定顺序 - How to iterate through a list of tuples and checking each list for a specific order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM