v15 python sort function broken?
#1
Question 
really apologize if this question doesn't belong in this section, but may be someone can help me out here since it may be related more to kodi than python.

I've searched and searched but can't find an answer for this sorting dilemma shown below which works in python 2.7.6 IDLE shell.

my_list = [('462000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_av.m3u8?null='), ('862000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_1_av.m3u8?null='), ('1378000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_2_av.m3u8?null='), ('2074000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_3_av.m3u8?null='), ('61000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_a.m3u8?null=')]

above is my list made of tuples, and I want to sort by first element. the way it should work is...
my_list.sort(key=lambda LSadfloat(L[0])), reverse=True)
or my_list.sort(key=lambda L:float(L[0]), reverse=True)
or sorted(my_list, key=lambda L:float(L[0]), reverse=True)

Regardless of using reverse or substituting float with int, answer is same. My result comes up as 61000 tuple row, and it only shows 61000 tuple under log as list within list 61000,'xxxx' (wiki), nothing else. seriously what am I missing here?
Reply
#2
Works for me:

Sorted returns copy of the list

Code:
Unsorted
[('462000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_av.m3u8?null='),
('862000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_1_av.m3u8?null='),
('1378000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_2_av.m3u8?null='),
('2074000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_3_av.m3u8?null='),
('61000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_a.m3u8?null=')]

reverse=True
[('2074000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_3_av.m3u8?null='),
('1378000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_2_av.m3u8?null='),
('862000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_1_av.m3u8?null='),
('462000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_av.m3u8?null='),
('61000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_a.m3u8?null=')]

reverse=False
[('61000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_a.m3u8?null='),
('462000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_av.m3u8?null='),
('862000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_1_av.m3u8?null='),
('1378000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_2_av.m3u8?null='),
('2074000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_3_av.m3u8?null=')]

Using sorted, reverse=True
[('2074000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_3_av.m3u8?null='),
('1378000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_2_av.m3u8?null='),
('862000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_1_av.m3u8?null='),
('462000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_av.m3u8?null='),
('61000', 'http://xxxxx.net/i/videos/vod/xxxxx/index_0_a.m3u8?null=')]
Reply
#3
Need to see the rest of the code.
Reply
#4
I have no clue whats going on here, in one of my addon it just works the way it suppose to, and in another addon with same exact code it doesn't work.
Reply
#5
(2015-10-07, 01:27)Karnagious Wrote: Need to see the rest of the code.

+1

Why bother responding if you aren't going to provide any useful information?
Reply
#6
Not being rude but there is no other information, for very odd reason it works in one addon and in another one (the above example), it just wont work. It's simple list sorting by first element in tuple.
Reply
#7
(2015-10-07, 20:44)yocoldrain Wrote: Not being rude but there is no other information, for very odd reason it works in one addon and in another one (the above example), it just wont work. It's simple list sorting by first element in tuple.

The sorting is obviously working as it can't just *not* work sometimes.

If you provide an example of the code where you have used the sort then people could tell you why your code doesn't work (as I can guarantee it isn't the sort function that doesn't work).

Don't provide code, you won't get much more help than you already have.
Reply
#8
OK, this is the working code, where sorting is working and print statements show unsorted first and then sorted.

Code:
html2 = make_request(url)
    if html2:
        matchlist2 = re.compile("BANDWIDTH=([0-9]+)[^\n]*\n([^\n]*)\n").findall(str(html2))
        if matchlist2:
            for (size, video) in matchlist2:
                if size:
                    size = int(size)
                else:
                    size = 0
                videos.append( [size, video] )
    else:
        videos.append( [-2, match] )
    print videos
    videos.sort(key=lambda L : L and L[0], reverse=True)
    print videos


and this one isn't working...

Code:
html2 = make_request(url)
    if html2:
        matchlist2 = re.compile("BANDWIDTH=([0-9]+)[^\n]*\n([^\n]*)\n").findall(str(html2))
        if matchlist2:
            for (size, video) in matchlist2:
                if size:
                    size = int(size)
                else:
                    size = 0
                videos.append( [size, video] )
    else:
        videos.append( [-2, match] )
    print videos
    videos.sort(key=lambda L : L and L[0], reverse=True)
    print videos


as you can see they are both the same, I don't see how anything else in entire addon can screw up sorting which is followed by the list. And as far as actual solution, I went with different Idea. But wanted to throw this out here if anyone has seen anything like this. I can print log, if anyone needs them when I get home.
Reply
#9
Both code snippets are the same, or am I missing something subtle?
Reply
#10
Nope, they are exactly same. I dont know whether its some type of cache issue, but one worked, and another one didn't. Its mind boggling, spent entire day to see if there was something subtle.
Reply
#11
(2015-10-07, 21:48)yocoldrain Wrote: Nope, they are exactly same. I dont know whether its some type of cache issue, but one worked, and another one didn't. Its mind boggling, spent entire day to see if there was something subtle.

We are trying to help but simply showing the same code twice saying one isn't working really is a waste of time.

If you can't give anymore context to the code then we can't help you.
Reply
#12
Yea, I completely understand that unless you show us the proof its on BS meter. And with my luck this may very well be working on other machines, and thats exactly what I'm going to do when I get home before I make fool out of my self. Once again, there is nothing different in code, exactly same. and as far as not showing code, my signature contains link to both addon on my repo, and the one which wouldnt work is hotstar and so I changed my code there for different solution.
Reply

Logout Mark Read Team Forum Stats Members Help
python sort function broken?0