0

Im currently on a python and I have the following problem: I have a list with bout a 1000 entries(int) lookk like this):

list = [593, 602, 608, 623, 632, 644, 644, 616, 589, 563, 583, 613, 660, 698, 728, 769, 815, 859, 885, 909, 931, 944, 960, 974, 986, 985, 986, 987, 990, 990, 993, 999, 1008, 1020, 1035, 1051, 1061, 1068]

and so on... i want to check if the first value is smaller than the second value, like this:

 list = [593 < 603, 608 < 623, 632 < 644, ... < ..., ... < ..]

Everytime its smaller i want to add 1 to my total, like this:

total = 0
total += 1

All in all i want the answers in boolean. I want to know how often its true or false like: true: 31 false: 2

Full code:

list = []
with open("data.txt") as data:
    data = [int(line.strip()) for line in data]


def funcAOC2():
    total = 0
    for i in range(0, len(data) - 2, 1):
        value = int(data[i] + data[i + 1] + data[i + 2])
        list.append(value)
    print(list)

Thanks for your time. :)

1
  • 1
    You could at least provide the source of the problem as a link rather than just saying "I have the following problem", you also haven't understood the problem statement. Commented Dec 2, 2021 at 12:44

5 Answers 5

1

look into the range function where you can give a third parameter called step.

list = [1, 2, 4, 3, 5, 6]
count= 0
for x in range(1, len(list), 2):
    if list[x - 1] < list[x]:
        count += 1

print(count)

x will be in this case 0 then 2 then 4

Sign up to request clarification or add additional context in comments.

2 Comments

This will fail miserably if there are an odd number of elements in the list. Also, using list as a variable name is ill-advised
With the regards to the question there was no talk about a dynamic list size. so sure it will have an error for another usecase that isnt being discussed here. And re list - that is the variable used in his question Edited in an odd list size safe version where it starts at index 1 and looks back instead of ahead
0

Given your input list l, you can zip it with the same list, but shifted, to obtain pairs to compare. The first list will miss the last item, and the second list will miss the first item.

for pair in zip(l[:-1], l[1:]):
    print(pair)
 
(593, 602)
(602, 608)
...

Doing so, it is easy to obtain your checks, just using a map function.

ll = list(map(lambda pair: pair[0] < pair[1], zip(l[:-1], l[1:])))
ll  # [True, True, True, True, True, False, False, ...]

sum(ll)  # 31, counts True
len(ll) - sum(ll)  # 6, counts False

1 Comment

The OP's example output suggests that he's interested in pairs of elements rather than all adjacent ones
0

Here are three alternatives all of which are "safe" if the list happens to contain an odd number of elements:

list_ = [1, 2, 4, 3, 5, 6]

print(len([None for x, y in zip(list_[::2], list_[1::2]) if x < y]))

...or...

count = 0
for x, y in zip(list_[::2], list_[1::2]):
    if x < y:
        count += 1
print(count)

...or...

count = 0
for i in range(1, len(list_), 2):
    if list_[i-1] < list_[i]:
        count += 1
print(count)

Comments

0

How about something like this super simple and clean

def count():
the_list = [593, 602, 608, 623, 632, 644, 644, 616, 589, 563, 583, 613, 660, 698, 728, 769, 815, 859, 885, 909, 931, 944, 960, 974, 986, 985, 986, 987,
            990, 990, 993, 999, 1008, 1020, 1035, 1051, 1061, 1068]
prev = None
total = 0
for num in the_list:
    if prev is not None:
        if num > prev:
            total += 1
    prev = num
print(total)

1 Comment

But this is comparing adjacent values not pairs of values as implied by the OP's question and proposed solution. Also, with this approach, just set prev = the_list[0] then for num in the_list[1:] removes the need to test prev during every iteration
0

My personal solution:

list = [593, 602, 608, 623, 632, 644, 644, 616, 589, 563, 583, 613, 660, 698, 728, 769, 815, 859, 885, 909, 931, 944, 960, 974, 986, 985, 986, 987, 990, 990, 993, 999, 1008, 1020, 1035, 1051, 1061, 1068]

for x in range(0, len(list) - 1, 1):
    if list[x] < list[x + 1]:
        total += 1
print(total)

This checks if to first number is smaller then the second, the third is smaller then the third and so on. For example thats what it does for every number visually:

if 593 < 602:
   count +1    
if 608 < 623:
   count +1

and so on...

Thats how it worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.