Python - 操作数组中的所有元素没有循环? [英] Python - Operations on all elements of an array without a loop?

查看:169
本文介绍了Python - 操作数组中的所有元素没有循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它将一个字符串列表(非常长的列表)加载到一个数组中。我需要对数组的每个元素执行几个操作。每个元素需要做三个操作:(1)计算字符串的长度;(2)用标量乘以;(3)取一个mod的值。

使用循环这非常简单,但是因为我的数组中有这么多元素,所以我想知道是否有更好的方法来做到这一点那简单的for循环。我需要它是快速的,循环通过数百万的元素似乎不是最有效的方式来管理这个。



有谁知道任何方式来优化在这种情况下使用 Python 的性能?
线程?还是有一个数组迭代器运算符,我可能没有听说过?

(我知道这听起来像一个家庭作业问题,但它不是,我向你保证。一个非常简化的版本,我需要完成)。

任何建议将不胜感激!
Thanks!

解决方案

必须用循环完成。 使用更快的循环



< pre $ n = [len(x)* k%m for x in S]


I have a python script which loads a list of strings (very long list) into an array. I need to perform several operations on each element of the array. There are three operations that I need to do on each element: (1) count the length of the string, (2) multiply that by a scalar, and (3), take a mod of that.

This is very-very simple to do using a loop, but because I have so many elements in my array, I am wondering if there is a better way to do this that a simple for loop. I need it to be fast, and looping through millions of elements doesn't seem like the most efficient way to manage this.

Does anyone know of any way to optimize the performance in this kind of scenario using Python? Threading? Or is there an array iterator operator that I might not have heard of?

(I know this sounds like a homework problem, but it's not, I assure you. It's just a very simplified version of what I need to accomplish).

Any advice would be most appreciated! Thanks!

解决方案

It must be done with a loop. Use a faster loop.

n = [len(x) * k % m for x in S]

这篇关于Python - 操作数组中的所有元素没有循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