如何判断多列表中哪个包含最多最小值?技术咨询
First, let's clarify the problem because your example seems a bit conflicting with the stated goal! You said you need to determine which of two k-element lists has more occurrences of its own minimum value—but in your sample:
- List
ahas a minimum of 1, which shows up once - List
bhas a minimum of 0, which shows up twice
By that logic, we'd choose b, but you said we should pick a. Maybe there was a typo in your example? Or perhaps you meant to count how many elements in one list are smaller than the corresponding element in the other? Let's cover both scenarios so you can pick what fits your actual need.
Solution 1: Count occurrences of each list's own minimum
This is the direct solution to your stated problem, and sorting isn't needed here at all—sorting would just waste time and rearrange elements unnecessarily. Here's how to do it:
- Find the minimum value in each list.
- Count how many times that minimum appears in its respective list.
- Compare the two counts: the list with the higher count is your answer. Handle ties however you want (e.g., return a tie indicator, pick the first list).
Code Example (Python)
def pick_list_by_self_min_count(a, b): # Get the minimum value for each list min_a = min(a) min_b = min(b) # Count how often each min appears count_a = a.count(min_a) count_b = b.count(min_b) # Compare and return result if count_a > count_b: return "a" elif count_b > count_a: return "b" else: return "tie" # adjust tie handling as needed # Test with your sample (this returns "b"—which aligns with counts, not your example note) k = 5 a = [100, 1, 2, 105, 3] b = [99, 7, 8, 0, 0] print(pick_list_by_self_min_count(a, b)) # Output: "b"
Solution 2: Count element-wise smaller values
If your actual goal was to count how many times elements in a are smaller than the corresponding position in b (and vice versa), sorting won't help here either—you need to compare elements in their original positions. Here's the fix:
- Iterate over both lists in lockstep.
- Count how many times
a[i] < b[i](for lista's win count) andb[i] < a[i](for listb's win count). - Compare the two totals to decide which list has more positions where it holds the smaller value.
Code Example (Python)
def pick_list_by_elementwise_min(a, b): count_a_wins = sum(1 for x, y in zip(a, b) if x < y) count_b_wins = sum(1 for x, y in zip(a, b) if y < x) if count_a_wins > count_b_wins: return "a" elif count_b_wins > count_a_wins: return "b" else: return "tie" # Test with your sample: a wins 2 positions, b wins 3 → returns "b" print(pick_list_by_elementwise_min(a, b)) # Output: "b"
The reason sorting didn't work for you is that sorting rearranges the elements, which breaks any positional context your problem might require. Stick to working with the original list order unless you specifically need sorted comparisons!
If neither of these matches your actual requirement, feel free to clarify the exact logic you need, and I can adjust the solution.
内容的提问来源于stack exchange,提问作者donchuan

