F
F
fantom_ask2020-04-30 17:49:51
Python
fantom_ask, 2020-04-30 17:49:51

How to automatically make all unknown index values ​​in an array equal to 0?

I want to shorten my code

test_list = {
'arr_1':['red','blue','yellow'],
'arr_2':['green','brown','black'],
'arr_3':['red','white','black','green']
}
test_id = {}

for x in range(3):
  for key in test_list:
    try:
      test_id[key]
    except KeyError:
      test_id[key] = 0
  
  print("text-{}  border-{} shadow-{} background-{}".format(
  test_list['arr_3'][min(test_id['arr_3'],len(test_list['arr_3'])-1)],
  test_list['arr_2'][min(test_id['arr_2'],len(test_list['arr_2'])-1)],
  test_list['arr_2'][min(test_id['arr_2']+1,len(test_list['arr_2'])-1)],
  test_list['arr_1'][min(test_id['arr_1'],len(test_list['arr_1'])-1)]
  ))
  
  for key in test_list:
    if key == 'arr_2' or  key == 'arr_3':
      sum = 2
    else :
      sum = 1
    test_id[key] = min(test_id[key]+sum,len(test_list[key])-1)

In the end it will

text-red border-green shadow-brown background-red
text-black, border-black shadow-black background-blue
text-green border-black shadow-black background-yellow

So not to recalculate the test_list array at the beginning for x in range(3)
Is there any way to do this?

I say in advance that the number sum can be arbitrary, since it is not known how many colors will need to be jumped over in each array
and how many colors there will be in general.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-04-30
@o5a

So not to recalculate the test_list array at the beginning of for x in range(3)

For example, these 2 options
test_id = {k:0 for k in test_list}

test_id = dict.fromkeys(test_list, 0)

As for the rest: why use min in print (that we do not go beyond the limits of the list), if this condition is already taken into account in the increment loop (where is sum)?
I even understood the algorithm of the program (although not immediately in all this addiction), but without knowing the initial idea, it is not entirely clear why the increment is made, but at the end it is still limited to the maximum element of the list, because it will degenerate like that. From here it is difficult to say what can be optimized.
But instead of loops with sum and test_id changes, you can set a dictionary of these increments and simply wind up the index on it in each loop
# словарь смещений индекса на каждом цикле
inc = {
'arr_1':1,
'arr_2':2,
'arr_3':2
}

for i in range(5):
    print("text-{}  border-{} shadow-{} background-{}".format(
test_list['arr_3'][min(i*inc['arr_3'], len(test_list['arr_3'])-1)],
test_list['arr_2'][min(i*inc['arr_2'], len(test_list['arr_2'])-1)],
test_list['arr_2'][min(i*inc['arr_2']+1, len(test_list['arr_2'])-1)],
test_list['arr_1'][min(i*inc['arr_1'], len(test_list['arr_1'])-1)]
))

выдаст
text-red  border-green shadow-brown background-red
text-black  border-black shadow-black background-blue
text-green  border-black shadow-black background-yellow
text-green  border-black shadow-black background-yellow
text-green  border-black shadow-black background-yellow

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question