1

i am a beginner in python my wish is to make an array of n lines and n rows. I have a dictionnary of words and i need to put them in an array n*n . provisions ={'cake':'20','eggs':'10','tomatoes':'4','potatoes':'2','bread':'4'}

| /  | 1           | 2            |
| 1  | cake        | eggs         |
| 2  | tomatoes    | potatoes     |

that is an example of what i want. we have here an array of 2 lines and 2 rows. i can have a dictionnary of more than 5 elements.That is just for an example.

import string

provisions = {'cake':'20','eggs':'10','tomatoes':'4','potatoes':'2','bread':'3'} 
tab = []

#i can modify the n for having an array of n*n no matter if provisions has more elements

n = 2
j = 0
i = 0

if provisions:
    for k,v in provisions.items():
        while i<n:
            while j<n:
                print(f"{k}")
                tab[[i],[j]] = provisions[i]
                j += 1
            i += 1
Lourna
  • 13
  • 3
  • Where did `"bread"` go? – Selcuk Jan 31 '23 at 10:16
  • @Selcuk i decide of the number of elements that i put in the array; and like "bread" is at the fifth position i don't take it. i want just the n*n elements. And in this case n=2 – Lourna Jan 31 '23 at 10:30

2 Answers2

1

 You can try the code below.Iterate the dict, count current index (m variable)and compute position in the matrix.

import math

provisions = {'cake': '20', 'eggs': '10', 'tomatoes': '4', 'potatoes': '2', 'bread': '3'}

n = 2
# init matrix
tab = [[0 for x in range(n)] for y in range(n)]

# I can modify the n for having an array of n*n no
# matter if provisions has more elements

# index in the dict
m = 0
if provisions:
    for k, v in provisions.items():
        # prevent index overflow
        if m < n * n:
            # compute position in matrix
            tab[math.floor(m / n)][m % n] = k
            m = m + 1

print(tab)



 If you want to determine element for position tab[i][j],you first get the index of this element : i * n + j(the first element in the dict is 0).Unfortunately, there is still no dedicated method to index into keys() / values() of the dictionary, you can find something in this answer.

lin
  • 1,425
  • 1
  • 19
  • 27
  • thank's that works. But how to make it actually output as an array because i'm going to have a list of more than 1000 elements with with an n=36. The goal was to determine which element is for example at position [7,8] just by reading the board. – Lourna Jan 31 '23 at 11:13
  • A bit like the table i put above – Lourna Jan 31 '23 at 11:19
0

if you want more of visibility of the different lists, you can just add that code at the end of the answer above:

for l in tab:
    print(l)

that will give:

['cake' 'eggs']
['tomatoes', 'potatoes']

for facilitate the treatment of the dictionary and having the array that you want, you can take all the keys of your dictionary and stock them in a list:

list = []
if provisions:
    for k, v in provisions.items():
        
        list.append(k)
print(list)

There is a way to put that array (tab) in a csv file.

import math
import xlsxwriter

provisions = {'cake': '20', 'eggs': '10', 'tomatoes': '4', 
'potatoes': '2', 'bread': '3'}
workbook = xlsxwriter.Workbook('tableau.xlsx')
worksheet = workbook.add_worksheet()
n = 2
...
print(tab)
row = 0
col = 0

for module in tab:
    str1 =''.join(module)
    if str1.isupper():
        pass
    else:
        worksheet.write_row(row, col, module)
        row += 1
workbook.close()

And it does the job.But you must to be sure that in choosing n you have enough of data for making n*n.

Lea
  • 23
  • 5
  • I think that if i record all that "list" in an csv file i can have the array that i want, with the numerotation like i hope. Do you have an idea of how i can process to have it please? – Lourna Feb 01 '23 at 09:17
  • I updated my answer @Lourna go and test it – Lea Feb 01 '23 at 23:12
  • thank you very much i test it, it's what i want. – Lourna Feb 01 '23 at 23:46