Python 板


LINE

现在我有一组程式码 from collections import defaultdict import numpy as np s=[11,55,22,22,55,22,55,22,33,33,55,33,44,44,55,55,44,44] d = defaultdict(list) for k,l in [(v,t) for t,v in enumerate(s)]: d[k].append(l) print(d.items()) 其目的是找出重复的项 并且显示在矩阵中的位置 而执行结果会是这样 dict_items([(11, [0]), (55, [1, 4, 6, 10, 14, 15]), (22, [2, 3, 5, 7]), (33, [8, 9, 11]), (44, [12, 13, 16, 17])]) 现在希望说可以把结果抽取出来 像是这样 a(0)=11,b(0)=[0] a(1)=55,b(1)=[1, 4, 6, 10, 14, 15] . . . . 想请问应该要对程式码做些什麽修改 才可以达到所希望的目的? --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 120.105.133.228
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1521373230.A.363.html
1F:推 Panthalassa: 也就是说 a(i) = s[i], b(i) = d[s[i]] ? 03/18 22:21
对唷 之前我有试了一下 这麽做只会显示最後一个的值 有想过用append 不过不确定要怎麽写QQ
2F:推 Panthalassa: 咦,我照着你的 code 打,跑出来是对的耶 03/18 22:37
0.0 我明天再试试看! 说不定是我自己打错之类的XDD 先谢过P大! ※ 编辑: st40182 (114.41.179.173), 03/18/2018 22:54:11
3F:推 Jeffrey11061: S_element = set(s) 03/19 01:02
4F:→ Jeffrey11061: b = {} 03/19 01:02
5F:→ Jeffrey11061: for i in S_element: 03/19 01:02
6F:→ Jeffrey11061: b[i] = [] 03/19 01:02
7F:→ Jeffrey11061: for idx, item in enumerate(s): 03/19 01:02
8F:→ Jeffrey11061: b[i].append(idx) 03/19 01:02
9F:→ Jeffrey11061: # a = s (? 03/19 01:02
10F:→ Jeffrey11061: 不知道这样能不能达到你的目的,差别在位置的list是 03/19 01:02
11F:→ Jeffrey11061: 用value本身当key(ps手机打的,可能会有些问题) 03/19 01:02
12F:→ Jeffrey11061: 啊....indent被吃掉了 不过应该还能阅读 03/19 01:03
13F:推 handsomeLin: 楼上j大 你这样本身跟他用一组dict做出来的事一样 03/19 02:44
14F:→ handsomeLin: 顺便提醒原po 你enumerate那不需要那样子写 03/19 02:45
15F:→ handsomeLin: for i, v in enumerate(s) 就可以拿到index跟val了 03/19 02:46
16F:→ handsomeLin: 不需要一个在list里建tuple再把tuple取出来 太多余 03/19 02:47
17F:→ handsomeLin: a = list(d) b = [] for v in a: b.append(d[v]) 03/19 02:49
18F:推 stucode: 帮楼上 h 大补充,b 可以用 list comprehensions 就好 03/19 05:36
19F:→ stucode: b = [d[v] for v in a] 03/19 05:36
20F:→ stucode: 或是另一种方法: 03/19 05:37
21F:→ stucode: a = list(d) # 等同於 list(d.keys()) 03/19 05:37
22F:→ stucode: b = list(d.values()) 03/19 05:37
23F:推 thethirdfoot: 抱歉请问for k,l in [(v,t) for t,v in e(s)]: 这段 03/19 09:17
24F:→ thethirdfoot: 意思是什麽呢 03/19 09:18
25F:推 handsomeLin: 第二种方法我是想说dict是unorder不知道直接取list会 03/19 11:31
26F:→ handsomeLin: 不会没对应到,不过第一种方法确实是不错的 可以把b 03/19 11:31
27F:→ handsomeLin: 缩成一行解决 03/19 11:31
28F:推 handsomeLin: t大,我上面有补充他那段是可以简化的 03/19 11:33
29F:→ stucode: 如果没有中途修改 dict 的话,dict.keys() 跟 03/19 14:34
30F:→ stucode: dict.values() 顺序会是对应的喔,官方文件有提到。 03/19 14:36
31F:推 thethirdfoot: 看不太懂他(v,t)跟t,v 03/19 16:48
32F:推 handsomeLin: 上了一课了 03/19 18:57







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP