我有这本字典:
dict = {
"A1": [round(f,prec) for vr in ex_vrs for f in vr.pos],"A2": []
}
我希望A2
类似于“ A2”:[0,1,1,....],长度= len(A1)/ 3 * 2
有什么想法可以做到这一点吗?非常感谢
我有这本字典:
dict = {
"A1": [round(f,prec) for vr in ex_vrs for f in vr.pos],"A2": []
}
我希望A2
类似于“ A2”:[0,1,1,....],长度= len(A1)/ 3 * 2
有什么想法可以做到这一点吗?非常感谢
尝试以下方法。
# 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)]
如果您希望使用字符串而不是数字来代替:
# 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)]
,
例如,假设您的A2长度为10,那么您可以执行以下操作:
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" ]
# then you can use if else condition
rest = ["a" if i%2 == 0 else "b" for i in range(10) ]
print (rest)