Python列表切片(Python list slicing)
我无法理解在这里做什么。 有人可以帮忙吗
我有几个列表:
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 = []
slice
是我需要从切片array
获得的索引列表。interval
是当slice[i] is interval[j]
时用于停止切片的索引列表,其中i和j是循环变量。 我需要根据slice[i] is not interval[j]
时的条件,根据slice
和intervals
从array
形成一个列表列表intermediate =intermediate + array[slice[i]:slice[i+1]+1]
在我的情况下:
当
slice[i]
和interval[j]
相等于值12.所以我需要从array
形成一个列表列表intermediate = array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1]
这是
intermediate = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]
并且当
slice[i] is interval[j]
output = output + intermediate
并且继续切片时。output = output + [intermediate]
这是
output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]
现在,间隔中的下一个值是17,所以直到我们在
slice
有17个,我们从array[slice[6]:slice[6+1]+1]
形成另一个列表并将其添加到输出中。 这继续下去。最终输出应该是:
output = [array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1] , array[slice[6]:slice[6+1]+1], array[slice[8]:slice[8+1]+1]]
这是
output = [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
I'm not able understand what to do here. Can someone help.
I've a few lists:
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 = []
slice
is a list of indices I need to get from slicingarray
.interval
is a list of indices used to stop the slicing whenslice[i] is interval[j]
where i and j are looping variables. I need to form a list of lists fromarray
based onslice
andintervals
based on the condition that whenslice[i] is not interval[j]
intermediate =intermediate + array[slice[i]:slice[i+1]+1]
here in my case:
when
slice[i]
andinterval[j]
are equal for value 12. So I need to form a list of lists fromarray
intermediate = array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1]
which is
intermediate = array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]
and when
slice[i] is interval[j]
output = output + intermediate
and the slicing is continued.output = output + [intermediate]
which is
output = output + [array[2:(4+1)] + array[6:(8+1)] + array[10:(12+1)]]
now the next value in interval is 17 so till we have 17 in
slice
we form another list fromarray[slice[6]:slice[6+1]+1]
and add this to the output. This continues.The final output should be:
output = [array[slice[0]:slice[0+1]+1] + array[slice[2]:slice[2+1]+1] + array[slice[4]:slice[4+1]+1] , array[slice[6]:slice[6+1]+1], array[slice[8]:slice[8+1]+1]]
which is
output = [[2, 3, 4, 5, 6, 7, 8, 9, 10], [12, 13, 14], [15, 16, 17]]
原文:https://stackoverflow.com/questions/39187018
最满意答案
您可以在工厂函数中创建
TaggedFun
实例:interface TaggedFun { (): void; tag: boolean; } function taggedFactory(tag: boolean, fn?: () => void): TaggedFun { let tagged = (fn || function () { // function body here }) as TaggedFun; tagged.tag = tag; return tagged; }
这样,您始终知道您拥有
tag
属性(如果使用工厂创建实例),并且您获得了类型安全性。
例如:function doSomething(tagged: TaggedFun) {} doSomething(() => { }); // error doSomething(taggedFactory(true)); // ok doSomething(taggedFactory(true, () => { })); // ok
( 游乐场代码 )
You can create instances of
TaggedFun
in a factory function:interface TaggedFun { (): void; tag: boolean; } function taggedFactory(tag: boolean, fn?: () => void): TaggedFun { let tagged = (fn || function () { // function body here }) as TaggedFun; tagged.tag = tag; return tagged; }
This way you always know that you have the
tag
property (if you create the instance using the factory), and you get type safety.
For example:function doSomething(tagged: TaggedFun) {} doSomething(() => { }); // error doSomething(taggedFactory(true)); // ok doSomething(taggedFactory(true, () => { })); // ok
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这两者之间有区别吗? 是。 func: (arg:string) => number; 此版本表示其属性。 当您尝试声明overloads时,这将限制您。 func2(arg:string): number; 这对于函数来说是首选,因为这意味着您可以在事实之后轻松声明重载(使用接口的开放性质) 似乎暗示这两者可以互换使用 那是因为它们是类型兼容的 。 并不意味着它们是同一个东西 。 请参阅以下属性与方法: 例 这应该澄清: interface Thing { func: (arg: string) ...
-
TypeScript:声明一个名为“new”的函数(TypeScript: declare a variable that is a function named “new”)[2022-12-25]
在某些情况下,您可以引用标识符名称: interface MyModule { "new"(name: string, options: Options): NewService; } declare const m: MyModule; export = m; In certain contexts you can quote identifier names: interface MyModule { "new"(name: string, options: Options): Ne ... -
你应该: 1)安装插件: npm i -S icheck 2)将引用复制到angular-cli.json: "scripts": ["../node_modules/icheck/icheck.js"] 3)typings.d.ts: declare ... function or what you need from icheck You should: 1) install the plugin: npm i -S icheck 2) copy the reference to angul ...
-
在Typescript中,我该如何声明一个返回字符串类型数组的函数?(In Typescript how do I declare a function that return string type array?)[2021-10-13]
你的错误是因为你正在返回推送方法的值。 push方法返回数组的新长度,这就是为什么它试图将一个数字转换为一个字符串数组。 所以,你应该做的是这样的: citiesData():string[] { this._citiesData.push('18-HAZARI','A.K','ABBOTABAD'); return this._citiesData; } Your error is because you're returning the value of the push method ... -
要引用在另一个包中声明的类型,您需要将它们导入到您希望使用它们的.ts或.d.ts文件中。 添加以下导入应该可以解决您的问题: import { AxiosResponse } from "axios"; To reference types declared in another package, you will need to import them into the .ts or .d.ts file in which you wish to use them. Adding the follow ...
-
我可以使用什么TypeScript模式来强制函数获取属性?(What TypeScript pattern can I use to enforce that a function gets a property?)[2023-09-14]
我认为你需要在TypeScript中包含面向对象并创建一个包含属性和函数的类。 在您的示例中组合函数和属性是有效的JavaScript,但如果您正在进入TypeScript,您也可以完全沉浸其中。 class MyClass { constructor(public myProp: string) { } myFunc(): string{ return this.myProp; } } 更新 免责声明:我不建议这样做 - 正如我所说,我建议使用TypeS ... -
做你正在做的事情很好,但我认为将数据库注入构造并将id引入验证更有意义。 创建setId()也很有价值: class item { protected $id, $db; # Inject the $db instead public function __construct(db $db) { $this->db = $db; } ...
-
TypeScript:在函数上声明一个具有必需属性的函数(TypeScript: declare a function with a required property on the function)[2021-12-27]
您可以在工厂函数中创建TaggedFun实例: interface TaggedFun { (): void; tag: boolean; } function taggedFactory(tag: boolean, fn?: () => void): TaggedFun { let tagged = (fn || function () { // function body here }) as TaggedFun; tagged.tag = t ...