I have this dictionary:
dict = {
"A1": [round(f, prec) for vr in ex_vrs for f in vr.pos],
"A2": []
}
I want A2 to be somewhat like "A2": [0,1,0,1, .... ] with a length = len(A1)/3*2
Any ideas how I can do this? Thank you so much
Try the following.
# initialise the dictionary with the A1 entry
dict = {"A1" : [round(f, prec) for vr in ex_vrs for f in vr.pos]}
# Determine length of the alternating list for the A2 entry
mylength = int(len(dict["A1"])/3*2)
# Use mod operator to determine (un)even numbers for the alternating list
dict["A2"] = [i % 2 for i in range(mylength + 1)]
In case you want strings, and not numbers to alternate:
# initialise the dictionary with the A1 entry
dict = {"A1" : [round(f, prec) for vr in ex_vrs for f in vr.pos]}
# Determine length of the alternating list for the A2 entry
mylength = int(len(dict["A1"])/3*2)
# Determine the two strings
string1 = "A"
string2 = "B"
# Use mod operator to determine (un)even numbers for the alternating list
dict["A2"] = [(string1 if i % 2 == 0 else string2) for i in range(mylength + 1)]
Suppose for example your length of A2 is 10 then you can do something like this:
len = 10 # Length of A2
rest = [i%2 for i in range(len) ]
print (rest)
# In case you want some other series of characters like ["a", "b", "a", "b" "a", "b" "a", "b" "a", "b" ]
# then you can use if else condition
rest = ["a" if i%2 == 0 else "b" for i in range(10) ]
print (rest)