slice

Python: how to slice a dictionary based on the values of its keys?

大城市里の小女人 提交于 2021-02-07 12:38:49
问题 Say I have a dictionary built like this: d={0:1, 1:2, 2:3, 10:4, 11:5, 12:6, 100:7, 101:8, 102:9, 200:10, 201:11, 202:12} and I want to create a subdictionary d1 by slicing d in such a way that d1 contains the following keys: 0, 1, 2, 100, 101, 102 . The final output should be: d1={0:1, 1:2, 2:3, 100:7, 101:8, 102:9} Is there an efficient Pythonic way of doing this, given that my real dictionary contains over 2,000,000 items? I think this question applies to all cases where keys are integers,

Difference between a[:] = b and a = b[:]? (Python)

会有一股神秘感。 提交于 2021-02-07 01:54:44
问题 I was asked this for a coding test and didn't know the answer. Anyone have any ideas? 回答1: [:] is the slice operator. When it's on the left side, it overwrites the contents of the list without creating a new reference. When it's on the right side, it creates a copy of the list with the same contents. 回答2: a = b[:] calls either __getslice__ or __getitem__ on b and assigns the result to a . In nearly all cases (e.g. lists, tuples, and other sequence types), this makes a shallow copy of the

Difference between a[:] = b and a = b[:]? (Python)

白昼怎懂夜的黑 提交于 2021-02-07 01:53:23
问题 I was asked this for a coding test and didn't know the answer. Anyone have any ideas? 回答1: [:] is the slice operator. When it's on the left side, it overwrites the contents of the list without creating a new reference. When it's on the right side, it creates a copy of the list with the same contents. 回答2: a = b[:] calls either __getslice__ or __getitem__ on b and assigns the result to a . In nearly all cases (e.g. lists, tuples, and other sequence types), this makes a shallow copy of the

Difference between a[:] = b and a = b[:]? (Python)

我与影子孤独终老i 提交于 2021-02-07 01:53:22
问题 I was asked this for a coding test and didn't know the answer. Anyone have any ideas? 回答1: [:] is the slice operator. When it's on the left side, it overwrites the contents of the list without creating a new reference. When it's on the right side, it creates a copy of the list with the same contents. 回答2: a = b[:] calls either __getslice__ or __getitem__ on b and assigns the result to a . In nearly all cases (e.g. lists, tuples, and other sequence types), this makes a shallow copy of the

Polygon infill algorithm

不羁岁月 提交于 2021-02-05 20:36:24
问题 I'm working on mesh slicing utility for 3d printing purposes. In general it should slice a 3d mesh model into 2d shapes (a number of polygons, probably with holes) and fill them with paths of determined thickness using a specific pattern. These paths will be used to generate a gcode commands for a 3d printer firmware. There are various open source tools with same purposes, written on python and perl. But my goal is to understand the workflow of slicer and to write my own tool in C or C++. So

Ranging over map keys of array type and slicing each array gives the same array for each iteration

强颜欢笑 提交于 2021-02-05 12:16:34
问题 When trying to add int array keys of a map to a slice of int slices, ranging and using arr[:] to slice array doesn't work as expected. The resultant slice contains only duplicates of the "first" key in the map(commented out for loop). However, copying the array key to another variable and slicing the new variable works, and the resultant slice contains distinct map key values. I wonder why the copying is necessary. Isn't k , the array key, copied from the map as a new array at each iteration?

Confused about append() behavior on slices

大兔子大兔子 提交于 2021-02-05 12:15:57
问题 func main() { slice := make([]int, 10, 10) slice[0] = 0 slice[1] = 1 slice1 := slice slice1[0] = 10000 fmt.Println(slice) slice1 = append(slice1, 100) slice1[0] = 20000 fmt.Println(slice) } result: [10000 1 0 0 0 0 0 0 0 0] [10000 1 0 0 0 0 0 0 0 0] In my understanding, slice is a pointer, slice1 and slice point to the same array, and the first output also proves this. But why did slice 's value remain unchanged after the append operation changed slice1 ? 回答1: The append() didn't change

Ranging over map keys of array type and slicing each array gives the same array for each iteration

两盒软妹~` 提交于 2021-02-05 12:14:17
问题 When trying to add int array keys of a map to a slice of int slices, ranging and using arr[:] to slice array doesn't work as expected. The resultant slice contains only duplicates of the "first" key in the map(commented out for loop). However, copying the array key to another variable and slicing the new variable works, and the resultant slice contains distinct map key values. I wonder why the copying is necessary. Isn't k , the array key, copied from the map as a new array at each iteration?

Confused about append() behavior on slices

只谈情不闲聊 提交于 2021-02-05 12:12:22
问题 func main() { slice := make([]int, 10, 10) slice[0] = 0 slice[1] = 1 slice1 := slice slice1[0] = 10000 fmt.Println(slice) slice1 = append(slice1, 100) slice1[0] = 20000 fmt.Println(slice) } result: [10000 1 0 0 0 0 0 0 0 0] [10000 1 0 0 0 0 0 0 0 0] In my understanding, slice is a pointer, slice1 and slice point to the same array, and the first output also proves this. But why did slice 's value remain unchanged after the append operation changed slice1 ? 回答1: The append() didn't change

Swift : what is the right way to split up a [String] resulting in a [[String]] with a given example?

夙愿已清 提交于 2021-02-05 09:23:10
问题 Starting with a large [String] and a given subarray size, what is the best way I could go about splitting up this array into smaller arrays? (The last array will be smaller than the given subarray size). Concrete example: Split up ["1","2","3","4","5","6","7","8","9"] with max split size 4 The code would produce [["1","2","3","4"],["4","5","6","7"],["7","8","9"]] Obviously I could do this a little more manually, but I feel like in swift something like map() or reduce() may do what I want