简体   繁体   English

Python数据[i]到数据[i + 100] <#(例如50)

[英]Python data[i] to data[i+100] < # (eg. 50)

I have a list containing a large amount of data. 我有一个包含大量数据的列表。 If I want to set a if statement as the pseudo code below: 如果我想将if语句设置为下面的伪代码:

if data[i] to data[i+100] < 50:
    data[i] == 1

Is there any way that I don't need to type something like below? 我有什么办法不需要键入以下内容?

data[i] <50 and data[i+1] <50 and data[i+2] <50 and .... and data[i+100] <50: 

Because it'd just be too time consuming. 因为那太浪费时间了。 If anyone knows the faster way, pls let me know. 如果有人知道更快的方法,请告诉我。 Appreciated!! 赞赏!

Yes, you can use all(..) for that over a slicing of the list: 是的,您可以在列表的切片上使用all(..)

if all(x < 50 for x in data[i:i+101]):
    data[i] = 1 # probably you want assignment?

You have to write i:i+101 as slicing (and not i:i+100 ), since the upper bound is exclusive . 您必须将i:i+101写为切片(而不是i:i+100 ),因为上限是互斥的

Or you can use range(..) and save on making a slice-copy of the list: 或者,您可以使用range(..)并保存列表的切片副本:

if all(data[j] < 50 for j in range(i,i+101)):
    data[i] = 1 # probably you want assignment?

Note that although you do not type all these conditions separately, of course Python will still perform iteration (and evaluate at most 101 such expressions). 请注意,尽管您没有单独键入所有这些条件,但是Python当然仍然会执行迭代(并最多计算 101个这样的表达式)。

Sure. 当然。 You can map over the values, checking your requirement, and then can use all to ensure that they meet it: 您可以map这些值,检查您的要求,然后可以使用all来确保它们满足要求:

>>> data = range(1000)
>>> all(map(lambda i: i < 50, data[:100]))
# => True

To break down whats going on: 要分解正在发生的事情:

  • data[:101] gets a slice of the first 101 items in the array data[:101]获取数组中前101个项目的一部分
  • map iterates over those items and returns a boolean if passed our check i < 50 . map遍历这些项目,如果通过我们的检查i < 50 ,则返回一个布尔值。 ie. 即。 the resulting list is [True, True, True, ...] 结果列表为[True, True, True, ...]
  • all then checks that every value is True all然后检查每个值为True

Then you can replace the items with some smart slicing: 然后,您可以使用一些智能切片来替换项目:

if all(map(lambda i: i < 50, data[:100])):
     data = [1] * 100 + data[100:]

是。

if all(data[i+k] < 50 for k in range(0,101)):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 读取 excel 上的数据,然后将单元格等同于不同的变量,例如。 x、y、z 等 - How can I use python to read data on excel and then equate the cells to different variables eg. x, y, z etc 我应该如何在Python中包装一个交互式子进程(例如shell) - How should I wrap an interactive subprocess (eg. shell) in Python 如何传递数据以初始化 package,例如 python 中的记录器? - How do I pass data in to initialise a package with eg a logger in python? 此脚本提供所有用户数据(例如,给定输出),但我想获取特定用户的 uid、uidnumber、mail、employeenumber。 我怎么做? - This script gives all the users data (eg. given output) but I want to fetch specific user's uid, uidnumber, mail, employeenumber. How do I do that? 如何通过Python Script Mac在另一个应用程序中打开文档(例如.txt) - How do I open a document (eg. .txt) in another app from a Python Script mac 全局解释器锁定和访问数据(例如,对于NumPy阵列) - Global Interpreter Lock and access to data (eg. for NumPy arrays) 如何从一组图像(.png,.jpg,.bmp等)中检索所有.txt格式(例如255、255、255)的原始rgb数据 - How do I retrieve all of the raw rgb data in .txt format (eg. 255, 255, 255) from a set of images (.png, .jpg, .bmp, etc.) 如何以可移植的方式将tox指向所有python版本(例如,不对tox.ini中的路径进行硬编码)? - How can I point tox to all python versions in a portable way (eg. without hard-coding the paths in tox.ini)? 如何在python中的代码点上拆分unicode字符串? (例如\\ u00B7或\\ u2022)? - How do I split a unicode string on code points in python? (eg. \u00B7 or \u2022)? Python瓶:fileupload对象的模式为“ rb +”。 例如。 熊猫的read_csv方法,我需要将模式设置为“ rt” - Python bottle: fileupload object has mode 'rb+'. For eg. Pandas read_csv method, I need the mode to be 'rt'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM