小编Jia*_*eng的帖子

使用Python创建2D坐标

我是Python的新手.现在,我正在做一个涉及创建2D坐标列表的项目.坐标应均匀放置,使用方格(10*10),如(0,0)(0,1)(0,2)(0,3)......(0,10)(1,0) )(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10).

这是我的代码:

coordinate = []
x = 0
y = 0
while y < 10:
    while x < 10:
        coordinate.append((x,y))
        x += 1
    coordinate.append((x,y))
    y += 1
print(coordinate)
Run Code Online (Sandbox Code Playgroud)

但我只能得到:[(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),( 7,0),(8,0),(9,0),(10,0),(10,1),(10,2),(10,3),(10,4),(10, 5),(10,6),(10,7),(10,8),(10,9)]

如何重写我的代码以获得所有要点?

python

6
推荐指数
4
解决办法
4万
查看次数

在Python中,如何打印列表中的值

我想从列表中打印出某个值。这是我的代码:

rdf_f = open("substrate.txt")
for line in rdf_f:
    fields = line.split()
    if len(fields) > 1:
        x = fields[1]
    print(x[2])
Run Code Online (Sandbox Code Playgroud)

如何正确使用print()命令打印出 的第三个值x?因为我收到一个错误:

IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)

我知道x = [1,2,3,4,5,6]我的代码是否有效。但这里x是一个垂直的柱子。当我使用时print(x),输出是

0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
...
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
3159
查看次数

在 Python 中将类似 Fortran 的格式写入文件

在 Fortran 中,我可以使用:

open(10, file = 'output')
write(10, '(5A10)') 'apple', 'banana', 'orange', 'grape', 'berry'
Run Code Online (Sandbox Code Playgroud)

Python 中是否有类似的方式来编写输出?

我知道我可以使用:

f.write('%5s\t%5s\t%5s\t%5s\t%5s' % ('apple', 'banana', 'orange', 'grape', 'berry'))
Run Code Online (Sandbox Code Playgroud)

但怎样才能让它更紧凑呢?使用类似的方式'(5A10)',而不是冗长的'%5s\t%5s\t%5s\t%5s\t%5s'

python fortran

2
推荐指数
1
解决办法
4538
查看次数

标签 统计

python ×3

fortran ×1

python-3.x ×1