Bence çok ilginç soruluk olduğunu: yapısına bloğu dışında deneyin-bir ayar "af dile" yöntemi de vardır. toplama sipariş olup olmadığını kontrol etmek için saf Python (utility kodu olmadan) bir yolu yoktur.
Eğer
collections.Iterable
türünü kullanabilirsiniz
Sequence
veya GeneratorType
kontrol etmek) = en sırayla gidelim.
>>>
>>> import collections
>>>
>>> result = [1,2,3,4,-1]
>>> isinstance(result, collections.Iterable)
True
>>>
>>> def generator_func(arg=10):
... for i in xrange(arg):
... yield i
...
>>>
>>> generator_func()
<generator object generator_func at 0x7f667c50f190>
>>>
>>> result = generator_func()
>>> isinstance(result, collections.Iterable)
True
Ama:
>>>
>>> result = {1,2,3,4,-1}
>>> isinstance(result, collections.Iterable)
True
>>>
Bu sizin için kötü bir durumdur. Çünkü: Bu durum için Elbette
>>> x = {1,2,3,-1}
>>> x
set([1, 2, 3, -1])
>>> [_ for _ in x]
[1, 2, 3, -1]
>>> x = {1,2,3,0}
>>> x
set([0, 1, 2, 3])
>>> [_ for _ in x]
[0, 1, 2, 3]
>>> import collections
>>> isinstance(x, collections.Iterable)
True
>>>
Bir collections.Sequence sadece kullanmalıdır.
>>> result = {1,2,3,4,-1}
>>> isinstance(result, collections.Sequence)
False
>>> isinstance({1:2, 3:3}, collections.Sequence)
False
>>>
Ama:
>>> result = generator_func()
>>> isinstance(result, collections.Sequence)
False
>>>
Böylece, ben bir fikir Sequence or GeneratorType
güzel kontrol etmek olduğunu düşünüyorum. Yani
:
>>> result = generator_func()
>>> isinstance(result, (collections.Sequence, collections.Iterator))
True
>>> result = [1,2,3,4,5]
>>> isinstance(result, (collections.Sequence, collections.Iterator))
True
>>> result = (1,2,3,4,5)
>>> isinstance(result, (collections.Sequence, collections.Iterator))
True
>>> result = {1,2,3,4,5}
>>> isinstance(result, (collections.Sequence, collections.Iterator))
False
>>> result = {1:1,2:2,3:3,4:4,5:5}
>>> isinstance(result, (collections.Sequence, collections.Iterator))
False
>>>
Аbout sırası bu bağlantıyı kontrol edin . Eğer öğelerin sipariş hakkında emin değilseniz
, ben bunları açıkça kontrol etmeliyiz.
«Açık örtülü daha iyidir.
>>>
>>> def order_check(result, order_rule = cmp_rule):
... for item, next_item in zip(result, result[1:]):
... if not order_rule(item, next_item):
... return False
... return True
...
>>> def cmp_rule(item, next_item):
... if item < next_item:
... return True
... return False
...
>>>
>>> result = [1,2,3,4,5]
>>> order_check(result)
True
>>> result = [1,2,3,4,5,-1]
>>> order_check(result)
False
>>>
»Fakat dürüstçe, jeneratörler Eğer içinde üretmek olarak sipariş aynı olacaktır garanti.
Neyin peşinde olduğunuzu anlamak oldukça zor. Örnekler verin lütfen. Yapısal olarak 'veri tipi' mi demek istiyorsunuz? – Pynchia
Bir işlevi çağırıyorum ve bir çeşit veri yapısı döndürüyor. Bana bir liste verebilirler, bana bir set verebilirler veya kendi yinelenebilir sınıflarını uygulayabilirlerdi. Bu yapının düzeni olduğunu ve yinelemeye karar verdiğimden bağımsız olarak aynı şekilde döneceğinden emin olmak istiyorum. –
Örnek olarak, bir set sipariş edilmez. Bir liste sipariş edilir. Sipariş verilmediğinden, bir set test durumlarımdan doğal olarak başarısız olmalıdır. Test davalarımın bir listesi geçmeli. –