首页 \ 问答 \ 用numpy制作一个范围列表(Make a list of ranges in numpy)

用numpy制作一个范围列表(Make a list of ranges in numpy)

我想列出一个随机起始点的整数序列列表。 我会在纯Python中执行此操作的方式是

x = np.zeros(1000, 10) # 1000 sequences of 10 elements each
starts = np.random.randint(1, 1000, 1000)
for i in range(len(x)):
    x[i] = np.arange(starts[i], starts[i] + 10)

我想知道是否有使用Numpy功能的更优雅的方式。


I want to make a list of integer sequences with random start points. The way I would do this in pure python is

x = np.zeros(1000, 10) # 1000 sequences of 10 elements each
starts = np.random.randint(1, 1000, 1000)
for i in range(len(x)):
    x[i] = np.arange(starts[i], starts[i] + 10)

I wonder if there is a more elegant way of doing this using Numpy functionality.


原文:https://stackoverflow.com/questions/39626041
更新时间:2022-04-22 07:04

最满意答案

编辑:

否则,如果你从给定的字符串开始,那么你应该像这样简单地切片:

a = a[1::2]
  1. 第一个元素1表示您想要从第二个元素开始切片元素。
  2. 最后一个元素2是间隔(每两个元素)
  3. 空的中间元素实际上是指数上限(独占)。 如果你不放任何东西意味着你想要所有物品。

原版的:

请参考您之前的问题:

如何使列表元素成为字符串?

从有a = [10, 20, 30]

为了从每个列表项目的第二个元素开始为给定的整数列表a获取连接字符串,可以使用[1::]从第二个元素开始将列表中的每个项目向前切片(索引1表示从第二个元素)是这样的:

a = [10, 20, 30]
a = [''.join(str(x)[1::] for x in a)]

print(a)

结果:

['000']

Edit:

Else, if you start from the given string, then you should simply slice it like this:

a = a[1::2]
  1. First element 1 means you want to slice the element starting from its second element.
  2. The last element 2 is the interval (every two element)
  3. The empty middle element is actually the upper index limit (exclusive). If you don't put anything means you want all items.

Original:

Refering from your previous question here:

How to make list elements into string?

Started from having a = [10, 20, 30],

In order to get the joined string for the given integer list a from the second element per list item onwards, you could slice each item of the list to from the second element onwards by using [1::] (index 1 means starting from the second element) like this:

a = [10, 20, 30]
a = [''.join(str(x)[1::] for x in a)]

print(a)

Result:

['000']

相关问答

更多
  • datetimeindex具有要访问的属性month ,您可以使用它来过滤df: In [132]: df = pd.DataFrame(index=pd.date_range(dt.datetime(2015,1,1), dt.datetime(2015,5,1))) df.loc[df.index.month==2] Out[132]: Empty DataFrame Columns: [] Index: [2015-02-01 00:00:00, 2015-02-02 00:00:00, 2015- ...
  • 您可以使用python的slice 自动 slice : >>> a = np.random.rand(3, 4, 5) >>> a[0, :, 0] array([ 0.48054702, 0.88728858, 0.83225113, 0.12491976]) >>> a[(0, slice(None), 0)] array([ 0.48054702, 0.88728858, 0.83225113, 0.12491976]) slice方法读取为slice(*start*, stop[, ...
  • my_list = [[1, A], [2, B], [3, C]] a, b = zip(*my_list) 请注意, a和b将最终成为元组。 my_list = [[1, A], [2, B], [3, C]] a, b = zip(*my_list) Note that a and b will end up being tuples.
  • +是一个函数,它返回一个新数组。 通过运行y + 100您运行了一个函数,该函数返回一个指向新数组的指针,该数组存储在y 。 如果你要运行y[0] = 5 , x也会改变。 在编辑中添加: +是numpy.add(y, 100)的隐式版本,广播100到[100, 100, 100, 100, 100, 100] numpy.add(y, 100) [100, 100, 100, 100, 100, 100] 。 然后它对两个数组求和,因此必须返回一个新数组。 numpy.add doc 另外,如评论中所述, ...
  • obj[,:3]是无效的Python,所以它会引发一个SyntaxError - 因此,你不能在你的源文件中使用该语法。 (当你尝试在numpy数组上使用它时也会失败) obj[,:3] is not valid python so it will raise a SyntaxError -- Therefore, you can't have that syntax in your source file. (It fails when you try to use it on a numpy array ...
  • 编辑: 否则,如果你从给定的字符串开始,那么你应该像这样简单地切片: a = a[1::2] 第一个元素1表示您想要从第二个元素开始切片元素。 最后一个元素2是间隔(每两个元素) 空的中间元素实际上是指数上限(独占)。 如果你不放任何东西意味着你想要所有物品。 原版的: 请参考您之前的问题: 如何使列表元素成为字符串? 从有a = [10, 20, 30] , 为了从每个列表项目的第二个元素开始为给定的整数列表a获取连接字符串,可以使用[1::]从第二个元素开始将列表中的每个项目向前切片(索引1表示从第二 ...
  • 编辑 :主要是我需要这个修改列表。 例如 a[*non-linear-slicing*] = [*list-with-new-values*] 大。 然后我们可以使用这个新值的列表来驱动这个事情。 演示: >>> a = range(17) >>> newvals = ['foo', 'bar', 'baz', 'qux'] >>> for i, val in enumerate(newvals): a[2**i] = val >>> a [0, 'foo', 'bar', 3, 'b ...
  • 一个简单的解决方案: array_ = [7,8,2,3,4,10,5,6,7,10,8,9,10,4,5,12,13,14,1,2,15,16,17] slice_ = [2, 4, 6, 8, 10, 12, 15, 17, 20, 22] intervals = [12, 17, 22] output = [] intermediate = [] for i in range(0, len(slice_), 2): intermediate.extend(array_[slice_[i]:s ...
  • 切片从第一个数字(包括)转到第二个数字(不包括),步骤为第三个数字。 在您的情况下,第一个数字访问“4”,第二个数字访问“2”。 步长为-1时,它将从第一个数字(“4”)位置的字符开始,然后获得下一个字符(或步长为-1,实际上是前一个字符“3”),但会停在那里,因为第二个索引(2)是独占的。 要获得从“4”到“0”的所有内容,您可以执行x[4:0:-1]或者更方便的是x[:5] 。 Slicing goes from the first number (inclusive) to the second nu ...
  • 这些行返回我想要的关联数组: $filter=array_filter($select); $new=array_intersect_key($arr,$filter); var_dump($new); These lines return the associative array that I want: $filter=array_filter($select); $new=array_intersect_key($arr,$filter); var_dump($new);

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)