Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中迭代到第n个值_Python_Matlab - Fatal编程技术网

在python中迭代到第n个值

在python中迭代到第n个值,python,matlab,Python,Matlab,如何在python中将for循环迭代为1到特定值 我可以在python中迭代列表,如: for x in l: print x 但是。 如果我想从1迭代到数字th,在matlab中,我将执行以下操作: str = "abcd" for i=1:z for j=1:y if s(:,i)==s(j,:)' Seq(i)=str(j); end end end 如何在python中迭代这些项?使用切片表示法这将

如何在python中将for循环迭代为1到特定值

我可以在python中迭代列表,如:

for x in l:
    print x
但是。 如果我想从1迭代到数字th,在matlab中,我将执行以下操作:

str = "abcd"  
for i=1:z
    for j=1:y
        if  s(:,i)==s(j,:)'
            Seq(i)=str(j);
        end
    end
end

如何在python中迭代这些项?

使用切片表示法这将创建包含前n项的临时列表:

>>> s = "abcd"
>>> for x in s[:2]:
...     print(x)
...
a
b
或使用:


使用切片表示法创建包含前n个项目的临时列表:

>>> s = "abcd"
>>> for x in s[:2]:
...     print(x)
...
a
b
或使用:


要访问列表中的值,请使用方括号与索引一起进行切片

for x in l[start:end]:
        print x
你有一篇关于切片符号的帖子

关于名单的另一个问题

例1:

myList = [1, 2, 3, 4]

for x in myList[:-1]:
    print x
输出:

1
2
3
2
3
例2:

myList = [1, 2, 3, 4]

for x in myList[1:3]:
    print x
输出:

1
2
3
2
3

要访问列表中的值,请使用方括号与索引一起进行切片

for x in l[start:end]:
        print x
你有一篇关于切片符号的帖子

关于名单的另一个问题

例1:

myList = [1, 2, 3, 4]

for x in myList[:-1]:
    print x
输出:

1
2
3
2
3
例2:

myList = [1, 2, 3, 4]

for x in myList[1:3]:
    print x
输出:

1
2
3
2
3


HTH

您需要习惯python中的切片思想,请参见

非切片:

a = [1,2,3,4,5,6,7,8]
n = 5

for i in range(n):
  print a[i]
切片:

a = [1,2,3,4,5,6,7,8]
n = 5

print a[:n]

您需要习惯python中的切片思想,请参见

非切片:

a = [1,2,3,4,5,6,7,8]
n = 5

for i in range(n):
  print a[i]
切片:

a = [1,2,3,4,5,6,7,8]
n = 5

print a[:n]