Python - 评估列表列表中的元素
Python - evaulate elements in list of lists
正在尝试学习 Python 并完成此任务,以了解是否有任何国家/地区从 2003 年到 2018 年连续逐年提高(得分更高)。
给出的列表:
lst=[[;2018;2015;2012;2009;2006;2003],[Country1;558;544;538;525;525;527],[Country2;551;548;561;555;547;550],[Country3;545;538;536;534;533;529],[Country4;526;524;554;546;547;542]]
列表比样本长很多。连续同比改善的国家/地区应显示在 table 中。第二个任务是做同样的事情,但针对年同比得分较低的国家/地区。
不允许导入。
我想我需要在 for 循环中做一些 if 搜索,但我真的在这里画了一个空白。我真的无法理解它。
非常感谢任何策略提示或代码示例。
首先,我会使用每年之间的差异,就像这样
lst=[[2018,2015,2012,2009,2006,2003],["Country1",558,544,538,525,525,527],["Country2",551,548,561,555,547,550],["Country3",545,538,536,534,533,529],["Country4",526,524,554,546,547,542]]
for list in lst[1:]:
nums = list[1:]
print([nextnum - num for num, nextnum in zip(nums, nums[1:])])
然后你可以做一个简单的循环检查差异在哪里positive/negative
我不确定你想如何将其表示为 table,但要计算 YoY,你可以这样做:
def yoy(y1, y2):
return (y2 - y1) / y1
然后将其应用到您的列表中,以了解哪些国家/地区逐年进步:
for row in lst[1:]:
# calculate YoY
country_yoys = []
for y1, y2 in zip(row[1:], row[2:]):
gain = yoy(y1, y2)
country_yoys.append(gain)
# Check if each YoY has an improvement
counsecutive_yoy_improv = all(sum(country_yoys[i:i+2]) > 0 for i in range(1, len(country_yoys)+1))
# Print results
if counsecutive_yoy_improv:
print(f"Country \"{row[0]}\" has been doing good!")
else:
print(f"Country \"{row[0]}\" has been doing bad!")
试试这个。
lst = [
['',2018,2015,2012,2009,2006,2003],
["Country1",558,544,538,525,525,524],
["Country2",551,548,561,555,547,550],
["Country3",545,538,536,534,533,529],
["Country4",526,524,554,546,547,542]
]
_, *year_data = lst.pop(0)
countries = lst
def is_decreasing(lst) -> bool:
last_checked, *rest = lst
for year_data in rest:
if year_data < last_checked:
last_checked = year_data
else:
return False
return True
def is_increasing(lst) -> bool:
last_checked, *rest = lst
for year_data in rest:
if year_data > last_checked:
last_checked = year_data
else:
return False
return True
def find_yoy():
increasing_yoy = []
decreasing_yoy = []
for country in countries:
country_name, *country_data = country
assert len(year_data) == len(country_data)
sorted_year_data, sorted_country_data = zip(*sorted(zip(year_data, country_data)))
if is_increasing(sorted_country_data):
increasing_yoy.append(country_name)
elif is_decreasing(sorted_country_data):
decreasing_yoy.append(country_name)
print("countries with increasing yoy:", ', '.join(increasing_yoy), '.')
print("countries with decreasing yoy:", ','.join(decreasing_yoy), '.')
find_yoy()
如果性能很重要,请将 is_decreasing
和 is_increasing
合并为一个函数。
正在尝试学习 Python 并完成此任务,以了解是否有任何国家/地区从 2003 年到 2018 年连续逐年提高(得分更高)。
给出的列表:
lst=[[;2018;2015;2012;2009;2006;2003],[Country1;558;544;538;525;525;527],[Country2;551;548;561;555;547;550],[Country3;545;538;536;534;533;529],[Country4;526;524;554;546;547;542]]
列表比样本长很多。连续同比改善的国家/地区应显示在 table 中。第二个任务是做同样的事情,但针对年同比得分较低的国家/地区。
不允许导入。
我想我需要在 for 循环中做一些 if 搜索,但我真的在这里画了一个空白。我真的无法理解它。
非常感谢任何策略提示或代码示例。
首先,我会使用每年之间的差异,就像这样
lst=[[2018,2015,2012,2009,2006,2003],["Country1",558,544,538,525,525,527],["Country2",551,548,561,555,547,550],["Country3",545,538,536,534,533,529],["Country4",526,524,554,546,547,542]]
for list in lst[1:]:
nums = list[1:]
print([nextnum - num for num, nextnum in zip(nums, nums[1:])])
然后你可以做一个简单的循环检查差异在哪里positive/negative
我不确定你想如何将其表示为 table,但要计算 YoY,你可以这样做:
def yoy(y1, y2):
return (y2 - y1) / y1
然后将其应用到您的列表中,以了解哪些国家/地区逐年进步:
for row in lst[1:]:
# calculate YoY
country_yoys = []
for y1, y2 in zip(row[1:], row[2:]):
gain = yoy(y1, y2)
country_yoys.append(gain)
# Check if each YoY has an improvement
counsecutive_yoy_improv = all(sum(country_yoys[i:i+2]) > 0 for i in range(1, len(country_yoys)+1))
# Print results
if counsecutive_yoy_improv:
print(f"Country \"{row[0]}\" has been doing good!")
else:
print(f"Country \"{row[0]}\" has been doing bad!")
试试这个。
lst = [
['',2018,2015,2012,2009,2006,2003],
["Country1",558,544,538,525,525,524],
["Country2",551,548,561,555,547,550],
["Country3",545,538,536,534,533,529],
["Country4",526,524,554,546,547,542]
]
_, *year_data = lst.pop(0)
countries = lst
def is_decreasing(lst) -> bool:
last_checked, *rest = lst
for year_data in rest:
if year_data < last_checked:
last_checked = year_data
else:
return False
return True
def is_increasing(lst) -> bool:
last_checked, *rest = lst
for year_data in rest:
if year_data > last_checked:
last_checked = year_data
else:
return False
return True
def find_yoy():
increasing_yoy = []
decreasing_yoy = []
for country in countries:
country_name, *country_data = country
assert len(year_data) == len(country_data)
sorted_year_data, sorted_country_data = zip(*sorted(zip(year_data, country_data)))
if is_increasing(sorted_country_data):
increasing_yoy.append(country_name)
elif is_decreasing(sorted_country_data):
decreasing_yoy.append(country_name)
print("countries with increasing yoy:", ', '.join(increasing_yoy), '.')
print("countries with decreasing yoy:", ','.join(decreasing_yoy), '.')
find_yoy()
如果性能很重要,请将 is_decreasing
和 is_increasing
合并为一个函数。