Home » Python » python – locating (float) pairs in a list for later use-Exceptionshub

python – locating (float) pairs in a list for later use-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

spyder (4.0.1, python 3.6)

The goal is to locate each [k,o] in list ‘a’, as part of an iteration process, in order to later define properties of some items (i out of 6 items).

import numpy
import numpy as np
from numpy import random

a = [] e = 220
for i in range(7):
t = random.uniform(.2,4.5)
stepSize = (4.5)/6
if i == 0: # 1st in the 0:7 range above
t = t
else:
t = t + stepSize*i
def myFunction(e,t):
k = e*t + stepSize
o = e*t - stepSize
return([k,o])
myFunction(e,t)
a += [myFunction(e,t)] # to store

type(a) is a list; it looks like a 6×2 matrix?
So it’s a list of floats in pairs (for each i) as it should be. Initially i thought the solution could be something like: a[k][i],a[o][i] or a([i,i])

For instance, i tried A.:

myFunction(e,t)
a += [myFunction(e,t)] # to store
setProperty(a[i],[i]) # gives error1

(*where setProperty is based on a toolbox and has a form like item.setProperty([k,o])

error1: ValueError: Unsupported value "[0]" for operation when setting pos in GratingStim -> which means it doesn’t locate/access the 0 position for k and o, but sees 0 (first i) as value.

and B:
myFunction(e,t)
a += [myFunction(e,t)] # to store
setProperty(a[k][i],a[o][i]) # which doesn't have access to k and o.

But the solution should probably be simpler than using nested functions and closures…

last, C:

setProperty(myFunction(k,o))

hoping that it would use k,o automatically; now with error3:

This command failed to be executed because an error occurred while trying to get the file code from Spyder's editor. The error was:
An exception has occurred, use %tb to see the full traceback.
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 768: character maps to <undefined>

I would need some help to improve my understanding in order to find easier/faster the solution (as probably obvious, i’m quite new to programming).

Thanks!

How to&Answers: