Python for循环不能正确迭代第二个条件

时间:2018-05-16 20:22:00

标签: python list for-loop while-loop tuples

我有一长串的元组列表,其格式为

num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)]

每个元组的第一个元素对应一个ID,第二个元素对应于事件发生的那一天,第三个元素对应于事件的类别,第四个元素对应于事件的值。

任务是打印出前5天的最大值,例如在第6天,只考虑第1天到第5天的事件,每个单独的ID,即基本形式的东西

Day   ID    Max
7     A1    400
7     A2    350
8     A1    750

目前,在声明num_list之后,我有以下代码,其中ID是所有ID值的集合。使用14和18作为日期直到第19天

first_value = 1
fifth_value = 5

for id in ID:
    while first_value <= 14 and fifth_value <= 18:
        result = max([i for i in num_list if i[1] <= fifth_value and i[1] >= first_value and i[0] == id], key = lambda  x:x[3])
        first_value += 1
        fifth_value += 1
        print(f"result[0]} {result[1]} {result[3]}")   

问题是这只返回第一个ID的最大结果,在这种情况下是A1。这是正确的,但我不确定为什么它没有为每个ID做这件事。我已经检查过,在while循环之前它会返回每个ID,所以我不确定这里的问题是什么

先谢谢,对不起,如果以前发过类似的内容,但我找不到它

3 个答案:

答案 0 :(得分:0)

通过您的示例,我可以轻松访问任何范围的元组:

#!/usr/bin/python

num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)]

print [item for item in num_list if item[1] < 19 ]
  

<强>输出:

mortiz@alberta:~/Documents/projects/python$ python tuples.py 
[('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A4', 14, 'CC', 109.8)]
  

如果您想要一个范围,请使用range()更改它:

print [item for item in num_list if item[1] in range(5,19) ]
  

<强>输出:

mortiz@alberta:~/Documents/projects/python$ python tuples.py 
[('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A4', 14, 'CC', 109.8)]
  

最后,所选范围的最高值:

#!/usr/bin/python

num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)]
result=[item for item in num_list if item[1] in range(5,19)]
highest=[item[3] for item in result]

print max(highest)
  

输出(最高值)

mortiz@alberta:~/Documents/projects/python$ python tuples.py
386.42

这就是你想要的吗?

答案 1 :(得分:0)

首先,此行中result[0]缺少左大括号 print(f"result[0]} {result[1]} {result[3]}") 但是你的主要问题是在你的while循环中,first_valuefifth_value继续增加而while循环运行第一个id,但是它们永远不会被重置,所以对于每个后续的id你永远不会输入while循环。

答案 2 :(得分:0)

我不确定你之后会怎样,但这是我的2美分。请注意,id是Python中的保留变量名称。 (因此使用id_

#Let's say we have the data shown in the `num_list`.

num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80), ('A7', 6, 'DD', 243.12), ('A3', 11, 'GG', 612.21)]

#First, we want to sort it by the `day` of occurence. Then we want to list all the possible IDs.

sorted_list = sorted(num_list, key=lambda x:x[1])
ID = sorted(list(set(x[0] for x in num_list)))

# Then, let's define a function, which will get the entry/row with the maximum value
# We give the function two arguments. The `day`, which is the day of observation.
#    If the day of observation is 6, then, only look at the entries which have day of the event 1-5
# The another argument is the id_ we are looking at ('A1', 'A4', ..etc.)

def get_max_within_time_window(day, id_):

    # Get all the entries with the wanted id
    entries_id = [x for x in sorted_list if x[0] == id_]

    # Get all the entries within the 5-day time window. The day of observation not included.
    entries_filtered = [x for x in entries_id if 0 < day-x[1] <= 5]

    # In case of zero matches, return empty list
    if not entries_filtered:
        return []

    # Return the event with highest `value`
    return max(entries_filtered, key=lambda x:x[3])

# Now, let's put this in action.
# Day*: The day of observation
# Day: The day of event occurence. If day of observation is 6, then day of occurence can be 1, 2, 3, 4 or 5.
print('Day*\tDay\tID\tMax')
for day in range(20):
    for id_ in ID:
        found = get_max_within_time_window(day, id_)
        if not found:
            continue
        max_val = found[3]
        day_occured = found[1]
        print(f'{day}\t{day_occured}\t{id_}\t{max_val}')

结果

Day*    Day     ID      Max
5       4       A1      977.98
6       4       A1      977.98
7       4       A1      977.98
7       6       A7      243.12
8       4       A1      977.98
8       6       A7      243.12
9       4       A1      977.98
9       6       A7      243.12
10      6       A7      243.12
11      6       A7      243.12
12      11      A3      612.21
13      11      A3      612.21
14      11      A3      612.21
15      14      A1      386.42
15      11      A3      612.21
15      14      A4      249.12
16      14      A1      386.42
16      11      A3      612.21
16      14      A4      249.12
17      14      A1      386.42
17      14      A4      249.12
18      14      A1      386.42
18      14      A4      249.12
19      14      A1      386.42
19      14      A4      249.12