<tfoot id='T0fY0'></tfoot>

    <small id='T0fY0'></small><noframes id='T0fY0'>

      <bdo id='T0fY0'></bdo><ul id='T0fY0'></ul>

    1. <i id='T0fY0'><tr id='T0fY0'><dt id='T0fY0'><q id='T0fY0'><span id='T0fY0'><b id='T0fY0'><form id='T0fY0'><ins id='T0fY0'></ins><ul id='T0fY0'></ul><sub id='T0fY0'></sub></form><legend id='T0fY0'></legend><bdo id='T0fY0'><pre id='T0fY0'><center id='T0fY0'></center></pre></bdo></b><th id='T0fY0'></th></span></q></dt></tr></i><div id='T0fY0'><tfoot id='T0fY0'></tfoot><dl id='T0fY0'><fieldset id='T0fY0'></fieldset></dl></div>
    2. <legend id='T0fY0'><style id='T0fY0'><dir id='T0fY0'><q id='T0fY0'></q></dir></style></legend>
    3. pythononic 切片集合的方式是什么?

      时间:2023-07-24
        <legend id='p2LeE'><style id='p2LeE'><dir id='p2LeE'><q id='p2LeE'></q></dir></style></legend>

        1. <small id='p2LeE'></small><noframes id='p2LeE'>

          • <bdo id='p2LeE'></bdo><ul id='p2LeE'></ul>
            <i id='p2LeE'><tr id='p2LeE'><dt id='p2LeE'><q id='p2LeE'><span id='p2LeE'><b id='p2LeE'><form id='p2LeE'><ins id='p2LeE'></ins><ul id='p2LeE'></ul><sub id='p2LeE'></sub></form><legend id='p2LeE'></legend><bdo id='p2LeE'><pre id='p2LeE'><center id='p2LeE'></center></pre></bdo></b><th id='p2LeE'></th></span></q></dt></tr></i><div id='p2LeE'><tfoot id='p2LeE'></tfoot><dl id='p2LeE'><fieldset id='p2LeE'></fieldset></dl></div>
              <tbody id='p2LeE'></tbody>

          • <tfoot id='p2LeE'></tfoot>

              • 本文介绍了pythononic 切片集合的方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一些数据列表,例如

                I have some list of data, for example

                some_data = [1, 2, 4, 1, 6, 23, 3, 56, 6, 2, 3, 5, 6, 32, 2, 12, 5, 3, 2]
                

                我想获得具有固定长度的唯一值(我不在乎我会得到哪个),我也希望它是 set 对象.

                and i want to get unique values with fixed length(i don't care which i will get) and i also want it to be set object.

                我知道我可以从 some_data 中进行 set,然后将其设为 list,对其进行裁剪,然后将其设为 set再次.

                I know that i can do set from some_data then make it list, crop it and then make it set again.

                set(list(set(some_data))[:5])  # don't look so friendly
                

                我知道我在 set 中没有 __getitem__ 方法,这不会使整个切片成为可能,但如果有机会让它看起来更好?

                I understand that i don't have __getitem__ method in set which wouldn't make the whole slice thing possible, but if there is a chance to make it look better?

                我完全理解 set 是无序的.因此,最终 set 中会出现哪些元素并不重要.

                And i completely understand that set is unordered. So it don't matter which elements will get in final set.

                可能的选择是使用:

                • 有序集
                • 使用 dictNone 值:

                set(dict(map(lambda x: (x, None), some_data)).keys()[:2])  # not that great
                

                推荐答案

                集合是可迭代的.如果您真的不在乎选择了您的集合中的哪些项目,您可以使用 itertools.islice 来获取一个迭代器,该迭代器将产生指定数量的项目(无论哪个在迭代顺序中排在第一位).将迭代器传递给 set 构造函数,您就可以在不使用任何额外列表的情况下获得子集:

                Sets are iterable. If you really don't care which items from your set are selected, you can use itertools.islice to get an iterator that will yield a specified number of items (whichever ones come first in the iteration order). Pass the iterator to the set constructor and you've got your subset without using any extra lists:

                import itertools
                
                some_data = [1, 2, 4, 1, 6, 23, 3, 56, 6, 2, 3, 5, 6, 32, 2, 12, 5, 3, 2]
                big_set = set(some_data)
                small_set = set(itertools.islice(big_set, 5))
                

                虽然这是您所要求的,但我不确定您是否真的应该使用它.集合可能会以非常确定的顺序进行迭代,因此如果您的数据经常包含许多相似的值,那么每次执行此操作时您最终可能会选择一个非常相似的子集.当数据由整数组成时(如示例中所示),这尤其糟糕,这些整数对自身进行哈希处理.迭代集合时,连续的整数会经常按顺序出现.使用上面的代码,在 big_set 中只有 32 乱序(使用 Python 3.5),所以 small_set{32, 1, 2, 3, 4}.如果您将 0 添加到数据中,即使数据集变得很大,您几乎总是会以 {0, 1, 2, 3, 4} 结束,因为这些值将始终填满集合哈希表中的前五个位置.

                While this is what you've asked for, I'm not sure you should really use it. Sets may iterate in a very deterministic order, so if your data often contains many similar values, you may end up selecting a very similar subset every time you do this. This is especially bad when the data consists of integers (as in the example), which hash to themselves. Consecutive integers will very frequently appear in order when iterating a set. With the code above, only 32 is out of order in big_set (using Python 3.5), so small_set is {32, 1, 2, 3, 4}. If you added 0 to the your data, you'd almost always end up with {0, 1, 2, 3, 4} even if the dataset grew huge, since those values will always fill up the first fives slots in the set's hash table.

                为了避免这种确定性采样,您可以使用 random.sample 如 jprockbelly 建议的那样.

                To avoid such deterministic sampling, you can use random.sample as suggested by jprockbelly.

                这篇关于pythononic 切片集合的方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='dRHk0'></bdo><ul id='dRHk0'></ul>
                    <i id='dRHk0'><tr id='dRHk0'><dt id='dRHk0'><q id='dRHk0'><span id='dRHk0'><b id='dRHk0'><form id='dRHk0'><ins id='dRHk0'></ins><ul id='dRHk0'></ul><sub id='dRHk0'></sub></form><legend id='dRHk0'></legend><bdo id='dRHk0'><pre id='dRHk0'><center id='dRHk0'></center></pre></bdo></b><th id='dRHk0'></th></span></q></dt></tr></i><div id='dRHk0'><tfoot id='dRHk0'></tfoot><dl id='dRHk0'><fieldset id='dRHk0'></fieldset></dl></div>

                  • <tfoot id='dRHk0'></tfoot>
                      <tbody id='dRHk0'></tbody>

                    <small id='dRHk0'></small><noframes id='dRHk0'>

                  • <legend id='dRHk0'><style id='dRHk0'><dir id='dRHk0'><q id='dRHk0'></q></dir></style></legend>