Hey everyone, I’m having trouble. I’m trying to generate a 2D list using 2 arrays, but I want the second array to not repeat on itself.
Anonymous in /c/coding_help
630
report
Hey everyone, I’m having trouble. I’m trying to generate a 2D list using 2 arrays, but I want the second array to not repeat on itself. For example, if I have array1=[1,3,5,7,9,11] and array2=[1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1,11.1] I want to create a 2D list in which every element in array1 has a pair in array2 in the form of:<br><br>[(1,1.1), (1,2.2), (1,3.3), (1,4.4), (1,5.5), (1,6.6), (1,7.7), (1,8.8), (1,9.9), (1,10.1), (1,11.1), (3,3.3), (3,4.4), (3,5.5), (3,6.6), (3,7.7), (3,8.8), (3,9.9), (3,10.1), (3,11.1), (5,5.5), (5,6.6), (5,7.7), (5,8.8), (5,9.9), (5,10.1), (5,11.1), (7,7.7), (7,8.8), (7,9.9), (7,10.1), (7,11.1), (9,9.9), (9,10.1), (9,11.1), (11,11.1)]<br><br>but instead I get:<br><br>[(1,1.1), (1,2.2), (1,3.3), (1,4.4), (1,5.5), (1,6.6), (1,7.7), (1,8.8), (1,9.9), (1,10.1), (1,110.1), (3,1.1), (3,2.2), (3,3.3), (3,4.4), (3,5.5), (3,6.6), (3,7.7), (3,8.8), (3,9.9), (3,10.1), (3,11.1), (5,1.1), (5,2.2), (5,3.3), (5,4.4), (5,5.5), (5,6.6), (5,7.7), (5,8.8), (5,9.9), (5,10.1), (5,11.1), (7,1.1), (7,2.2), (7,3.3), (7,4.4), (7,5.5), (7,6.6), (7,7.7), (7,8.8), (7,9.9), (7,10.1), (7,11.1), (9,1.1), (9,2.2), (9,3.3), (9,4.4), (9,5.5), (9,6.6), (9,7.7), (9,8.8), (9,9.9), (9,10.1), (9,11.1), (11,1.1), (11,2.2), (11,3.3), (11,4.4), (11,5.5), (11,6.6), (11,7.7), (11,8.8), (11,9.9), (11,10.1), (11,11.1)]<br><br>You can see it’s what I want, but it only starts being what I want at 9.9.<br><br>So far I have this code:<br><br>​<br><br>for j in range(start, length1):<br> for i in range(len(array2)-1):<br> if array1[j] <= array2[i] <= 100:<br> lst.append((array1[j], array2[i]))<br><br>​<br><br>In this code, start is a variable I define in the beginning that I control, and I was thinking of making it a while loop that adds 1 to start until I’m done, but I’m having trouble with it. Any help on this?<br><br>(Also, side note, would you guys recommend using a different language than python? I use python because it’s what my school uses, but I want to learn coding in its majority, not just python).
Comments (12) 21527 👁️