标签: python-itertools

如何搜索字符串以查看我是否可拼写单词

比如我有

x = "dsjcosnag"
y = "dog"

print(checkYinX(y,x))
>>true
Run Code Online (Sandbox Code Playgroud)

所以我想我需要使用while循环作为y中每个字母的计数器,然后我可以使用itetools循环遍历每个x,每个循环它会检查x == y,如果它是它会删除它然后检查o中的下一个字母.

有更简单的方法吗?

python string python-itertools

0
推荐指数
1
解决办法
64
查看次数

使用lambda函数过滤iterables

我试图在Python中解决Project Euler问题2,并决定基于iterables的策略.

这是Fibonacci序列的生成器,

def fnFibonacci():
    fibNumPrev, fibNumCurrent = 0, 1
    while True:
        yield fibNumCurrent
        fibNumPrev, fibNumCurrent = fibNumCurrent, fibNumCurrent + fibNumPrev
Run Code Online (Sandbox Code Playgroud)

当我尝试过滤掉小于4百万且可被2整除的斐波那契数字时,它不起作用,将所有内容过滤掉:

sum(list(itertools.takewhile(lambda x: x < 4e6 and x % 2 == 0 , fnFibonacci())))
Run Code Online (Sandbox Code Playgroud)

但是,这两个(忽略了均匀条件):

sum(list(itertools.takewhile(lambda x: x < 4e6, fnFibonacci())))
Run Code Online (Sandbox Code Playgroud)

这个列表理解:

sum([fibNum for fibNum in list(itertools.takewhile(lambda x: x < 4e6, fnFibonacci())) if fibNum % 2 == 0])
Run Code Online (Sandbox Code Playgroud)

工作.真的无法分辨出发生了什么.

python lambda python-itertools python-3.x

0
推荐指数
1
解决办法
264
查看次数

从csv中的组中选择数据并将数据附加到文本文件

我有一个问题,我不知道目前如何解决.我有一个csv,格式如下所示.现在我需要做的是执行一些匹配方案并将一些文本字符串附加到文件.

x,classA,uniqueclassindicator1,1,125,21.8,1,5.22,
x,classc,uniqueclassindicator1,3,125,21.8,2,5.22,
x,classd,uniqueclassindicator2,1,125,21.8,,,
x,classe,uniqueclassindicator2,2,125,21.8,,,
x,classBa,uniqueclassindicator2,3,125,21.8,,,
x,classBc,uniqueclassindicator2,4,125,21.8,,,
x,classAd,uniqueclassindicator3,1,125,21.8,2,2.56,
x,classc,uniqueclassindicator3,2,125,21.8,1,2.56,
x,classD,uniqueclassindicator3,3,125,21.8,,,
x,classa,uniqueclassindicator3,4,125,21.8,,,
x,classn,uniqueclassindicator4,1,125,21.8,,,
x,classm,uniqueclassindicator4,2,125,21.8,,,
x,classt,uniqueclassindicator4,3,125,21.8,,,
x,classd,uniqueclassindicator4,4,125,30.8,,,
x,classa,uniqueclassindicator4,5,125,31.8,,,
x,classn,uniqueclassindicator4,6,125,30.8,,,
x,classq,uniqueclassindicator5,1,125,35.8,1,3.31,3.1
x,classqe,uniqueclassindicator5,2,125,21.8,2,3.31,3.1 
x,classS,uniqueclassindicator5,3,125,21.8,3.31,3.1
x,classK,uniqueclassindicator5,4,125,21.8,,,
x,classL,uniqueclassindicator5,5,125,21.8,,,
x,classG,uniqueclassindicator5,6,125,21.8,,,
x,classH,uniqueclassindicator6,1,125,35.8,1,2.89,2.25   
x,classF,uniqueclassindicator6,2,125,21.8,3,2.89,2.25
x,classP,uniqueclassindicator6,3,125,21.8,2,2.89,2.25
x,classY,uniqueclassindicator6,4,125,21.8,,,
x,classU,uniqueclassindicator6,5,125,21.8,,,
x,classR,uniqueclassindicator6,6,125,21.8,,,
Run Code Online (Sandbox Code Playgroud)

