Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maintaining list order
#11
You could make use of "max_but_less_than" max_lt function to make the code shorter

from bisect import bisect_left

def max_lt(seq, val):
    # http://code.activestate.com/recipes/415233-getting-minmax-in-a-sequence-greaterless-than-some/#c8
    return seq[bisect_left(seq, val) - 1]

thres = [0,3000,6000]
nested_list = [[3], [4, 400], [3, 4222]]

substituted = [[max_lt(thres, val) for val in seq] for seq in nested_list]

print(substituted)
Output:
[[0], [0, 0], [0, 3000]]
Reply
#12
Interesting! Thanks for that suggestion, going to have look at it!
Reply
#13
Now my clumsy brain has processed it and as a result I refactored your original code a little bit:

comparelist = [0, 3000, 6000] 
elements = [[3], [4, 400], [3,4222]] 
result = []

for element in elements: 
    result.append([])                            # add sublist for every list in elements
        for num in element:                    
            for value in reversed(comparelist):  
                if value < num: 
                    result[-1].append(value)    # append first match and break
                    break 
I assume that 'comparelist' is always sorted and starting from end is required to return closest match (in last case 3000 instead of 0)

However, there is some ambiguity: what should be returned when condition fully or partially not met? (for example comparelist = [4500, 5000, 6000])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#14
@perfringo,

Well pointed out the ambiguity. Eventhough chances that will occur is small, why not take the effort to handle it the way I would want it to.

So the possibilties:
1. Negative values in element lists.
Right now they aren't included in the resultlist.
It should return the first item in the comparelist. So -1000 (element list) should return first in compare list (nearest)

2. Comparelist has values larger than the largest value in elementlist.
It should return the smallest (so nearest) in the comparelist So in your suggestion that would be 4500.

This is what I think:
#comparelist = [4500, 5000, 6000] 
comparelist = [0, 3000, 6000]
elements = [[-1,3], [4, 400], [3,4222]] 
result = []
 
for element in elements:
    result.append([])
    for num in element:
        if all(value > num for value in comparelist):
            result[-1].append(comparelist[0])
        else:
            for value in reversed(comparelist):  
			if num > value:
				result[-1].append(value)
				break

print(result)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,062 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,258 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Order a list with successive permutations based on another list yvrob 3 2,762 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,240 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  list approach due nested order 3Pinter 6 2,739 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  How to count and order numbers in a list rachyioli 2 2,514 Aug-21-2019, 10:51 AM
Last Post: perfringo
  Randomise network while maintaining topology of nodes using edge-swap approach Tom1988 3 4,062 May-25-2017, 10:59 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020