Reputation: 1
I have been making a python hash function ,and I have this so far.
keystring = [1, 4, 2, 3, 2, 1, 4, 1]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while "1".lower() in keystring:
if "1".lower() in keystring:
bucket1.append(keystring[keystring.index("1") + 1])
print (bucket1)
I have tried running it but it just says nothing ,and quits the program immediately.
Upvotes: 0
Views: 33
Reputation: 1
As @FLAK-ZOSO said you are searching for str
in while
loop.
keystring
items to str
like this:keystring = ["1", "4", "2", "3", "2", "1", "4", "1"]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while "1" in keystring:
if "1" in keystring:
bucket1.append(keystring[keystring.index("1") + 1])
print (bucket1)
while
loop to int
keystring = [1, 4, 2, 3, 2, 1, 4, 1]
bucket1 = []
bucket2 = []
bucket3 = []
bucket4 = []
while 1 in keystring:
if 1 in keystring:
bucket1.append(keystring[keystring.index(1) + 1])
print (bucket1)
I don't know "correct" way to do it because I don' know context of your app but for
loop might be better for this operation. And you are checking same thing in if
statement as while
loop. In while
loop you are searching for 1 and then with if
statement you are checking for 1 again which you already know it's there.
TLDR: You don't need if
statement.
Upvotes: 0
Reputation: 4088
while "1".lower() in keystring:
This is never true or, to better say, True
.
In Python "1"
is a string (str
), which has the .lower
method, which returns a string.
Since keystring
is a list
of int
egers, you will never find a str
in it, and the while
loop will never begin.
Upvotes: 2