在整个示例中,假设基于零的索引

您会注意到在csv中,第2列是uniqueclassindicator,我需要为每个类执行以下操作.

1.

如果第3列和第6列为1,并且第3列和第6列中相同的唯一类(不同的行)都是2,则生成字符串:

   "text data text" (column [1]) #where row = 1# "text data" column [1] #where row =2# "text" (column[17])`
Run Code Online (Sandbox Code Playgroud)

例如,在第15行中我们有这个确切的场景.所以字符串文本字符串需要读取: text data text classq text data classqe text 3.31

在上面的文本字符串中,从第1列第15行拉出"classq",从第1列第16行拉出"classqe",从第8列第15行拉出"3.31".

重申一下,产生此字符串的匹配是针对此类中的uniqueclassindicator5,第3列和第6列匹配(1-1和2-2)

2.

与1基本相同,但是当第3列和第6列为1,2和2,1时.这发生在uniqueclassindicator3中,请参阅第7行作为示例.所以我们需要追加字符串:

text data text classc text data classAd text 2.56 #Note I have listed the class …
Run Code Online (Sandbox Code Playgroud)

python csv python-itertools python-2.7

0
推荐指数
1
解决办法
292
查看次数

两个字典python的笛卡尔积

好的,所以我有两本词典.

dictionary_1 = {'status': ['online', 'Away', 'Offline'],
                'Absent':['yes', 'no', 'half day']}
