0

i have to write a program that get the number of time that an item (linked to another list)occours into a list , i write an example to understand: i have 2 lists:

l=[13, 14, 14, 8, 13, 13, 14, 14, 8, 13 ] and l1=['pea', 'rpai', 'rpai', 'schiai', 'pea', 'rpe', 'zoi', 'zoi', 'briai ', 'rpe']

now, every item of l is linked at only and only one item of l1 (so 13--'pea', 14--'rpai' ecc) like a bijective function. i want to create a third list l2 that associate 1 number to all the equal "tuple" (es. 13--'pea' =1, 14--'rpai'=2 ) so if during cycle i met again another equal tuple i link directly the number associated (so if i get again like the 4th element of l1 13--'pea' im gonna link again the number 1)... that's what i created, but it doestn seems to work , anyone can let me understand what s goin wrong? thanks

    for i in lista:       
    if i not in pro:
         
         pro.append(i)
         l2.append(conta) 
         d[i[1]]=conta 
         conta+=1 
    else:
         a=d.get(i[1])
         l2.append(a)
        

now "lista" is the list that contain the tuple of the 2 lists (so be like = [(13, 'pea), (14,'rpai') ecc] pro is an initial empty list and l2 is the final result, d is a dict that i use to create the connection from l element and l1 (d={13:0, 14:1 ...}) , but i cant understand where is the errror, any suggestion=? thanks!

Aryan
  • 1,093
  • 9
  • 22

2 Answers2

1

Does this solve your question?

l = [13, 14, 14, 8, 13, 13, 14, 14, 8, 13]
l1 = ['pea', 'rpai', 'rpai', 'schiai', 'pea',
      'rpe', 'zoi', 'zoi', 'briai ', 'rpe']

final = []
for num, item in zip(l, l1):
    final.append((num, l1.index(item)))

print(final)

Output:

[(13, 0), (14, 1), (14, 1), (8, 3), (13, 0), (13, 5), (14, 6), (14, 6), (8, 8), (13, 5)]
Aryan
  • 1,093
  • 9
  • 22
  • see [zip docs](https://docs.python.org/3.3/library/functions.html#zip) – Raphael Nov 10 '20 at 13:38
  • no because at the end i need a list like [0, 1, 1, 2, 0, 3, 4, 4, 5, 3 ], in fact all this element comes from [13:0, 14:1 , 14:1 ecc] ) – Sesto Roll Nov 10 '20 at 13:49
  • @SestoRoll i have made a edit to the post please check it out and let me know if it solves the question, – Aryan Nov 10 '20 at 13:53
  • seems good but for example 8 must have 2 has index , not 3 , index must be sequenzial, i think i could fix this with an if condition – Sesto Roll Nov 10 '20 at 14:00
  • is it the count you want as value? so (13,3), (14,3), (8, 2),..? – Raphael Nov 10 '20 at 14:01
  • @Raphael no, i want that count incremenet only if i never met this tuple before , so 13,0 14, 1 14, 1 8 , 2 ....8 must have 2 bcs i never met it – Sesto Roll Nov 10 '20 at 14:04
  • sorry, i cannot help much further on this because it is going above my head what to do, sorry! – Aryan Nov 10 '20 at 14:11
  • @Sesto, ah ok. I understand. I answer in a few minutes ;) – Raphael Nov 10 '20 at 14:29
  • @Sesto: Just to be sure. What number to you expect the second time you reach 13? – Raphael Nov 10 '20 at 14:31
  • It should be 0, shouldn't it? And another question: you said those links are bijective but 13 for an instance is first "linked" to "pea" and then to "rpe" – Raphael Nov 10 '20 at 14:54
  • @Raphael into the second time must be 13:4 , also for bijective i meant tuple(13, pea) determinates 0 ecc – Sesto Roll Nov 10 '20 at 18:16
0

I'm still not 100% sure if I understood you correctly, but does this meet your requirements?

numbers = [13, 14, 14, 8, 13, 13, 14, 14, 8, 13]
items = ['pea', 'rpai', 'rpai', 'schiai', 'pea', 'rpe', 'zoi', 'zoi', 'briai ', 'rpe']

# we build a list tuples where the i-th tuple consists of the i-th number and the i-th item
tuples = zip(numbers, items)  # [(13, "pea"), (14, "rpai"), (14, "rpai"), ... ]

# now we associate an index to each of those combinations (=tuples)
index = {}
index_increment = 0
for t in tuples:
    if t not in index:
        index[t] = index_increment
        index_increment += 1

# print the results
for t, i in index.items():
    print(f'{t}: {i}')

output:

(13, 'pea'): 0
(14, 'rpai'): 1
(8, 'schiai'): 2
(13, 'rpe'): 3
(14, 'zoi'): 4
(8, 'briai '): 5
Raphael
  • 1,731
  • 2
  • 7
  • 23