0

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

0

2 Answers 2

1

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)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, How would you do it if I had to alternate between two strings instead of 0 and 1?
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.