dn PyTutor at danceswithmice.info
Fri Jun 26 05:33:19 EDT 2020

```On 26/06/20 3:46 PM, SUMAN KANTI ROY wrote:
> a=[1,2,3,4,5,6,7,8,9,10]
> arr=[[1,2],[2,23],[4,5]]
> dic={}
> for i in range(0,len(arr)):
>      if arr[i][0] not in dic:
>          dic[arr[i][0]]=arr[i][1]
> arr1=[]
> def find_key(val,dic):
>      for i in dic.keys():
>          if dic[i]==val:
>              return i
> new_arr=sorted(dic.values(),reverse=True)
> ans= [[0 for i in range(3)] for i in range(len(new_arr))]
> for i in range(len(new_arr)):
>      ans[i][0]=a[i]
>      ans[i][1]=find_key(new_arr[i],dic)
>      ans[i][2]=new_arr[i]
> print(ans)
>
> 0/p=====[[1, 2, 23], [2, 4, 5], [3, 1, 2]]
> I want to make it 3 column matrix ,,,with column heading
> name['device',R.B.N','CQI']

As @Alan has said, you are trying to write Java/C/? in Python. The
Python language has its own idioms and techniques. I'm also going to
criticise the choice of variable_names - and Python uses "lists" not
"arrays"!

Here is some food for thought (and there are other ways of achieving the
same):

- because the array is a list of 2-member lists you can use Python's
dynamic conversions:

>>> arr=[[1,2],[2,23],[4,5]]
>>> dic = dict( arr )
>>> dic
{1: 2, 2: 23, 4: 5}

- each element of a dictionary consists of a key and a value. They can
be considered separately:

>>> keys = dic.keys()
>>> keys
dict_keys([1, 2, 4])
>>> values = dic.values()

- two lists can be joined - except that the basic result is an iterator:

>>> inverted_arr = list( zip( dic.values(), dic.keys() ) )
>>> inverted_arr
[(2, 1), (23, 2), (5, 4)]

- and just to complete the picture, we do the list-to-dict 'trick' again:

>>> inverted_dic = dict( inverted_arr )
>>> inverted_dic
{2: 1, 23: 2, 5: 4}

- which has generated all of the constructs needed!

I'll leave it to you to decide if you need the inverted dict. Research
the Python documentation (it's very good!), and note that zip() allows
multiple lists to be zip-ped together, providing...

Can you now solve the problem without using any for-loops?
--
Regards =dn
```