#π Need help with a list
178 messages Β· Page 1 of 1 (latest)
@tidal knot
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.
!code
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
ok, there are a number of problems
I agree
π
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.
maybe if zibintai[i] for starters
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.
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?
i == 0
else:
i == 1?
||i == 0 or i == 1||
alternatively, ||use the in operator with a sequence, i in [0, 1]||
what does that do ?
do you have the concept of truthy values down yet? basically any non-zero number and non-empty string is inherently True
x in y checks, like it reads, whether x is present in y or not.
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
not completely
oh so basically the same, but more efficient
And how do you fix the first problem ?
Do you use list[i] and then like list[i-1] ?
!d enumerate
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...
yes, but the i needs to be an index of the list. if i is an element of the list instead, it doesn't make sense to say list[i]
do you understand the index vs element distinction I'm making?
yes
And how to fix that ?
^
To not make it like static task that would break if you change parameters
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
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))
!e
lst = ["hello", "world", "!"]
for i in range(len(lst)):
print(i, lst[i])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0 hello
002 | 1 world
003 | 2 !
does this example help?
Oh you use in instead of my first line
I'm not sure what you mean by that?
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
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"
though will have to use "in" later
Though I get out of range now
Like you said will happen
yep. you need to decide what you want to do for those cases (first and last)
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
if by even you mean "divisible by 2", then in English we call them even and odd (not uneven)
Oh yeah that
I'll try doing it myself real quick
Ah I still get list index out of range
show me
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")
you're still trying to access zibintai[i-1] and zibintai[i+1]
How to do it differently ?
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
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
think a bit, you'll get it
not quite
list[-1] will give you the last element, but how do you detect whether the loop is at the last element?
if i = list[-1] ?
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
using len ?
you're on the right track
So like if the index is equal to the range of list ?
show me the code you'd write for that
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
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
you're still trying to access i-1 and i+1 in all cases, see
^
think about the logical flow of your code.
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
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
you can't have two elses on the same if, right
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")
there's only one place where you do it... yeah, on line 7
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
changed what to i?
0
exactly
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
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
they're both logical operators, they operate on logical expressions
so and checks if both statements are true
yes
so me using 2 ands is wrong too right ?
zibintai[i - 1] == 1 and zibintai[i + 1] == 1
yep
what's the code at this point
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
nice
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 ?
I'm not sure what you mean
just wrap the existing thing in another loop that is something along the lines of for k in range(2, 6)
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 π
np
!close
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.