鱼C论坛

 找回密码
 立即注册
查看: 1891|回复: 4

[已解决]求助,在一个list里面,怎么去除有相同的元素的成分

[复制链接]
发表于 2016-10-29 11:46:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 thyx 于 2016-10-29 12:40 编辑

我想比如一个list['t1','h1','t2','y5','y6','f6','t7'],只要有相同的数字就可以去除掉,不用管字母是什么,所以结果就应该是['t2','y5','t7'],应该怎么写,求助啊
最佳答案
2016-10-31 22:46:33
  1. >>> list1 = ['t1','h1','t2','y5','y6','f6','t7','j9','F4','88','I3','-5']
  2. >>> list2 = [d for d in list1 for c in [b for b in [(list(a) [1])for a in list1] if [(list(a) [1])for a in list1].count(b) == 1] if (list(d)[1]) == c]
  3. >>> list2
  4. ['t2', 't7', 'j9', 'F4', '88', 'I3']
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-10-29 15:40:48 | 显示全部楼层
  1. list1 = ['t1','h1','t2','y5','y6','f6','t7']
  2. list2 = []
  3. for m in range(len(list1)):
  4.     for n in range(len(list1)):
  5.         if m != n:
  6.                 if list1[n][1] == list1[m][1]:
  7.                     list2.append(list1[m])
  8.                     list2.append(list1[n])
  9. list3 = list(set(list2))
  10. list4 = list3+list1
  11. list5 = [x for x in list4 if list4.count(x) ==1]
  12. print(list5)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-29 19:14:33 | 显示全部楼层
  1. >>> ls = ['t1','h1','t2','y5','y6','f6','t7']
  2. >>> cntr = {}
  3. >>> for x in ls:
  4.         cntr.setdefault(x[-1],[])
  5.         cntr[x[-1]].append(x)       

  6. >>> [cntr[x][0] for x in cntr if len(cntr[x])==1]
  7. ['t7', 't2', 'y5']
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-31 17:07:12 | 显示全部楼层
filter
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-31 22:46:33 | 显示全部楼层    本楼为最佳答案   
  1. >>> list1 = ['t1','h1','t2','y5','y6','f6','t7','j9','F4','88','I3','-5']
  2. >>> list2 = [d for d in list1 for c in [b for b in [(list(a) [1])for a in list1] if [(list(a) [1])for a in list1].count(b) == 1] if (list(d)[1]) == c]
  3. >>> list2
  4. ['t2', 't7', 'j9', 'F4', '88', 'I3']
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 14:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表