dictionary_2 = {'healthy': ['yes', 'no'],
                'insane': ['yes', 'no']
Run Code Online (Sandbox Code Playgroud)

现在我需要将它们组合起来,以便我得到一个新的字典:

{'status': ['online', 'online', 'away', 'away', 'Offline', 'Offline'],
 'Absent': ['yes', 'yes', 'no', 'no', 'half day', 'half day'],
 'healthy': ['yes', 'no', 'yes', 'no', 'yes', 'no'],
 'insane': ['yes', 'no', 'yes', 'no', 'yes', 'no']
}
Run Code Online (Sandbox Code Playgroud)

这是一个很晚的更新,但如果有人感兴趣,我找到了一种没有itertools的方法.

def cartesian_product(dict1, dict2):
    cartesian_dict = {}
    dict1_length = len(list(dict1.values())[0])
    dict2_length = len(list(dict2.values())[0])
    h = []
    for key in dict1:
        for value in dict1[key]:
            if not key in cartesian_dict: …
Run Code Online (Sandbox Code Playgroud)

python dictionary cartesian-product python-itertools python-3.x

0
推荐指数
1
解决办法
3164
查看次数

如何使用itertools计算重复元素的所有组合?

我试图使用itertools来计算['a', 'b', 'c']使用combinations_with_replacement重复元素的列表的所有组合.问题在于索引似乎用于区分元素:

返回输入iterable中元素的r长度子序列,允许单个元素重复多次.

组合以字典排序顺序发出.因此,如果对输入iterable进行排序,则将按排序顺序生成组合元组.

元素根据其位置而不是其价值被视为唯一元素.因此,如果输入元素是唯一的,则生成的组合也将是唯一的.

请输入以下代码段:

import itertools

for item in itertools.combinations_with_replacement(['a','b','c'], 3): 
    print (item)
Run Code Online (Sandbox Code Playgroud)

结果输出:

('a', 'a', 'a')
('a', 'a', 'b')
('a', 'a', 'c')
('a', 'b', 'b')
('a', 'b', 'c')
('a', 'c', 'c')
('b', 'b', 'b')
('b', 'b', 'c')
('b', 'c', 'c')
('c', 'c', 'c')
Run Code Online (Sandbox Code Playgroud)

我需要的是组合集包含以下元素:('a', 'b', 'a')似乎缺少.如何计算完整的组合集?

python python-itertools

0
推荐指数
1
解决办法
1292
查看次数

如何在python中迭代三个列表

总蟒蛇新秀在这里.我希望迭代三个列表,其中前两个应该以所有可能的方式重新组合,而只有一个值附加到第三个列表的每个组合.到目前为止,我得到了:

import itertools

list1 = [1,2,3]
list2 = [1,2,3]
list3 = [1,2,3,4,5,6,7,8,9]

for x, y, in itertools.product(list1, list2):
    print x, y, list3
Run Code Online (Sandbox Code Playgroud)

返回:

1 1 [1, 2, 3, 4, 5, 6, 7, 8, 9]
1 2 [1, 2, 3, 4, 5, 6, 7, 8, 9]
1 3 [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 1 [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 2 [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 3 …
Run Code Online (Sandbox Code Playgroud)

python for-loop nested-loops python-itertools python-2.7

0
推荐指数
1
解决办法
1271
查看次数

python重复迭代器中的列表元素

有没有办法创建迭代器来重复列表中的元素某些时间?例如,列出了一个列表:

color = ['r', 'g', 'b']
Run Code Online (Sandbox Code Playgroud)

有没有办法以itertools.repeatlist(color, 7)可以生成以下列表的形式创建迭代器?

color_list = ['r', 'g', 'b', 'r', 'g', 'b', 'r']
Run Code Online (Sandbox Code Playgroud)

python python-itertools

0
推荐指数
1
解决办法
1220
查看次数

列表的所有排列都有重复但不是双倍

我见过类似的但不一样的:这里.我绝对想要所有列表元素的排列,而不是组合.我是不同的,因为a,b,c的itertools排列返回abc而不是aba(soooo close).我怎样才能得到像aba一样的结果?

('a',)     <-excellent
('b',)     <-excellent
('c',)     <-excellent
('a', 'b') <-excellent
('a', 'c') <-excellent
('b', 'a') <-excellent
('b', 'c') <-excellent
('c', 'a') <-excellent
('c', 'b') <-excellent
('a', 'b', 'c') <-- I need a,b,a
('a', 'c', 'b') <-- I need a,c,a
('b', 'a', 'c') <-- I need b,a,b... you get the idea
Run Code Online (Sandbox Code Playgroud)

哦最大排列长度(python.org itertools中的"r")等于len(列表),我不想包括'双打',例如aab或abb ......或者abba:P列表可以任何长度.

import itertools
from itertools import product
my_list = ["a","b","c"]
#print list(itertools.permutations(my_list, 1))
#print list(itertools.permutations(my_list, 2))
#print list(itertools.permutations(my_list, 3)) <-- this *ALMOST* works …
Run Code Online (Sandbox Code Playgroud)

python permutation python-itertools

0
推荐指数
1
解决办法
84
查看次数

创建特定的排列Python

我有二维列表的值:

# 4 x 4, 2-dimensional list
values = [[4, 7, 8], 
          [1, 3, 4], 
          [7, 5, 6], 
          [2, 9, 1]]
Run Code Online (Sandbox Code Playgroud)

我想为每个列表创建包含这些值的所有可能排列(笛卡尔积)的元组.

# example for the list at index 0 of values
args0 = [(4, 7, 8), (7, 4, 8), (4, 8, 7), (8, 4, 7), (7, 8, 4), (8, 7, 4)] 
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法来解决这个问题吗?我已经尝试过itertools,但无法使用"特定值".

python list permutation python-itertools

0
推荐指数
1
解决办法
29
查看次数

Itertools groupby: group list of lists by first two values of sublists

I have a list of lists like this:

data = [['a', 'b', 2000, 100], ['a', 'b', 4000, 500], ['c', 'd', 500, 8000], ['c', 'd', 60, 8000], ['c', 'd', 70, 1000], ['a', 'd', 2000, 100], ['a', 'd', 1000, 100]]
Run Code Online (Sandbox Code Playgroud)

and I want to group them together if they have the same first two values. Output would be:

data = [(['a', 'b', 2000, 100], ['a', 'b', 4000, 500]), (['c', 'd', 500, 8000], ['c', 'd', 60, 8000], ['c', 'd', 70, 1000]), (['a', 'd', …
Run Code Online (Sandbox Code Playgroud)

python performance group-by python-itertools

0
推荐指数
1
解决办法
61
查看次数