#πŸ”’ Need help with a list

178 messages Β· Page 1 of 1 (latest)

tidal knot
#

I want to check if the neighbours of the number are equal to 1, else if 1 neighbour was equal to 1 and another to 0.

signal mesaBOT
#

@tidal knot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tidal knot
#

`py

#

How do I write code here ? πŸ˜„

#

I forgot again

iron flume
#

!code

signal mesaBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

tidal knot
#

thx

#
ith open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in zibintai:
        if i == 0 or 1 and (i-1) and (i+1) == 1:
            print("0")
        else:
            print("1")
#

Current code

#

Data file

6 2
0 1 0 1 1 1
iron flume
#

ok, there are a number of problems

tidal knot
#

I agree
πŸ˜„

iron flume
#

first, i in your loop is each element of the list β€” not the index. so i-1 or i+1 is just the current element minus or plus one, not the previous or next element.

tidal knot
#

maybe if zibintai[i] for starters

iron flume
#

second, if i == 0 or 1 does not work like you think. Python's or is a logical or condition, not the English "or" used to separate elements in a list.
this means that both sides of or are separate, independent expressions

#

so i == 0 or 1 is always True, because 1 is always True.

tidal knot
#

Ahh

#

How do I go around that

iron flume
#

since they are, as I said, separate and independent expressions, you need to think of them as such.

#

have a go: how would you write i == 0 or 1 correctly?

tidal knot
#

i == 0
else:
i == 1?

iron flume
#

||i == 0 or i == 1||

tidal knot
#

oh πŸ˜„

#

I was thinking entirely wrong then

iron flume
#

alternatively, ||use the in operator with a sequence, i in [0, 1]||

tidal knot
#

what does that do ?

rare hull
iron flume
#

so i in [0, 1] is the same as writing i == 0 or i == 1 except more convenient when you have multiple things to check against

tidal knot
#

And how do you fix the first problem ?

#

Do you use list[i] and then like list[i-1] ?

iron flume
#

!d enumerate

signal mesaBOT
#

enumerate(iterable, start=0)```
Return an enumerate object. *iterable* must be a sequence, an [iterator](https://docs.python.org/3/glossary.html#term-iterator), or some other object which supports iteration. The [`__next__()`](https://docs.python.org/3/library/stdtypes.html#iterator.__next__) method of the iterator returned by [`enumerate()`](https://docs.python.org/3/library/functions.html#enumerate) returns a tuple containing a count (from *start* which defaults to 0\) and the values obtained from iterating over *iterable*.

```py
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
```  Equivalent to...
iron flume
#

do you understand the index vs element distinction I'm making?

tidal knot
#

index is like position in the list ?

#

for example list[0] is 0 in [0 1 0 1 1 1]

iron flume
#

yes

tidal knot
#

And how to fix that ?

iron flume
tidal knot
#

To not make it like static task that would break if you change parameters

iron flume
#

or, since you're a beginner, I'd say just iterate over range(len(lst)) instead

#

although then you need to be mindful of the fact that the first element has no previous, and the last element has no next, element

tidal knot
#

If none of the 2 rules apply, then the number will change according to "k"

#

I'm lost on how to write it though using range(len(lst))

iron flume
#

!e

lst = ["hello", "world", "!"]
for i in range(len(lst)):
    print(i, lst[i])
signal mesaBOT
iron flume
#

does this example help?

tidal knot
#

Oh you use in instead of my first line

iron flume
#

I'm not sure what you mean by that?

tidal knot
#

Goes instead of for i in list:

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i-1] and zibintai[i+1] == 1:
            print("0")
        else:
            print("1")
#

so that's the current code

iron flume
#

yes. if I had done for i in lst, then i would have taken on the values of each element of the list in turn, i.e. i = "hello" then i = "world" finally i == "1"

tidal knot
#

though will have to use "in" later

#

Though I get out of range now

#

Like you said will happen

iron flume
#

yep. you need to decide what you want to do for those cases (first and last)

