我的查询涉及显示每行中的数据大于前一行,即第2行>第1行,第3行>第2行,在重新启动“迭代”之前,这只会发生到数据有字的点。
数据集如下
853764622753
1055334825425
927168311000
1142876772659
1390557034408
1538194473087
1567178619062
1459597906913
1345383143356
1204616439828
Australia
33050343783
48852482960
44291490421
52902703376
65951627200
69684317719
74164435946
75244166773
53074370486
37847715736
Azerbaijan
我不知道如何处理代码。我知道你需要初始化一个布尔值来设置为True。我的结束代码应该能够处理行并指示它是True还是False 代码增加了。
increased = True
for x in open("dataset"):
y = x.rstrip("\n")
if y.isnumeric():
floated = float(y)
if increased = increased and (floated > floated ) ???CONFUSED
print("GDP increased every year:",increased)
else:
print("GDP increased every year:",increased)
else:
str(y)
答案 0 :(得分:1)
您必须保留上一行的记录并比较这些值。有几种方法可以做到这一点。
一种简单的方法就是这样
previous = None
increased = True
with open('dataset') as f:
for line in f:
line = line.strip()
if not line:
continue # ignore blank lines
if line.isnumeric():
if previous is not None and increased:
# if increased is currently True, see if it is still True
increased = float(line) > float(previous)
previous = line
else:
# Now prints out country name
print('GDP for {} increased every year: {}'.format(line, increased))
increased = True
previous = None
示例输出
GDP for Australia increased every year: False
GDP for Azerbaijan increased every year: False