我有一套像(669256.02,6117662.09,669258.61,6117664.39,669258.05,6117665.08),我需要迭代,像
for x,y in (669256.02, 6117662.09, 669258.61, 6117664.39, 669258.05, 6117665.08)
print (x,y)
Run Code Online (Sandbox Code Playgroud)
哪个会打印
669256.02 6117662.09
669258.61 6117664.39
669258.05 6117665.08
Run Code Online (Sandbox Code Playgroud)
我在Python 3.3顺便说一句
(这个问题与这一个和这个问题有关,但那些是预先走动的发电机,这正是我想要避免的)
我想把一个发电机拆分成块.要求是:
我试过以下代码:
def head(iterable, max=10):
for cnt, el in enumerate(iterable):
yield el
if cnt >= max:
break
def chunks(iterable, size=10):
i = iter(iterable)
while True:
yield head(i, size)
# Sample generator: the real data is much more complex, and expensive to compute
els = xrange(7)
for n, chunk in enumerate(chunks(els, 3)):
for el in chunk:
print 'Chunk %3d, value %d' % (n, el)
Run Code Online (Sandbox Code Playgroud)
这有点工作:
Chunk 0, value 0
Chunk 0, value 1 …Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码,一次取一个巨大的文本文件(几GB)N行,处理该批处理,并移动到下一行N行,直到我完成整个文件.(我不在乎最后一批是不是完美的尺寸).
我一直在阅读有关使用itertools islice进行此操作的信息.我想我在那里:
from itertools import islice
N = 16
infile = open("my_very_large_text_file", "r")
lines_gen = islice(infile, N)
for lines in lines_gen:
...process my lines...
Run Code Online (Sandbox Code Playgroud)
麻烦的是我想处理下一批16行,但我遗漏了一些东西
我有一个大型数据帧(几百万行).
我希望能够对它进行groupby操作,但只需按任意连续(最好是相等大小)的行子集进行分组,而不是使用各行的任何特定属性来决定它们去哪个组.
用例:我想通过IPython中的并行映射将函数应用于每一行.哪个行转到哪个后端引擎并不重要,因为该函数一次基于一行计算结果.(从概念上讲,至少;实际上它是矢量化的.)
我想出了这样的事情:
# Generate a number from 0-9 for each row, indicating which tenth of the DF it belongs to
max_idx = dataframe.index.max()
tenths = ((10 * dataframe.index) / (1 + max_idx)).astype(np.uint32)
# Use this value to perform a groupby, yielding 10 consecutive chunks
groups = [g[1] for g in dataframe.groupby(tenths)]
# Process chunks in parallel
results = dview.map_sync(my_function, groups)
Run Code Online (Sandbox Code Playgroud)
但这似乎很啰嗦,并不能保证大小相等.特别是如果索引是稀疏的或非整数的或其他什么.
有什么更好的方法吗?
谢谢!
假设你有这个字符串:
ABCDEFGH
Run Code Online (Sandbox Code Playgroud)
你想要扭转它,使它成为:
GHEFCDAB
Run Code Online (Sandbox Code Playgroud)
什么是最有效/ pythonic解决方案?我尝试了一些不同的东西,但它们看起来都很糟糕......
提前致谢!
更新:
如果有人感兴趣,这不是为了做作业.我有一个脚本处理来自网络捕获的数据并将其作为一个十六进制字节字符串返回.问题是数据仍处于网络状态.由于应用程序的编写方式,我不想回过头来尝试使用say socket.htons,我只想反转字符串.
不幸的是,我的尝试似乎很可怕,我知道必须有一个更好的方法(一个更加pythonic的解决方案) - 因此我的问题在这里.
今天早些时候,我需要一次迭代一个字符串2个字符来解析格式化的字符串"+c-R+D-E"(有一些额外的字母).
我最终得到了这个,但它看起来很难看.我最后评论它正在做什么,因为它感觉不明显.它几乎似乎是pythonic,但并不完全.
# Might not be exact, but you get the idea, use the step
# parameter of range() and slicing to grab 2 chars at a time
s = "+c-R+D-e"
for op, code in (s[i:i+2] for i in range(0, len(s), 2)):
print op, code
Run Code Online (Sandbox Code Playgroud)
有没有更好/更清洁的方法来做到这一点?
我正在尝试用Python编写Haskel函数'splitEvery'.这是它的定义:
splitEvery :: Int -> [e] -> [[e]]
@'splitEvery' n@ splits a list into length-n pieces. The last
piece will be shorter if @n@ does not evenly divide the length of
the list.
Run Code Online (Sandbox Code Playgroud)
这个的基本版本工作正常,但我想要一个适用于生成器表达式,列表和迭代器的版本.并且,如果有一个发电机作为输入,它应该返回一个发电机作为输出!
# should not enter infinite loop with generators or lists
splitEvery(itertools.count(), 10)
splitEvery(range(1000), 10)
# last piece must be shorter if n does not evenly divide
assert splitEvery(5, range(9)) == [[0, 1, 2, 3, 4], [5, 6, 7, 8]]
# should give same …Run Code Online (Sandbox Code Playgroud) 我正在阅读一些PNG数据,每个像素有4个通道.我想一次迭代数据1个像素(意味着每4个元素= 1个像素,rgba).
red_channel = 0
while red_channel < len(raw_png_data):
green_channel, blue_channel, alpha_channel = red_channel +1, red_channel +2, red_channel +3
# do something with my 4 channels of pixel data ... raw_png_data[red_channel] etc
red_channel += 4
Run Code Online (Sandbox Code Playgroud)
这种方式看起来并不"正确".是否有更多的Pythonic方法迭代序列,一次4个项目,并将这4个项目解压缩?
我需要一次读取最多N行读取一个大文件,直到EOF.在Python中最有效的方法是什么?就像是:
with open(filename, 'r') as infile:
while not EOF:
lines = [get next N lines]
process(lines)
Run Code Online (Sandbox Code Playgroud) 我想要一个算法迭代列表切片.切片大小设置在功能之外,可以有所不同.
在我看来它是这样的:
for list_of_x_items in fatherList:
foo(list_of_x_items)
Run Code Online (Sandbox Code Playgroud)
有没有办法正确定义list_of_x_items或使用python 2.5执行此操作的其他方法?
edit1:澄清 "分区"和"滑动窗口"这两个术语听起来都适用于我的任务,但我不是专家.所以我会更深入地解释这个问题并添加到问题中:
fatherList是我从文件中获取的多级numpy.array.函数必须找到系列的平均值(用户提供系列的长度)平均我正在使用该mean()函数.现在进行问题扩展:
edit2:如何修改你提供的函数来存储额外的项目,并在下一个fatherList被输入函数时使用它们?
例如,如果列表长度为10且块的大小为3,则列表的第10个成员将被存储并附加到下一个列表的开头.