tidal knot
#

Cause what I need to do is if none of the 2 rules apply you check if number k is even or uneven and change number according to that

#

I'm not sure if even and uneven is correct term in english

iron flume
#

if by even you mean "divisible by 2", then in English we call them even and odd (not uneven)

tidal knot
#

Oh yeah that

#

I'll try doing it myself real quick

#

Ah I still get list index out of range

iron flume
#

show me

tidal knot
#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i-1] and zibintai[i+1] == 1:
            print("0")
        else:
            if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i-1] == 0 and zibintai[i+1] == 1 or zibintai[i-1] ==1 and zibintai[i+1] == 0:
                print("1")
            else:
                if k % 2 == 0:
                    print("11")
                else:
                    print("00")
iron flume
#

you're still trying to access zibintai[i-1] and zibintai[i+1]

tidal knot
#

How to do it differently ?

iron flume
#

you need to detect if you're at the start or end, and in those cases not even try to access those out-of-range indices

tidal knot
#

How to check for start and end ?

#

For start if it's [0] probably

#

But for end ?

#

I think i read about it one time

iron flume
#

think a bit, you'll get it

tidal knot
#

But don't remember

#

Something with -

#

Ah list[-1]

iron flume
#

not quite

#

list[-1] will give you the last element, but how do you detect whether the loop is at the last element?

tidal knot
#

if i = list[-1] ?

iron flume
#

you're comparing an index i to an element list[-1]

#

so no

#

just think about what values i takes on in these cases we want to detect

tidal knot
#

using len ?

iron flume
tidal knot
#

So like if the index is equal to the range of list ?

iron flume
#

show me the code you'd write for that

tidal knot
iron flume
#

almost correct

#

the thing is that lists are zero-indexed, as you know, so the highest value i will go up to is actually len-1, not len

tidal knot
#

oh so add -1 to the end

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i-1] and zibintai[i+1] == 1:
            print("0")
        else:
            if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i-1] == 0 and zibintai[i+1] == 1 or zibintai[i-1] ==1 and zibintai[i+1] == 0:
                print("1")
            else:
                if i == len(zibintai) - 1:
                    if k % 2 == 0:
                        print("11")
                    else:
                        print("00")
#

It still picks on this

#

Did I place it in a wrong place

iron flume
#

you're still trying to access i-1 and i+1 in all cases, see

iron flume
#

think about the logical flow of your code.

tidal knot
#

Should be the first thing

#

I'll try making it

flint bobcat
#

it maybe beneficial at this point to not try and account for everything in one line and break it up so you can see whats happening

tidal knot
#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if i == zibintai[0] or i == len(zibintai) - 1:
            if k % 2 == 0:
                print("11")
            else:
                print("00")
            else:
                if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] and zibintai[i + 1] == 1:
                    print("0")
                else:
                    if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 0 and zibintai[i + 1] == 1 or zibintai[i - 1] == 1 and zibintai[i + 1] == 0:
                        print("1")

now my it says my syntax broke after else: that comes after print("00")

#

but it does print out 2 11 so the first part works

iron flume
#

you can't have two elses on the same if, right

tidal knot
#

Oh

#

I think I know

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if i == zibintai[0] or i == len(zibintai) - 1:
            if k % 2 == 0:
                print("11")
            else:
                print("00")
        else:
            if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] and zibintai[i + 1] == 1:
                print("0")
            else:
                if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 0 and zibintai[i + 1] == 1 or zibintai[i - 1] == 1 and zibintai[i + 1] == 0:
                    print("1")
iron flume
#

i == zibintai[0] is incorrect

#

you're, again, comparing an index to an element

tidal knot
#

in line 7 ?

#

or in all program

iron flume
#

there's only one place where you do it... yeah, on line 7

tidal knot
#

oh right u specified 0 not i

#

I changed it to "i" and it works well, but is that the correct way ?

#

oh nvm

#

it's not

tidal knot
#

Ignore that πŸ˜„

