我有类似的字典:
{0:'I', 3: 'Mr.', 5: 'Nevrekar', 8: 'JK', 14: 'Soham', 15: 'Sharma', 4: 'Akshay'}
我要返回它的连续键的第一组合(最少2个)。 在上述情况下为(3,4,5)。
因此它将返回一个列表:
['Mr.', 'Akshay', 'Nevrekar'].
我该如何实现?
我的尝试:
name=[]
count = 0
for k,g in name_list.items():
if count == 0:
temp =None
if k-1 == temp:
name.append(g)
count+= 1
temp = k
其输出为:
['Akshay', 'Nevrekar', 'Sharma']
答案 0 :(得分:1)
more_itertools
中有一个食谱,在这里很方便:
import more_itertools as mit
d = {0:'I', 3: 'Mr.', 4: 'Akshay', 5: 'Nevrekar', 8: 'JK', 14: 'Soham', 15: 'Sharma'}
cons = [list(group) for group in mit.consecutive_groups(sorted(d.keys()))]
# [[0], [3, 4, 5], [8], [14, 15]]
res = [[d[y] for y in x] for x in cons if len(x)>1]
# [['Mr.', 'Akshay', 'Nevrekar'], ['Soham', 'Sharma']]
如果仅需要连续整数至少为 3的组,则必须相应地修改不等式(len(x) > 2
)。
答案 1 :(得分:0)
来自the docs:
public void execPrint(String url) {
System.Windows.Forms.WebBrowser ie = new System.Windows.Forms.WebBrowser();
ie.DocumentCompleted += Ie_DocumentCompleted;
ie.Navigate(url);
ie.Visible = false;
}
private void Ie_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
System.Windows.Forms.WebBrowser ie = (System.Windows.Forms.WebBrowser)sender;
ie.ShowPrintPreviewDialog();
}