Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iteration with 2 Variables
#1
Hello, This is my first attempt at writing a program using Python and I'm a bit stuck.
I'd like to write the sequence shown in the comments to an Excel spreadsheet. I can do it for just one variable i but get an error (Value Error: Too many values to unpack (Expected 2) when i try to use a second variable j to create a nested loop.
Can anyone help? (Sorry I know this will seem really basic to some)

Here is the code:

import xlsxwriter

workbook = xlsxwriter.Workbook('MyTest.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0

for i,j in range(0,2),(0,2):

    worksheet.write(row, col, i)
    worksheet.write(row, col+1, j)

    row += 1

workbook.close()
#want output
Output:
#00 #01 #02 #10 #11 #12 #20 #21 #22
Reply
#2
If you want to nest loops, put one inside the other, not on top of the other:

for i in range(0, 3):
    for j in range(0, 3):
        print('#{}{}'.format(i, j))
Also, check out the itertools module. It has very useful generators for things like this: product, combinations, permutations.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for the reply. Your code worked in the DOS window but I'm having no luck getting it to work in Excel. Any ideas?


import xlsxwriter
workbook = xlsxwriter.Workbook('MyTest.xlsx')
worksheet = workbook.add_worksheet()
row = 0
col = 0
for i in range(0,3):
	for j in range(0,3):
		worksheet.write(row, col, i)
		worksheet.write(row, col+1, j)
row +=1
workbook.close()
Desired output in Excel is

00
10
11
12
13
10
11
12
13
etc
Reply
#4
Your desired output has changed, and is unclear. You want 00, and the repeat 10, 11, 12, and 13 forever?

What exactly are you getting in excel with the code from your last post, and how exactly is it not what you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks for the reply.

What I get is:

Row A B
1 2 2

What I'm trying to get is:

Row A B
1 0 0
2 0 1
3 0 2
4 0 3
5 1 0
6 1 1
7 1 2
8 1 3
9 2 0
10 2 1
11 2 2
12 2 3
13 3 0
14 3 1
15 3 2
16 3 3
Reply
#6
You don't change the row value
row +=1 is not indented into the loop
Reply
#7
Thanks for your help Yoriz and Icabod801. I didn't realize python was so indent sensitive. Thanks to your help, Here is the code that works

import xlsxwriter
workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet = workbook.add_worksheet('MySheet')
row = 0
col = 0
for i in range(0,4):
		
	for j in range(0,4):
		worksheet.write(row, col, i)
		worksheet.write(row, col+1, j)
		
		row +=1
		
workbook.close()
Reply
#8
The function itertools.product is very handy for products.
The nested iteration you do, is a product.
If you use the product function, you can remove the nested loop.
The complexity is shifted into the product function and you get i and j.

from itertools import product
import xlsxwriter


workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet = workbook.add_worksheet('MySheet')
col = 0
range4 = range(4)
matrix = product(range4, range4)
for row, (i, j) in enumerate(matrix, start=1):
    worksheet.write(row, col, i)
    worksheet.write(row, col+1, j)


workbook.close()
The enumerate function enumerates any iterable.
The second argument defines the start value of enumeration.
Tuple unpacking is used, to assign i and j.

Regular example of enumeration:
for row, i in enumerate(range(10), start=1):
    print(row, i)
I call it nested tuple unpacking.
iterable = [(1,2), (3,4), (5,6), (7,8)] # 2

for row, (first, second) in enumerate(iterable, start=1):
    print(row, first, second)
First the yielded iterable from for is assigned to row, then the second element is unpacked and assigned to first and second.
If you remove the parenthesis around (first, second), you'll get
Error:
ValueError: not enough values to unpack, (expected 3, got 2)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
Thanks DeaD_Eye. I'll try that out. Much appreciated. Cheers, Mark
Reply


Forum Jump:

User Panel Messages

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