You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除列表中不区分大小写的重复项且保留原始顺序与元素大小写?

Solution to Case-Insensitive Exact Match Deduplication (Preserving Original Case & Order)

Let's break down how to solve this problem exactly as you need it. The core requirements are:

  • Only remove duplicates that are exact case-insensitive matches (partial overlaps like "Plastic" and "Polypropylene Plastic" stay)
  • Keep the original case of the first occurrence of each unique element
  • Maintain the original order of elements from the input list

Python Implementation

Here's a straightforward, efficient approach that hits all your requirements:

myList = ["paper", "Plastic", "aluminum", "PAPer", "tin", "glass", "tin", "PAPER", "Polypropylene Plastic"]

# Track lowercase versions of elements we've already seen
seen_lower = set()
deduped_list = []

for item in myList:
    item_lower = item.lower()
    # Only keep the item if we haven't seen its lowercase version before
    if item_lower not in seen_lower:
        seen_lower.add(item_lower)
        deduped_list.append(item)

# Update the original list with the deduped result
myList = deduped_list
print(myList)

How It Works

  • Tracking seen elements: We use a set (seen_lower) to store lowercase versions of elements we've processed. Sets let us check for existence in O(1) time, making this solution efficient even for longer lists.
  • Preserving original case & order: For each item in the original list, we only add it to the result if its lowercase form hasn't been seen yet. This ensures we keep the first occurrence exactly as it was, and maintain the original order of non-duplicate elements.
  • Ignoring partial matches: Since we're checking for exact lowercase matches, elements like "Plastic" (lowercase: plastic) and "Polypropylene Plastic" (lowercase: polypropylene plastic) are treated as unique, so both are retained.

Output

Running the code will give you the exact desired result:

["paper", "Plastic", "aluminum", "tin", "glass", "Polypropylene Plastic"]

内容的提问来源于stack exchange,提问作者Crickets

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:20:47