当前位置: 首页 > news >正文

Python | 为列表中的元素分配唯一值

我们可以给列表中的所有数字分配一个唯一的值,重复时它会保留给它的值。这是一个非常常见的问题,在Web开发中,处理物品id时会遇到。让我们讨论一下解决这个问题的一些方法。

1. 使用enumerate() + 列表解析

# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print("The original list is : " + str(test_list))# using list comprehension + enumerate
# assign unique value to list elements
temp = {i: j for j, i in enumerate(set(test_list))}
res = [temp[i] for i in test_list]# printing result
print("The unique value list is : " + str(res))

输出

The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 3, 0, 1, 2, 3]

2. 使用setdefault() + map() + count()

from itertools import count# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print("The original list is : " + str(test_list))# using setdefault() + map() + count()
# assign unique value to list elements
res = list(map({}.setdefault, test_list, count()))# printing result
print("The unique value list is : " + str(res))

输出

The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 5, 2]

3. 使用in,not in操作符和index

# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print ("The original list is : " + str(test_list))# assign unique value to list elements
x=[]
for i in test_list:if i not in x:x.append(i)
res=[]
for i in test_list:res.append(x.index(i))# printing result
print ("The unique value list is : " + str(res))

输出

The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 3, 2]

4. 使用reduce()

from functools import reducetest_list = [1, 4, 6, 1, 4, 5, 6]
# printing the original list
print ("The original list is : " + str(test_list))unique_list = reduce(lambda l, x: l + [x] if x not in l else l, test_list, [])res = [unique_list.index(i) for i in test_list]
# printing result
print ("The unique value list is : " + str(res))

输出

The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 1, 2, 0, 1, 3, 2]

5. 使用sorted()和bisect_left()

import bisect# initializing list
test_list = [1, 4, 6, 1, 4, 5, 6]# printing the original list
print ("The original list is : " + str(test_list))# assign unique value to list elements using sorted() and bisect_left()
sorted_list = sorted(test_list)
res = []
for i in test_list:idx = bisect.bisect_left(sorted_list, i)res.append(idx)# printing result
print ("The unique value list is : " + str(res))

输出

The original list is : [1, 4, 6, 1, 4, 5, 6]
The unique value list is : [0, 2, 5, 0, 2, 4, 5]

6. 使用numpy

import numpy as nptest_list = [1, 4, 6, 1, 4, 5, 6]
# printing the original list
print("The original list is:", test_list)# convert list to numpy array
arr = np.array(test_list)# get unique values and their indices
unique_arr, unique_indices = np.unique(arr, return_inverse=True)# get indices of unique values for each element in original list
res = unique_indices.tolist()# printing result
print("The unique value list is:", res)

输出

The original list is: [1, 4, 6, 1, 4, 5, 6]
The unique value list is: [0, 1, 3, 0, 1, 2, 3]
http://www.lryc.cn/news/347652.html

相关文章:

  • HTML炫酷的相册
  • C++笔试强训day20
  • 【PHP【实战项目】系统性教学】——使用最精简的代码完成用户的登录与退出
  • Linux下的常用基本指令
  • phpstorm环境配置与应用
  • 【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
  • Hive Aggregation 聚合函数
  • Unity 性能优化之GPU Instancing(五)
  • LeetCode 138. 随机链表的复制
  • 【PC微信小程序点不动处理方法】
  • 量化交易:日内网格交易策略.md
  • Ubuntu 20.04在Anaconda虚拟环境中配置PyQt4
  • charts3D地球--添加航线
  • 变色龙还是树懒:揭示大型语言模型在知识冲突中的行为
  • Android OpenMAX(四)OMX Core
  • 【Linux】轻量级应用服务器如何开放端口 -- 详解
  • git如何查看密码
  • redis脑裂问题
  • 日本率先研发成功6G设备,刺痛了谁?为何日本能率先突破?
  • SpringBoot自动配置源码解析+自定义Spring Boot Starter
  • Kafka 环境配置与使用总结
  • 【算法】滑动窗口——串联所有单词的子串
  • 等保测评安全物理环境测评讲解
  • TensorRT-llm入门
  • TinyXML-2介绍
  • JAVA课程设计
  • 基于SpringBoot+Vue的旅游网站系统
  • http代理ip按流量划算还是个数划算?
  • Banana Pi BPI-F3, 进迭时空K1芯片设计,定位工业级应用,网络通信及工业自动化
  • 安科瑞工业IT产品及解决方案—电源不接地,设备外壳接地【监测系统对地绝缘电阻】