小测试 in del.py import datetime cur = datetime.datetime.now() num = 1 a_list = {"a":1, "b":2, "c":3} while num < 100000: if "a" in a_list: pass num += 1 now = datetime.datetime.now() print (now - cur).total_seconds
d = {: , : , : , : , : , : } def is_key_present(x): if x in d: print('Key is present in the dictionary') else: print('Key is not present in the dictionary') is_key_present() is_key_present()
基本思路是,将key对应的value设置为list,将对应的值append进去. 示例: f=open("a1.txt") ha={} for i in f: i=i.strip().split() print(i[0],i[1]) for k in i[1:]: ha.setdefault(i[0],[]).append(k) print("#"*10) for k in ha: print(k,sorted(list(set(ha[k])))) 示例文件和输出如