标签: python-itertools

使用itertools.dropwhile时遇到困难

我试图itertools.dropwhile只使用返回第三个元素之后的生成器中的元素,但是我遇到了一些麻烦:

from itertools import dropwhile

    it = (i for i in range(10,20))
    a = dropwhile(enumerate < 3, it)   
    next(a)
    TypeError: 'bool' object is not callable 
Run Code Online (Sandbox Code Playgroud)

我正在寻找的输出是:

[14, 15, 16, 17, 18, 19]
Run Code Online (Sandbox Code Playgroud)

谁能解释我的代码有什么问题并提供有效的解决方案?谢谢.

python python-itertools

2
推荐指数
1
解决办法
4340
查看次数

python list by first first

list1=['hello','hope','hate','hack','bit','basket','code','come','chess']
Run Code Online (Sandbox Code Playgroud)

我需要的是:

list2=[['hello','hope','hate','hack'],['bit','basket'],['code','come','chess']]
Run Code Online (Sandbox Code Playgroud)

如果第一个字符相同并且是相同的组,则将其子列表.

我怎么解决这个问题?

python python-itertools divide

2
推荐指数
1
解决办法
3305
查看次数

Python itertools.islice的源代码在哪里?

Python itertools.islice的源代码在哪里?

我找到了itertoolsmodule.c,但找不到函数itertools.islice(也许它就在那里,但我对C代码知之甚少).

我想了解当我这样做时会发生什么:

itertools.islice(spamreader, rowStart, rowFinish+1)
Run Code Online (Sandbox Code Playgroud)

python python-itertools

2
推荐指数
1
解决办法
554
查看次数

我怎样才能将列表压缩到已经压缩的列表中?

假设我有一个两元素元组列表和一个(非元组)文字列表,例如整数:

a = [('x', 'a'), ('y', 'b'), ('z', 'c')]
b = [1, 2 ,3]
Run Code Online (Sandbox Code Playgroud)

我想列出三元素元组,所以我编码如下:

zipped = zip((t[0] for t in a), (t[1] for t in a), b)
assert zipped == [('x', 'a', 1), ('y', 'b', 2), ('z', 'c', 3)]
Run Code Online (Sandbox Code Playgroud)

我当前的代码工作得很好,但我想知道有更高效和优雅的配方,但我的代码必须迭代并解压缩每个元组两次.任何人都可以建议吗?

python list python-itertools iterable-unpacking

2
推荐指数
1
解决办法
341
查看次数

'leet'程序 - 获得所有排列

我写了一个简单的函数,将单词中的字母转换为它们的'leet'数字对应物.

def Leet(word):
    letters = list(word.lower())
    for n, letter, in enumerate(letters):
        if letter == 'o':
            letters[n]= '0'
        elif letter == 'i':
            letters[n]= '1'
        elif letter == 'z':
            letters[n]= '2'
        elif letter == 'e':
            letters[n]= '3'
        elif letter == 'a':
            letters[n]= '4'
        elif letter == 's':
            letters[n]= '5'
        elif letter == 'g':
            letters[n]= '6'
        elif letter == 't':
            letters[n]= '7'
        elif letter == 'b':
            letters[n]= '8'
    return ''.join(letters)
Run Code Online (Sandbox Code Playgroud)

所以当我输入时'zit',程序将返回'217'.

我的问题是,我该怎么改变它给我的每一个可能的置换('217','2it','z1t','zi7' …

python permutation cartesian-product python-itertools

2
推荐指数
1
解决办法
1007
查看次数

如何为pandas中的多个变量明智地创建所有组合?

对于n个变量的给定范围.我以n = 3为例.

A : [1,3]
B:  [5,10,12]
C: [100,113]
Run Code Online (Sandbox Code Playgroud)

请注意,上述范围内的值也可以是浮点数.

我们如何创建一个数据框,其中每列代表输入变量的唯一组合?

    c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 c11 c12
a   1   1   1   3   3   3   1   1   1   3   3   3
b   5   10  12  5   10  12  5   10  12  5   10  12
c   100 100 100 100 100 100 113 113 113 113 113 113
Run Code Online (Sandbox Code Playgroud)

python combinations loops python-itertools dataframe

2
推荐指数
1
解决办法
5210
查看次数

如果我取消注释for循环中的print()函数,'groups'列表将填充空列表而不是结果

import itertools

data1 =[100,80,80,80,80,80,90,90,85,85,85]
keys = []
groups = []
sorted_data1 = sorted(data1)

for k,g in itertools.groupby(sorted_data1):
    #print('key: ',k,' group: ',list(g))
    keys.append(k)
    groups.append(list(g))

print('keys: ', keys)
print('Groups: ', groups)
Run Code Online (Sandbox Code Playgroud)

上述代码的结果:

keys:  [80, 85, 90, 100]
Groups:  [[80, 80, 80, 80, 80], [85, 85, 85], [90, 90], [100]]

RESULT I GET WHEN 'print()' IS UNCOMMENTED:
key:  80  group:  [80, 80, 80, 80, 80]
key:  85  group:  [85, 85, 85]
key:  90  group:  [90, 90]
key:  100  group:  [100]
keys:  [80, 85, …
Run Code Online (Sandbox Code Playgroud)

python for-loop python-itertools python-3.x

2
推荐指数
1
解决办法
41
查看次数

根据值生成现有dict中的所有可能的dicts

我有一本字典

kwarg = {'a': 0 , 'b': 1 , 'c': 2} 我希望根据以下值生成所有可能的替代词典:

x1={'a': 1 , 'b': 0 , 'c':2}

x2={'a': 1 , 'b': 2 , 'c':0}

x3={'a': 0 , 'b': 2 , 'c':1}

x4={'a': 0 , 'b': 1 , 'c':2}

x5={'a': 2 , 'b': 1 , 'c':0}

x6={'a': 2 , 'b': 0 , 'c':1}
Run Code Online (Sandbox Code Playgroud)

但我有6个元素(0,1,2,3,4,5),需要使用python algorytm.

python combinations dictionary python-itertools python-3.x

2
推荐指数
1
解决办法
26
查看次数

列表python的有限排列

我有一个列表,想生成没有重复元素的有限数量的排列。

itertools.permutations(x)
Run Code Online (Sandbox Code Playgroud)

给出所有可能的排序,但我只需要特定数量的排列。(我的初始列表包含〜200个元素=> 200!将花费不合理的时间,我不需要所有这些元素)

到目前为止我做了什么

def createList(My_List):
    New_List = random.sample(My_List, len(My_List))
    return New_List

def createManyList(Nb_of_Lists):
    list_of_list = []
    for i in range(0, Nb_of_Lists):
        list_of_list.append(createList())
    return list_of_list
Run Code Online (Sandbox Code Playgroud)

它正在运行,但是我的List_of_list将没有唯一的排列,或者至少我对此没有任何保证。

有没有办法做到这一点?谢谢

python permutation python-itertools

2
推荐指数
1
解决办法
52
查看次数

为什么文档中提到padnone模仿内置地图的行为?

在适用于Python 3.7和更低版本的Itertools食谱中,提到了padnone “模拟内置map()函数的行为”:

def padnone(iterable):
    """Returns the sequence elements and then returns None indefinitely.

    Useful for emulating the behavior of the built-in map() function.
    """
    return chain(iterable, repeat(None))
Run Code Online (Sandbox Code Playgroud)

虽然我了解的有用性padnone,但是我看不到它如何以任何方式模拟 map。这是错误还是我错过了什么?

python python-itertools python-3.x

2
推荐指数
1
解决办法
36
查看次数