2

Video names may appear more than once. Example: L = [('abc',10),('def',15),('ghi',10),('abc', 12), ...,('xyz',100)] The function should return ['xyz','abc',...,'def','ghi']

Here is what I have so far (which doesn't work and I can't seem to fix it no matter what I try):

def ranked_games(L):
    sorted_list = [x for x in L.items()]
    sorted_list.sort(key = lambda x:x[1]) #sort by value
    sorted_list.reverse()
    print("Sorted by Value Descending",sorted_list) 

I would appreciate any insight on either a better way to approach this or if you know what's wrong with my code... thank you in advance.

Rachel
  • 21
  • 1

5 Answers5

1

Does this accomplish what you're after?

L = [('abc',10),('def',15),('ghi',10),('abc', 12),('xyz',100)]
print("Sorted by Value Descending",[i[0] for i in sorted(L, key=lambda k: k[1], reverse=True)][:10])
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1

Is this what you're looking for?

L = [('abc',10),('def',15),('ghi',10),('abc', 12),('xyz',100)]

def ranked_games(L):
    sorted_list = sorted(L,key = lambda x:x[1],reverse=True) #sort by value
    print("Sorted by Value Descending",[i[0] for i in sorted_list]) 

ranked_games(L)
Dan
  • 527
  • 4
  • 16
1

For fixing the code above, you can simply try following:

def ranked_games(L):
    L.sort(key = lambda x:x[1], reverse=True) #sort by value
    print("Sorted by Value Descending", [i for i, _ in L[:10]]) # [:10] for slicing 10 sorted values

# calling the function
my_list = [('abc',10),('def',15),('ghi',10),('abc', 12),('xyz',100)]
ranked_games(my_list)
print('Original List: ', my_list)

Result:

Sorted by Value Descending ['xyz', 'def', 'abc', 'abc', 'ghi']
Original List:  [('xyz', 100), ('def', 15), ('abc', 12), ('abc', 10), ('ghi', 10)]

Note that the original list is also sorted since .sort method of list is called. If you do not want the original list to be sorted then you may want to call sorted function and pass in list as a parameter as below:

def ranked_games(L):
    sorted_list = sorted(L, key = lambda x:x[1], reverse=True) #sort by value
    print("Sorted by Value Descending",[i for i, _ in sorted_list[:10]]) # [:10] for slicing 10 sorted values

# calling the function
my_list = [('abc',10),('def',15),('ghi',10),('abc', 12),('xyz',100)]
ranked_games(my_list)
print('Original List: ', my_list)

Result:

Sorted by Value Descending ['xyz', 'def', 'abc', 'abc', 'ghi']
Original List:  [('abc', 10), ('def', 15), ('ghi', 10), ('abc', 12), ('xyz', 100)]

For more about difference between sort and sorted, you can look at relevant question What is the difference between sorted(list) vs list.sort()?.

niraj
  • 17,498
  • 4
  • 33
  • 48
1

I think you are missing the combining part. The 'abc' must be combined before the sorting can be done, otherwise you are not really sorting the right list:

l = [('abc', 10), ('def', 15), ('ghi', 10), ('abc', 12), ('xyz', 100)]
diction = {}
for (i, j) in l:
    if i in diction:
        diction[i] = diction[i] + j
    else:
        diction[i] = j
sorted_list = list(diction.items())
sorted_list = sorted(sorted_list, key=lambda x: x[1], reverse=True)

print(sorted_list[:10])
Nikolay Kosev
  • 124
  • 1
  • 4
0
l=[('abc',10),('def',15),('ghi',10),('abc', 12),('xyz',100)]

sortedlist = sorted(l,key = lambda x:x[1],reverse=True)
d=dict(sortedlist)

print(list(d.keys()))
ravi tanwar
  • 598
  • 5
  • 16
  • While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia Jul 25 '18 at 23:38