我有一個包含數據,這樣的列表:檢測連續整數列表中的
[1, 2, 3, 4, 7, 8, 10, 11, 12, 13, 14]
我想打印出連續整數的範圍:
1-4, 7-8, 10-14
是否有一個內置在/快速/有效的方式做到這一點?
我有一個包含數據,這樣的列表:檢測連續整數列表中的
[1, 2, 3, 4, 7, 8, 10, 11, 12, 13, 14]
我想打印出連續整數的範圍:
1-4, 7-8, 10-14
是否有一個內置在/快速/有效的方式做到這一點?
從the docs:
>>> from itertools import groupby
>>> from operator import itemgetter
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
>>> for k, g in groupby(enumerate(data), lambda (i, x): i-x):
... print map(itemgetter(1), g)
...
[1]
[4, 5, 6]
[10]
[15, 16, 17, 18]
[22]
[25, 26, 27, 28]
你可以很容易適應這種獲得印刷範圍集合。
不要忘記'導入itertools'。此外,這隻適用於Python 2.4及更高版本。 – Gabe 2010-03-02 09:48:16
實際上你至少在Python 2.6中需要從'itertools import *'和'from operator import *'(或者等價的)。 – 2011-04-11 11:12:34
不要使用明星進口! **從不**使用明星進口!改用'from itertools import groupby'和'from operator import itemgetter'來代替。 – 2013-08-28 20:41:12
內置:否,據我所知。您必須在數組中運行。首先將第一個值放在一個變量中並打印出來,然後只要你繼續敲下一個數字,別忘了記住另一個變量的最後一個數字。如果下一個數字不符合,請檢查記住的最後一個數字與第一個數字。如果它是一樣的,什麼都不要做。如果不同,請打印「 - 」和最後一個數字。然後將當前值放入第一個變量中並重新開始。 在數組的末尾,您運行相同的例程,就好像您已經輸入了一個數字。
我可以寫代碼,當然,但我不想破壞你的家庭作業:-)
你指定這正是打印:
>>> nums = [1, 2, 3, 4, 7, 8, 10, 11, 12, 13, 14]
>>> ranges = sum((list(t) for t in zip(nums, nums[1:]) if t[0]+1 != t[1]), [])
>>> iranges = iter(nums[0:1] + ranges + nums[-1:])
>>> print ', '.join([str(n) + '-' + str(next(iranges)) for n in iranges])
1-4, 7-8, 10-14
如果列表任何單一的號碼範圍,它們將被表示爲NN:
>>> nums = [1, 2, 3, 4, 5, 7, 8, 9, 12, 15, 16, 17, 18]
>>> ranges = sum((list(t) for t in zip(nums, nums[1:]) if t[0]+1 != t[1]), [])
>>> iranges = iter(nums[0:1] + ranges + nums[-1:])
>>> print ', '.join([str(n) + '-' + str(next(iranges)) for n in iranges])
1-5, 7-9, 12-12, 15-18
這裏是不使用任何模塊,這是良好的採訪,通常在他們要求,而不USI面試另一鹼性溶液NG任何模塊:
#!/usr/bin/python
def split_list(n):
"""will return the list index"""
return [(x+1) for x,y in zip(n, n[1:]) if y-x != 1]
def get_sub_list(my_list):
"""will split the list base on the index"""
my_index = split_list(my_list)
output = list()
prev = 0
for index in my_index:
new_list = [ x for x in my_list[prev:] if x < index]
output.append(new_list)
prev += len(new_list)
output.append([ x for x in my_list[prev:]])
return output
my_list = [1, 3, 4, 7, 8, 10, 11, 13, 14]
print get_sub_list(my_list)
輸出:
[[1], [3, 4], [7, 8], [10, 11], [13, 14]]
我有一個類似的問題,我使用排序列表如下。它輸出一個字典中列出的值範圍的字典。這些鍵分隔連續數字的每次運行,並且也是序列之間數字之間的非連續項目的運行總數。
你的清單給我的{0: [1, 4], 1: [7, 8], 2: [10, 14]}
def series_dictf(index_list):
from collections import defaultdict
series_dict = defaultdict(list)
sequence_dict = dict()
list_len = len(index_list)
series_interrupts = 0
for i in range(list_len):
if i == (list_len - 1):
break
position_a = index_list[i]
position_b = index_list[i + 1]
if position_b == (position_a + 1):
sequence_dict[position_a] = (series_interrupts)
sequence_dict[position_b] = (series_interrupts)
if position_b != (position_a + 1):
series_interrupts += 1
for position, series in sequence_dict.items():
series_dict[series].append(position)
for series, position in series_dict.items():
series_dict[series] = [position[0], position[-1]]
return series_dict
輸出使用設置操作,下面的算法可以執行
def get_consecutive_integer_series(integer_list):
integer_list = sorted(integer_list)
start_item = integer_list[0]
end_item = integer_list[-1]
a = set(integer_list) # Set a
b = range(start_item, end_item+1)
# Pick items that are not in range.
c = set(b) - a # Set operation b-a
li = []
start = 0
for i in sorted(c):
end = b.index(i) # Get end point of the list slicing
li.append(b[start:end]) # Slice list using values
start = end + 1 # Increment the start point for next slicing
li.append(b[start:]) # Add the last series
for sliced_list in li:
if not sliced_list:
# list is empty
continue
if len(sliced_list) == 1:
# If only one item found in list
yield sliced_list[0]
else:
yield "{0}-{1}".format(sliced_list[0], sliced_list[-1])
a = [1, 2, 3, 6, 7, 8, 4, 14, 15, 21]
for series in get_consecutive_integer_series(a):
print series
輸出爲上述列表中的 「a」
1-4
6-8
14-15
您可以使用具有名爲Counter的類的集合庫。計數器可以派上用場,如果試圖輪詢沒有明顯的元素的可迭代的
from collections import Counter
data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
cnt=Counter(data)
print(cnt)
輸出來這樣
Counter({1: 1, 4: 1, 5: 1, 6: 1, 10: 1, 15: 1, 16: 1, 17: 1, 18: 1, 22: 1, 25: 1, 26: 1, 27: 1, 28: 1})
這就像任何其他的字典,可以查詢的鍵值
這並沒有回答這個問題,你只是在計算出現的事件,而OP問到對'範圍' – user1767754 2018-01-06 22:47:56
一個簡短的解決方案,無需額外的導入工作。它接受任何可迭代,排序無序的投入,並刪除重複的項目:
def ranges(nums):
nums = sorted(set(nums))
gaps = [[s, e] for s, e in zip(nums, nums[1:]) if s+1 < e]
edges = iter(nums[:1] + sum(gaps, []) + nums[-1:])
return list(zip(edges, edges))
例子:
>>> ranges([2, 3, 4, 7, 8, 9, 15])
[(2, 4), (7, 9), (15, 15)]
>>> ranges([-1, 0, 1, 2, 3, 12, 13, 15, 100])
[(-1, 3), (12, 13), (15, 15), (100, 100)]
>>> ranges(range(100))
[(0, 99)]
>>> ranges([0])
[(0, 0)]
>>> ranges([])
[]
這是一樣的@ dansalmo的solution我發現驚人的,雖然有點難以閱讀和適用(因爲它不是作爲函數給出的)。
請注意,它可以很容易地修改,以便通過例如噴出「傳統」開放範圍[start, end)
。改變return語句:
return [(s, e+1) for s, e in zip(edges, edges)]
見http://stackoverflow.com/questions/2154249/identify-groups-of-continuous-numbers-in-a-list,這點你到http://文檔。 python.org/library/itertools.html#examples – 2010-03-02 09:14:46
家庭作業?你向我們展示了你所嘗試過的東西,我們會看看我們能否做得更好。 – 2010-03-02 09:16:12
謝謝,我應該在發現之前發現:/ – James 2010-03-02 09:18:13