我怎样才能管理一个包含1亿多字符串的庞大列表?我怎样才能开始使用如此庞大的列表?
示例大列表:
cards = [
"2s","3s","4s","5s","6s","7s","8s","9s","10s","Js","Qs","Ks","As"
"2h","3h","4h","5h","6h","7h","8h","9h","10h","Jh","Qh","Kh","Ah"
"2d","3d","4d","5d","6d","7d","8d","9d","10d","Jd","Qd","Kd","Ad"
"2c","3c","4c","5c","6c","7c","8c","9c","10c","Jc","Qc","Kc","Ac"
]
from itertools import combinations
cardsInHand = 7
hands = list(combinations(cards, cardsInHand))
print str(len(hands)) + " hand combinations in texas holdem poker"
Run Code Online (Sandbox Code Playgroud) 假设我有list_of_numbers = [[1, 2], [3], []]并且想要更简单的对象列表对象x = [1, 2, 3]。
按照这个相关解决方案的逻辑,我做
list_of_numbers = [[1, 2], [3], []]
import itertools
chain = itertools.chain(*list_of_numbers)
Run Code Online (Sandbox Code Playgroud)
不幸的是,chain这并不完全是我想要的,因为(例如)chain在控制台上运行会返回<itertools.chain object at 0x7fb535e17790>.
f如果我这样做x = f(chain)然后x在控制台上输入我得到的功能是什么[1, 2, 3]?
更新: 实际上我最终需要的结果是array([1, 2, 3])。我在所选答案的评论中添加一行来解决这个问题。
假设我有两个列表(或numpy.arrays):
a = [1,2,3]
b = [4,5,6]
Run Code Online (Sandbox Code Playgroud)
如何检查每个元素a是否小于b同一索引的相应元素?(我假设指数从0开始)即
at index 0 value of a = 1 < value of b = 4
at index 1 value of a = 2 < value of b = 5
at index 2 value of a = 3 < value of b = 6
Run Code Online (Sandbox Code Playgroud)
如果a等于[1,2,7],则那将是不正确的,因为索引2的值a大于b.如果a长度比任何长度小b,它应该只比较指数a和b.
例如这对a,b
a = [1,2] …Run Code Online (Sandbox Code Playgroud) 这是我的问题.给出一个清单
xList = [9, 13, 10, 5, 3]
Run Code Online (Sandbox Code Playgroud)
我想计算每个元素的总和乘以后续元素
sum([9*13, 9*10, 9*5 , 9*3]) +
sum([13*10, 13*5, 13*3]) +
sum([10*5, 10*3]) +
sum ([5*3])
Run Code Online (Sandbox Code Playgroud)
在这种情况下,答案是608.
有没有办法做到这一点或许itertools本地numpy?
以下是我提出的功能.它完成了这项工作,但它远非理想,因为我想添加其他东西.
def SumProduct(xList):
''' compute the sum of the product
of a list
e.g.
xList = [9, 13, 10, 5, 3]
the result will be
sum([9*13, 9*10, 9*5 , 9*3]) +
sum([13*10, 13*5, 13*3]) +
sum([10*5, 10*3]) +
sum ([5*3])
'''
xSum = 0
for xnr, x in …Run Code Online (Sandbox Code Playgroud) 目的是在给定整数列表的情况下找到增加/单调数字的组.结果组中的每个项目必须是前一项目的+1增量
给出一个输入:
x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
我需要找到数量不断增加的群体并实现:
increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)]
Run Code Online (Sandbox Code Playgroud)
最终还有越来越多的数字:
len(list(chain(*increasing_numbers)))
Run Code Online (Sandbox Code Playgroud)
还有小组的len:
increasing_num_groups_length = [len(i) for i in increasing_numbers]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下来获得增加的数字:
>>> from itertools import tee, chain
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b)
...
>>> x = [8, 9, 10, 11, 7, 1, 2, 3, 4, 5, 6]
>>> set(list(chain(*[(i,j) for i,j in pairwise(x) if j-1==i])))
set([1, 2, 3, 4, 5, …Run Code Online (Sandbox Code Playgroud) itertools.countPython(2.7.9)中的计数器对于线程安全计数非常方便.我怎样才能获得计数器的当前值?
计数器递增并在每次调用时返回最后一个值next():
import itertools
x = itertools.count()
print x.next() # 0
print x.next() # 1
print x.next() # 2
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
我没有办法在没有调用的情况下获得计数器的当前值next(),这会产生增加计数器或使用repr()函数的不良副作用.
继上述内容之后:
print repr(x) # "count(3)"
Run Code Online (Sandbox Code Playgroud)
所以你可以解析输出repr().就像是
current_value = int(repr(x)[6:-1])
Run Code Online (Sandbox Code Playgroud)
会做的伎俩,但真的很难看.
有没有办法更直接地获得计数器的当前值?
我有3个numpy阵列,需要在它们之间形成笛卡尔积.阵列的尺寸不固定,因此它们可以采用不同的值,一个例子可以是A =(10000,50),B =(40,50),C =(10000,50).
然后,我执行一些处理(如a + bc)以下是我用于产品的功能.
def cartesian_2d(arrays, out=None):
arrays = [np.asarray(x) for x in arrays]
dtype = arrays[0].dtype
n = np.prod([x.shape[0] for x in arrays])
if out is None:
out = np.empty([n, len(arrays), arrays[0].shape[1]], dtype=dtype)
m = n // arrays[0].shape[0]
out[:, 0] = np.repeat(arrays[0], m, axis=0)
if arrays[1:]:
cartesian_2d(arrays[1:], out=out[0:m, 1:, :])
for j in range(1, arrays[0].shape[0]):
out[j * m:(j + 1) * m, 1:] = out[0:m, 1:]
return out
a = [[ 0, -0.02], [1, -0.15]]
b …Run Code Online (Sandbox Code Playgroud) python numpy out-of-memory cartesian-product python-itertools
我有一个.csv包含3列.PersonX,PersonY和PersonZ.有7000行名称和不同的组合.我的目标是看看3对和3对是最高匹配.我无法在excel中找到能够实现这一目标的公式.我确定python能够与itertools组合,但我还没那么高级.名称可以是任何顺序,只是想看看那些2或3个人在同一行中的次数.任何建议都会有很大的帮助,谢谢!
数据的小例子.
PersonX PersonY PersonZ
Aaron Ekblad Keith Yandle Vincent Trocheck
Aaron Ekblad Denis Malgin Mike Matheson
Aaron Ekblad Denis Malgin Mike Matheson
Aaron Ekblad Jonathan Huberdeau Keith Yandle
Aaron Ekblad Jonathan Huberdeau Keith Yandle
Aaron Ekblad Jamie McGinn Keith Yandle
Aaron Ekblad Aleksander Barkov Jonathan Huberdeau
Aaron Ekblad
Adam Erne Andrej Sustr Vladislav Namestnikov
Adam Erne Anthony Cirelli
Adam Erne
Adam Henrique Rickard Rakell Ryan Getzlaf
Adam Henrique Brandon Montour Ryan Getzlaf
Adam Henrique Corey Perry …Run Code Online (Sandbox Code Playgroud) 我想创建一个范围(例如(1,5))的数字与一些重复(例如4):
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
Run Code Online (Sandbox Code Playgroud)
一种方法是写:
list(itertools.chain(*([x] * 4 for x in range(1, 5))))
Run Code Online (Sandbox Code Playgroud)
或类似地:
list(itertools.chain(*(itertools.repeat(x, 4) for x in range(1, 5))))
Run Code Online (Sandbox Code Playgroud)
但是,有一个平坦的步骤,可以避免.
是否有更多的pythonic或更紧凑的版本来生成这样的序列?
我的代码使用如下列表的笛卡尔积:
import itertools
cartesian_product = itertools.product(list('ABCDEF'), repeat=n)
Run Code Online (Sandbox Code Playgroud)
n可以是 0 到 4 之间的任意值。
numba目前不支持itertools.product. 到目前为止我还无法想出一个可行的替代方案。欢迎任何建议!