#

But like how do I check if it's the first number then

iron flume
#

think

#

what value does i have at the start?

tidal knot
#

0

iron flume
#

exactly

tidal knot
#

So just if i = 0 ?

#

I don't get it

iron flume
#

yep

#

that's it

tidal knot
#

But now I get 5 numbers somehow instead of 6

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if i == 0 or i == len(zibintai) - 1:
            if k % 2 == 0:
                print("11")
            else:
                print("00")
        else:
            if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] and zibintai[i + 1] == 1:
                print("0")
            else:
                if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 0 and zibintai[i + 1] == 1 or zibintai[i - 1] == 1 and zibintai[i + 1] == 0:
                    print("1")
#

should be 11, 1, 0, 1, 0, 11

iron flume
#

zibintai[i - 1] and zibintai[i + 1] == 1 this doesn't work like you think for the same reasons we talked about before, i.e. both sides are independent and separate expressions

tidal knot
#

oh

#

it's the same for and

iron flume
#

they're both logical operators, they operate on logical expressions

tidal knot
#

so and checks if both statements are true

iron flume
#

yes

tidal knot
#

so me using 2 ands is wrong too right ?

iron flume
#

nope

#

x and y and z just checks if all three expressions are true

tidal knot
#

zibintai[i - 1] == 1 and zibintai[i + 1] == 1

iron flume
#

yep

tidal knot
#

That still doesn't fix it for some reason

#

Oh

iron flume
#

what's the code at this point

tidal knot
#

fixed it

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for i in range(len(zibintai)):
        #print(i, zibintai[i])
        if i == 0 or i == len(zibintai) - 1:
            if k % 2 == 0:
                print("11")
            else:
                print("00")
        else:
            if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 1 and zibintai[i + 1] == 1:
                print("0")
            else:
                if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 0 and zibintai[i + 1] == 1 or zibintai[i - 1] == 1 and zibintai[i + 1] == 0:
                    print("1")
                else:
                    if k % 2 == 0:
                        print("11")
                    else:
                        print("00")
#

basically

#

added else:
if k % 2 == 0:
print("11")
else:
print("00")

#

to the end

#

cause when it's 0,1,0

#

both rules don't apply also

#

since neighbous are neither 1 and 1 or 0/1 1/0

iron flume
#

nice

tidal knot
#

i'll try different numbers now to see if it works

#

How could I make it loop that if I change "k" to 5, it makes 5 different answers like with k changing ?

iron flume
#

I'm not sure what you mean

tidal knot
#

so like for first one it takes k of 2

#

then k of 3

#

up to 5

iron flume
#

just wrap the existing thing in another loop that is something along the lines of for k in range(2, 6)

tidal knot
#

Oh I should also maybe append them to a list

#

How do you get 5 different lists in that order like in picture ?

#
with open("zibintaid.txt", "r") as file:
    n, k = map(int, file.readline().split())
    z = file.readline()
    zibintai = [int(i) for i in z.split()]
    for k in range(1, 5):
        for i in range(len(zibintai)):
            #print(i, zibintai[i])
            if i == 0 or i == len(zibintai) - 1:
                if k % 2 == 0:
                    print("11")
                else:
                    print("00")
            else:
                if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 1 and zibintai[i + 1] == 1:
                    print("0")
                else:
                    if zibintai[i] == 0 or zibintai[i] == 1 and zibintai[i - 1] == 0 and zibintai[i + 1] == 1 or zibintai[i - 1] == 1 and zibintai[i + 1] == 0:
                        print("1")
                    else:
                        if k % 2 == 0:
                            print("11")
                        else:
                            print("00")
#

I wrapped the thing in a loop

#

though if I append a number instead of print, i will get a long list

#

I figured it out nvm

#

Thanks for explaining all of this to me πŸ˜„

iron flume
#

np

tidal knot
#

🫑

#

.close

tight tigerBOT
#
Did you mean:
tidal knot
#

!close

signal mesaBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.