lundi 25 mars 2019

Python: Insert Elements between two Matches to Dictionary

I have two lists; lets call them list1 and list2. In list1 have a questions with answers like this:

   Question1,Answer,Answer, Question2, Answer, Answer, Answer, Question3, Answer (and so one) 

In list2 have a questions (without answers) from list1

I inserted list2 to dict with name slice as arguments and arranged np.zeros as values. So I have something like this in slice:

  Q1 ...... 0
  Q2 ...... 0
  Q3 ...... 0

And i want to insert data from list1 (without questions) to slice in values possition where are now zeros. So I want something like this:

  Q1 ...... A,A
  Q2 ...... A,A,A
  Q3 ...... A
  ...

I have tried the next code but it didn't work... next() inserted just the next index alphabet: like "o" or so...

zero = np.zeros(len(list2))
slice = dict(zip(list2, zero))
for c in list1:
    for m in slice:
        if c == m:
            c = iter(c)
            slice.update({m:next(c)})
print(slice)

There is a problem that I don't know indexes of answers and there is a random number of answers. Like one answer for question one, but three answers for question two and so one... Only thing that I have is that I have that questions without answers in another list

pleaseee help....

Aucun commentaire:

Enregistrer un commentaire