用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
满意答案
编辑:
否则,如果你从给定的字符串开始,那么你应该像这样简单地切片:
a = a[1::2]
- 第一个元素
1
表示您想要从第二个元素开始切片元素。- 最后一个元素
2
是间隔(每两个元素)- 空的中间元素实际上是指数上限(独占)。 如果你不放任何东西意味着你想要所有物品。
原版的:
请参考您之前的问题:
从有
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]
- First element
1
means you want to slice the element starting from its second element.- The last element
2
is the interval (every two element)- 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::]
(index1
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']
相关问答
更多按特定月份切片(Slicing by specific month)
动态Python数组切片(Dynamic Python Array Slicing)
Python垂直阵列切片(Python Vertical Array Slicing)
Python切片不会更改切片数组的值(Python slicing does not change the sliced array's value)
Python数组切片 - 如何实现二维数组切片?(Python array slicing — How can 2D array slicing be implemented?)
如何在Python中使用切片来选择其他的(How to select every other using slicing in Python)
Python非线性切片(Python non-linear slicing)
Python列表切片(Python list slicing)
反向切片Python(Slicing Python in Reverse)
在PHP中切片数组(Slicing arrays in PHP)
相关文章
更多A Great List of Windows Tools
Python 列表(list)操作
Becoming a data scientist
源码解读Mybatis List列表In查询实现的注意事项
How to Start a Business in 10 Days
hibernate 对list修改
Drupal Forums instead of phpBB or vBulletin: A casestudy
FreeMarker集合(List、Map)
Become a Master Designer: Rule Three: Contrast, Contrast, Contrast
[转]So You Want To Be A Producer
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!