假设我有一个这样的字典
origin_dict={0:[],1:[],2:['bus'],3:['bus'],4:['bus'],5:[],6:[],7:['train'],8:['train'],9:['train'],10:[],11:[],12:['train'],13:['train'],14:[]}
我想用相同的值对它们进行分组,但只有当它们连续时。
new_dict={0:{2:'bus',3:'bus',4:'bus'},1:{7:'train',8:'train',9:'train'},2:{12:'train',13:'train'}}
任何人都有任何想法?
答案 0 :(得分:1)
from collections import defaultdict
from operator import itemgetter
from itertools import groupby
origin_dict = {0: [], 1: [], 2: ['bus'], 3: ['bus'], 4: ['bus'], 5: [], 6: [], 7: [
'train'], 8: ['train'], 9: ['train'], 10: [], 11: [], 12: ['train'], 13: ['train'], 14: []}
result = defaultdict(list)
for k, v in origin_dict.iteritems():
key = "".join(sorted(v))
if key != "":
result[key].append(k)
solution = defaultdict(dict)
index = 0
for k, data in result.iteritems():
for k1, g in groupby(enumerate(data), lambda (i, x): i - x):
for v2 in map(itemgetter(1), g):
solution[index][v2] = k
index += 1
print origin_dict
print dict(solution)
答案 1 :(得分:0)
这是我的简单而有效的解决方案。
if [[ "$(adb shell dumpsys wifi | grep mAirplaneModeOn)" == "mAirplaneModeOn true" ]]; then
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
&& input keyevent 19
&& input keyevent 23
&& input keyevent 4
fi