我正在尝试使用Python.我想在几个列表(L [i])中切片列表(高原),但我有以下错误消息:
File "C:\Users\adescamp\Skycraper\skycraper.py", line 20, in <module>
item = plateau[debut:fin]
TypeError: slice indices must be integers or None or have an __index__ method
Run Code Online (Sandbox Code Playgroud)
有关线是与之相关的 item = plateau[debut:fin]
from math import sqrt
plateau = [2, 3, 1, 4, 1, 4, 2, 3, 4, 1, 3, 2, 3, 2, 4, 1]
taille = sqrt(len(plateau))
# Division en lignes
L = []
i = 1
while i < taille:
fin = i * taille
debut = fin - taille
item = plateau[debut:fin] …Run Code Online (Sandbox Code Playgroud) 我有一块结构.
type Config struct {
Key string
Value string
}
// I form a slice of the above struct
var myconfig []Config
// unmarshal a response body into the above slice
if err := json.Unmarshal(respbody, &myconfig); err != nil {
panic(err)
}
fmt.Println(config)
Run Code Online (Sandbox Code Playgroud)
这是这个的输出:
[{key1 test} {web/key1 test2}]
Run Code Online (Sandbox Code Playgroud)
如何搜索此数组以获取元素在哪里key="key1"?
我是Go语言的新手.我发现自己array与slice数据类型混淆了.
从Go docs,数组描述如下:
数组在Go和C中的工作方式有很大差异.在Go中,
- 数组是值.将一个数组分配给另一个数组会复制所有元素.
- 特别是,如果将数组传递给函数,它将接收数组的副本,而不是指向它的指针.
- 数组的大小是其类型的一部分.类型[10] int和[20] int是不同的.
功能:
与C系列中的所有语言一样,Go中的所有内容都按值传递.也就是说,函数总是获取正在传递的东西的副本,就好像有一个赋值语句将值赋给参数.例如,将int值传递给函数会生成int的副本,并且传递指针值会生成指针的副本,但不会生成它指向的数据.
sort.Ints(arrayValue)当我将其声明为数组而不是切片时,为什么要修改传递的变量?
码
var av = []int{1,5,2,3,7}
fmt.Println(av)
sort.Ints(av)
fmt.Println(av)
return
Run Code Online (Sandbox Code Playgroud)
产量
[1 5 2 3 7]
[1 2 3 5 7]
Run Code Online (Sandbox Code Playgroud) 我通常del在我的代码中使用删除对象:
>>> array = [4, 6, 7, 'hello', 8]
>>> del(array[array.index('hello')])
>>> array
[4, 6, 7, 8]
>>>
Run Code Online (Sandbox Code Playgroud)
但我听到很多人说使用del是unpythonic.使用del不好的做法?
>>> array = [4, 6, 7, 'hello', 8]
>>> array[array.index('hello'):array.index('hello')+1] = ''
>>> array
[4, 6, 7, 8]
>>>
Run Code Online (Sandbox Code Playgroud)
如果没有,为什么有很多方法可以在python中完成同样的事情?一个比其他人好吗?
选项1:使用 del
>>> arr = [5, 7, 2, 3]
>>> del(arr[1])
>>> arr
[5, 2, 3]
>>>
Run Code Online (Sandbox Code Playgroud)
选项2:使用 list.remove()
>>> arr = [5, 7, 2, 3]
>>> arr.remove(7)
>>> arr
[5, 2, 3] …Run Code Online (Sandbox Code Playgroud) 我想要做的是获取一个字符串,this.those.that并从第n次出现的字符中获取子字符串.所以,从字符串的开头到第二次出现.就会返回this.those.同样,从第二次出现.到字符串结尾将返回that.对不起,如果我的问题有雾,那就不容易解释了.另外,请不要建议制作额外的变量,结果将是字符串而不是数组.
我正在寻找一种方法来轻松确定列表中的所有非None项是否出现在单个连续切片中. 我将使用整数作为非None项的示例.
例如,该列表[None, None, 1, 2, 3, None, None]符合我对连续整数条目的要求.与此相反,[1, 2, None, None, 3, None]是不连续的,因为有整数之间无条目.
还有一些例子可以说明这一点.
连续:
[1, 2, 3, None, None]
[None, None, 1, 2, 3]
[None, 1, 2, 3, None]
不连续:
[None, 1, None, 2, None, 3]
[None, None, 1, None, 2, 3]
[1, 2, None, 3, None, None]
我的第一种方法是使用变量来跟踪我们是否遇到了一个问题None,以及我们是否遇到了一个问题int- 这最终导致了一个高度嵌套且非常难以遵循的if/else系列嵌入在for循环中的语句.(除了丑陋之外,我承认在每种情况下都没有让它工作).
任何人都知道一种更简单的方法来确定列表中的非None项是否出现在单个连续切片中?
我有以下清单
bar = ['a','b','c','x','y','z']
Run Code Online (Sandbox Code Playgroud)
我想要做的就是分配1,第4和第5的值bar到v1,v2,v3,有没有更紧凑的方式比这做的事:
v1, v2, v3 = [bar[0], bar[3], bar[4]]
Run Code Online (Sandbox Code Playgroud)
因为在Perl中你可以这样做:
my($v1, $v2, $v3) = @bar[0,3,4];
Run Code Online (Sandbox Code Playgroud) 我有一个片段: Keys []* datastore.Key
我怎么能在模板文件中索引其中一个?我猜{{.Keys[3] }},但这不起作用,我搜索了很多,但没有任何线索.
任何建议都会受到欢迎,谢谢.
我在弄清楚如何切片python列表时遇到了一些麻烦,如下图所示:
>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
据我所知,python slice意味着lst [start:end],包括start,排除end.那么我如何从元素n开始寻找列表的"休息"呢?
非常感谢您的帮助!
slice ×10
python ×6
list ×4
go ×2
arrays ×1
func ×1
go-templates ×1
javascript ×1
mutable ×1
null ×1
sequence ×1
side-effects ×1
string ×1
struct ×1
substring ×1