用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)[2021-09-30]
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数组切片(Dynamic Python Array Slicing)[2022-10-21]
您可以使用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[, ... -
Python垂直阵列切片(Python Vertical Array Slicing)[2022-09-01]
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 另外,如评论中所述, ...
-
Python数组切片 - 如何实现二维数组切片?(Python array slicing — How can 2D array slicing be implemented?)[2023-01-07]
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表示从第二 ...
-
Python非线性切片(Python non-linear slicing)[2021-05-02]
编辑 :主要是我需要这个修改列表。 例如 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 ... -
Python列表切片(Python list slicing)[2022-03-21]
一个简单的解决方案: 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 ... -
反向切片Python(Slicing Python in Reverse)[2023-10-16]
切片从第一个数字(包括)转到第二个数字(不包括),步骤为第三个数字。 在您的情况下,第一个数字访问“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 ... -
在PHP中切片数组(Slicing arrays in PHP)[2022-11-28]
这些行返回我想要的关联数组: $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);