How to sort items in a list using two different values?

1

I have a list , which contains several values, each group of values corresponds to an audio of a language, but I am not able to sort using two conditions.

audio_list = [{640: 640, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 640, 'size': 226600396}, {448: 448, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 448, 'size': 159037132}, {384: 384, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 384, 'size': 136516042}, {384: 384, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 384, 'size': 136516044}, {256: 256, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 256, 'size': 91473868}, {192: 192, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 192, 'size': 68952780}, {128: 128, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 128, 'size': 46431692}, {128: 128, 'lang': u'pt-BR', 'language': 'Brazilian', 'lang_code': 'pt-BR', 'bitrate': 128, 'size': 46431692}, {96: 96, 'lang': u'en', 'language': 'English', 'lang_code': 'en', 'bitrate': 96, 'size': 35171148}, {96: 96, 'lang': u'pt-BR', 'language': 'Brazilian', 'lang_code': 'pt-BR', 'bitrate': 96, 'size': 35171148}]

language = ['pt-BR', 'en']

for idx, language_code in enumerate(language):
    max_audio = audio_list[ idx ]
    lang_bitrate = max_audio.get('bitrate')
    lang_name = max_audio.get('language')
    print(lang_name + " - " + str(lang_bitrate))

When I run the above code, I get the following result:

  

English - 640

     

English - 448

But I should get the following result, sorted by the type of audio that comes first in language and bitrate :

  

Brazilian - 128

     

English - 640

How would I get the above result, sorted by% of audio% in% with%, and in order of% with%?

    
asked by anonymous 16.10.2018 / 16:48

1 answer

2

If I understood and / or implemented correctly, the result will be different from what you present, I will sort the list of dictionaries, then just adapt it to your context:

# Ordenando
audio_sorted = sorted(audio_list, key=lambda k: (list(k.keys())[0],\
k['language'], k['bitrate']))

# Apresentando os 3 primeiros resultados:
audio_sorted[:3]
[{96: 96,
  'lang': 'pt-BR',
  'language': 'Brazilian',
  'lang_code': 'pt-BR',
  'bitrate': 96,
  'size': 35171148},
 {96: 96,
  'lang': 'en',
  'language': 'English',
  'lang_code': 'en',
  'bitrate': 96,
  'size': 35171148},
 {128: 128,
  'lang': 'pt-BR',
  'language': 'Brazilian',
  'lang_code': 'pt-BR',
  'bitrate': 128,
  'size': 46431692}]

See working at repl.it .

Note:
You can vary the "Keys" to get the desired result.

    
16.10.2018 / 18:29
What are the advantages of using the box-sizing property on elements? [duplicate] ___ erkimt __ dynamic array allocation within a struct ______ qstntxt ___

statement and code:

  

Make a program that stores movies produced by several directors   and:       - Create and read a vector of 5 directors, each containing name (maximum 20 letters), number of films       and movies. The movie member is a vector, which must be created after reading the number of movies. Each       film is composed of name, year and duration.       - Look for a director by name, showing all the movies he has already produced. Repeat the process       until you enter an empty string.

%pre%

My question is: am I allocating the array correctly?

It does not work when I try to put more than 2 movies in the same director! Thank you.

    
______ azszpr336404 ___
  

My question is: am I allocating the array correctly?

Not at all. The allocation was done based on the %code% size, allocating a %code% vector but the %code% type is %code% :

%pre%

In other words, it has allocated a vector of directors and saved it in a double char pointer, so the types and sizes do not play. If your array is an array of %code% to save strings declared as %code% then the correct allocation would be:

%pre%

Note that I omitted cast because it is not required. I should also add that calling the %code% field would be much brighter than it represents, instead of %code% .

But I can not help saying that this does not answer the statement. The statement clearly states that:

  

Each movie consists of name, year and duration

Then %code% should be a structure:

%pre%

And now this would be the structure used within %code% :

%pre%

The allocation would now be:

%pre%

Now it's not enough to adjust the remaining code for this new movie structure.

    
___