H
H
heshhe2020-05-04 20:56:16
Python
heshhe, 2020-05-04 20:56:16

How to improve the array handler?

There is an array:

[
['Место', 'адрес', 'время', 'событие, 'время', 'событие'], \
['Место', 'адрес', 'время', 'событие, 'время', 'событие', 'событие'], \
['Место', 'адрес', 'время', 'событие', 'событие', 'время', 'событие'], \
['Место', 'адрес', 'время', 'событие', 'событие']
]


At the output you need to get:

Place Место
Address адрес
Time время
Event событие

#следующий список..


If there are two events then:

Place Место
Address адрес
Time время
Event событие
Event событие

#следующий список..


If there are two times then:

Place Место
Address адрес
Time время
Event событие
Time время
Event событие

#следующий список..


.. and so on by analogy

Here's how I implemented it:
#zapros - массив
s=''
    i=0
    for line in zapros:
        s += 'Place '+ line[0] + '\n'+ 'Address   ' + line[1] + '\n' + ' Time ' + line[2] + '\n' + 'Event ' + line[3] + '\n'
        if len(zapros[i]) == 4 :
            s += '\n\n' 
     
        if len(zapros[i]) == 5 :
            s+='Event  '+line[4] +'\n\n'
        
        if len(zapros[i]) == 6 :    
            s+=' Time '+line[4] +'\n'+ 'Event  '+ line[5]+'\n\n' 

        if len(zapros[i]) == 7 :    
            s+=' Time '+line[4] +'\n'+ 'Event  '+ line[5]+'\n' + 'Event  '+ line[6]+'\n\n'

        if len(zapros[i]) == 8 :    
            s+=' Time '+line[4] +'\n'+ 'Event  '+ line[5]+'\n'+' Time '+line[6] +'\n'+ 'Event  '+ line[7]+'\n\n'

        if len(zapros[i]) == 9 :    
            s+=' Time '+line[4] +'\n'+ 'Event  '+ line[5]+'\n'+' Time '+line[6] +'\n'+ 'Event  '+ line[7]+'\n'+ 'Event  '+ line[8]+'\n\n'
        if len(zapros[i]) == 10 :   
            s+=' Time '+line[4] +'\n'+ 'Event  '+ line[5]+'\n'+' Time '+line[6] +'\n'+ 'Event  '+ line[7]+'\n'+' Time '+line[8] + 'Event  '+ line[9]+'\n\n'
            
        i+=1


But, in my example, there are two lists with the same number of variables - 7:
['Place', 'address', 'time', 'event, 'time', 'event', 'event'], ['Place', 'address', 'time', 'event', 'event', 'time', 'event']

The first list will pass as expected and we get the expected result:

Place Место
Address адрес
Time время
Event событие
Time время
Event событие
Event событие


.. in the second option there will be porridge:

Place Место
Address адрес
Time время
Event событие
Time событие
Event время
Event событие


How to fix it?
How to compose the code in such a way that it would not depend on the number of objects? (so as not to write like now, for example, 10 conditions)

Condition:
Place, address, time, event - lines with different number of characters and spaces

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2020-05-05
@heshhe

Given the information available, try the following as an option:

Nested list that we get at the very beginning (test case)

>>> events_data = [
['place1', 'address1', 'time1', 'event1'], ['place1', 'address1', 'time1', 'event2'],
['place2', 'address2', 'time1', 'event1'], ['place2', 'address2', 'time2', 'event1'],
['place2', 'address2', 'time2', 'event2'], ['place2', 'address2', 'time2', 'event3'],
['place3', 'address3', 'time1', 'event1'], ['place3', 'address3', 'time2', 'event1'],
['place3', 'address3', 'time2', 'event2'], ['place3', 'address3', 'time2', 'event3']
]

# Словарь для сортировки данных
>>> from collections import defaultdict
>>> events_dict = defaultdict(dict)
>>> for data in events_data:
          *place, time, event = data
          if time in events_dict[tuple(place)]:
                events_dict[tuple(place)][time].append(event)
          else:
                events_dict[tuple(place)][time] = [event]

# Имеем
>>> pprint.pprint(dict(events_dict))
{('place1', 'address1'): {'time1': ['event1', 'event2']},
 ('place2', 'address2'): {'time1': ['event1'],
                          'time2': ['event1', 'event2', 'event3']},
 ('place3', 'address3'): {'time1': ['event1'],
                          'time2': ['event1', 'event2', 'event3']}}

# Выводим на печать
>>> for place in events_dict:
          print('\nPlace: {}\nAddress: {}'.format(*place))	
          for time, events_in_time in events_dict[place].items():
                print('Time: ', time)
                print(('Event: {}\n' * len(events_in_time)).format(*events_in_time), end='')

output result

Place: place1
Address: address1
Time: time1
Event: event1
Event: event2
Place: place2
Address: address2
Time: time1
Event: event1
Time: time2
Event: event1
Event: event2
Event: event3
Place: place3
Address: address3
Time: time1
Event: event1
Time: time2
Event: event1
Event: event2
Event: event3

A
Alexander, 2020-05-05
@sanya84

Well, if the data is as it is, then so)

data = [
['Место', 'адрес', 'время', 'событие', 'время', 'событие'], \
['Место', 'адрес', 'время', 'событие', 'время', 'событие', 'событие'], \
['Место', 'адрес', 'время', 'событие', 'событие', 'время', 'событие'], \
['Место', 'адрес', 'время', 'событие', 'событие']
]

my_dict = {'Место': 'Place', 'адрес': 'Address', 'время': 'Time', 'событие': 'Event'}

def main():
    for i in range(len(data)):
        print("#следующий список...")
        for j in data[i]:
            print(my_dict[j], j)


if __name__ == '__main__':
    main()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question