Python Forum
New to coding, Using the zip() function to create Diret and getting weird results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to coding, Using the zip() function to create Diret and getting weird results
#1
Bug 
Hey guys im new to coding doing a mature student diploma, im doing my 3rd project for the course and im getting results that don’t make sense. Please can someone tell me why the directory on the second pass is different? If you put this code in Python Tutor you will see that dirct_1 on the second pass starts in 10's when it should start at 1.

What am I trying to do?
Generate a directory containing for eg.. a:1 key and 1 as value, next would be b:2 with 2 as value and so on. So basically it’s a battleship grid reference.

Issue: On the second iteration the value starts at 10 and next run at 20.
Anny and all help welcome...


letters = []


dict_1 = {}
def go():
    for i in range(97, 107):
        letters.append(chr(i))
        numbers = (list(range(1, 3)))
        print(numbers)
        final = []
        i = 0
    for i in (numbers):
        for content in (letters):
            num = content + ":" + str(i)
            alpha_num = num.strip()
            final.append(alpha_num)
            
    values = (list(range(1, 201)))
    dict_1 = dict(zip(final, values))
    
    
    print(f"Final_zip =  {final}")
    print(f"Values zip =  {values}")
    print(f"Dict_1 =  {dict_1}")
go()
go()

Attached Files

Thumbnail(s)
   
Reply
#2
Use the python bbcode (the python button in the message box) to enclose your code.

Unless there's a reason, please just use the letters directly rather than ascii codes.

for letter in "ABCDEFGH":
    ...
Instead of
for i in range(97, 107):
    letter = chr(i)
But the actual problem you're running into is because letters is a global variable and the function is just appending to it. So each time, the list gets longer.

And since the letters repeat, it will add each one to the dict, but only the last one stays.

As letters isn't used outside the function, it shouldn't be there. Put it inside the function and that behavior will disappear.
Reply
#3
Not sure I understand what it is your wanting but, Here is an example of a grid. Can be modified to store x,y in a dict.

from string import ascii_uppercase

letters = [letter for letter in ascii_uppercase[:9]]
numbers = [i+1 for i in range(len(letters))]

print(f"   {'  '.join(letters)}")

for n in numbers:
    dot = f' . '*len(letters)
    print(n, dot, sep=' ', end='\n')
output
Output:
A B C D E F G H I 1 . . . . . . . . . 2 . . . . . . . . . 3 . . . . . . . . . 4 . . . . . . . . . 5 . . . . . . . . . 6 . . . . . . . . . 7 . . . . . . . . . 8 . . . . . . . . . 9 . . . . . . . . .
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Neither letters or dict_1 should be defined outside the function go()

This:
    for i in range(97, 107):
        letters.append(chr(i))
        numbers = (list(range(1, 3)))
        print(numbers)
        final = []
        i = 0
    for i in (numbers):
        for content in (letters):
            num = content + ":" + str(i)
            alpha_num = num.strip()
            final.append(alpha_num)
Can be replaced by:
final = []
for number in (1, 2):  # More consice than range(1, 3)
    for letter in "abcdefghij"  # Easier to understand than range(97, 107)
        final.append(f"{letter}:{number}")
Or even a 1 line comprehension.
Reply
#5
(Apr-08-2023, 10:39 AM)deanhystad Wrote: Neither letters or dict_1 should be defined outside the function go()

This:
    for i in range(97, 107):
        letters.append(chr(i))
        numbers = (list(range(1, 3)))
        print(numbers)
        final = []
        i = 0
    for i in (numbers):
        for content in (letters):
            num = content + ":" + str(i)
            alpha_num = num.strip()
            final.append(alpha_num)
Can be replaced by:
final = []
for number in (1, 2):  # More consice than range(1, 3)
    for letter in "abcdefghij"  # Easier to understand than range(97, 107)
        final.append(f"{letter}:{number}")
Or even a 1 line comprehension.
Reply
#6
Wow guys, thanks so much for all the advice ive been going mad trying to figure this out. But I see now from all you advice that its my logic, im learning and quicker now because of you guys, Thanks a million, heading out for day but when I get back ill have another crack at it. Thanks again everyone.
Reply
#7
(Apr-07-2023, 11:29 PM)Shagamatula Wrote: Hey guys im new to coding doing a mature student diploma, im doing my 3rd project for the course and im getting results that don’t make sense. Please can someone tell me why the directory on the second pass is different? If you put this code in Python Tutor you will see that dirct_1 on the second pass starts in 10's when it should start at 1.

What am I trying to do?
Generate a directory containing for eg.. a:1 key and 1 as value, next would be b:2 with 2 as value and so on. So basically it’s a battleship grid reference.

Issue: On the second iteration the value starts at 10 and next run at 20.
Anny and all help welcome...


letters = []


dict_1 = {}
def go():
    for i in range(97, 107):
        letters.append(chr(i))
        numbers = (list(range(1, 3)))
        print(numbers)
        final = []
        i = 0
    for i in (numbers):
        for content in (letters):
            num = content + ":" + str(i)
            alpha_num = num.strip()
            final.append(alpha_num)
            
    values = (list(range(1, 201)))
    dict_1 = dict(zip(final, values))
    
    
    print(f"Final_zip =  {final}")
    print(f"Values zip =  {values}")
    print(f"Dict_1 =  {dict_1}")
go()
go()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python create function validation mg24 1 849 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 798 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,179 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 958 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Question Pandas : How to create an algorithm that helps me improve results and creating new co Smordy 8 2,271 Apr-10-2022, 10:28 PM
Last Post: Larz60+
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to define a function to create a resorted list? sparkt 6 2,848 Aug-08-2020, 04:10 PM
Last Post: sparkt
  Weird function defaults error? wallgraffiti 5 2,188 Aug-07-2020, 05:55 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,444 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 1,744 Jun-21-2020, 09:40 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020