由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问python的多重循环怎么实现?
相关主题
Python 缩进的syntax请问运行python的一些技巧。
有人用Haskell吗correct indentation in python ?
python的一大缺点python indentation
问一下python初学者的问题总觉得python 用缩进定义逻辑block 这种思想很诡异
Python for Data Analysispython有快速loop over dict的方法吗?
我来说说python的优点吧python自动缩进出错 快疯了
lisperindent C++ source code by VC++ 6.00
花了一个小时学习了python这个程序怎么解决
相关话题的讨论汇总
话题: range话题: str话题: python话题: cout
进入Programming版参与讨论
1 (共1页)
x**********d
发帖数: 693
1
类似C++里面的这段请问用python如何实现?
for (int i=0; i<5; i++){
a=a+i;
cout< for (int j=0; j<5; j++{
b=b+j;
cout< }
cout< }
d**o
发帖数: 864
2
for i in range(5):
a=a+i
for j in range(5):
b=b+j

【在 x**********d 的大作中提到】
: 类似C++里面的这段请问用python如何实现?
: for (int i=0; i<5; i++){
: a=a+i;
: cout<: for (int j=0; j<5; j++{
: b=b+j;
: cout<: }
: cout<: }

x**********d
发帖数: 693
3
事实上我现在有一个5x5的float32类型的array想按行输出到文本,然后我这么写的,
结果编译过不去,这是怎么一回事,赶脚是循环没写对?
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b(i,j)+' ')
f.write('n')
f.close()

【在 d**o 的大作中提到】
: for i in range(5):
: a=a+i
: for j in range(5):
: b=b+j

d**o
发帖数: 864
4
看错误信息。不知道你b(i,j)是什么东西,
但是str(b(i,j)少了一个)
然后f.write('\n')换行。

【在 x**********d 的大作中提到】
: 事实上我现在有一个5x5的float32类型的array想按行输出到文本,然后我这么写的,
: 结果编译过不去,这是怎么一回事,赶脚是循环没写对?
: f=open('testfile.txt','wb')
: for i in range(5):
: for j in range(5):
: f.write(str(b(i,j)+' ')
: f.write('n')
: f.close()

x**********d
发帖数: 693
5
b是这么个东西:
a_list =[1, 2, 3, 4, 5,
2, 3, 4, 5, 1,
3, 4, 5,1, 2,
4, 5, 1, 2, 3,
5, 1, 2, 3, 4,]
a_array = numpy.array(a_list,dtype='float32')
b=a_array.reshape(5,5)
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b(i,j))+' ')
f.write('\n') <--------红色波浪线指明错误在这里
f.close()
报错:
Indentation Error: unindent does not match outer indentation level
请问这个怎么破?

【在 d**o 的大作中提到】
: 看错误信息。不知道你b(i,j)是什么东西,
: 但是str(b(i,j)少了一个)
: 然后f.write('\n')换行。

f***c
发帖数: 338
6
should we access the array via [] instead of ()?
b(i,j) ---> b[i][j]
?
x**********d
发帖数: 693
7
哦。。可是换成b[i][j]依然不对,还是那个错。。

【在 f***c 的大作中提到】
: should we access the array via [] instead of ()?
: b(i,j) ---> b[i][j]
: ?

f***c
发帖数: 338
8
Indentation Error: unindent does not match outer indentation level
是不是应该再看看你的格式,我们知道python对indentation要求很严的。
good luck
x**********d
发帖数: 693
9
破了!貌似python 跳出一个循环要换一行。。就是这样
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b[i][j])+' ')

f.write('n')
f.close();
但是有个新问题:f.write('n')能够从j循环里跳出来了归i循环,但是f.close()那句
即便是回车换行还是归在i循环里面挑不出来,这是怎么回事???btw, 我用的python
tools在vs下面写的,按道理跟用python IDE写没区别吧?

【在 f***c 的大作中提到】
: Indentation Error: unindent does not match outer indentation level
: 是不是应该再看看你的格式,我们知道python对indentation要求很严的。
: good luck

f***c
发帖数: 338
10
从来没有听说过“貌似python 跳出一个循环要换一行",呵呵
不要用tab键。证明了你这里就是格式问题,仔细检查一下吧。
相关主题
我来说说python的优点吧请问运行python的一些技巧。
lispercorrect indentation in python ?
花了一个小时学习了pythonpython indentation
进入Programming版参与讨论
x**********d
发帖数: 693
11
哦!这也有点太严格了。。写成这样就对了
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b[i][j])+' ')
f.write('n')
f.close();
可是为什么文本里面只有空格没换行???

【在 f***c 的大作中提到】
: 从来没有听说过“貌似python 跳出一个循环要换一行",呵呵
: 不要用tab键。证明了你这里就是格式问题,仔细检查一下吧。

s*i
发帖数: 5025
12
\n

[发表自未名空间手机版 - m.mitbbs.com]

【在 x**********d 的大作中提到】
: 哦!这也有点太严格了。。写成这样就对了
: f=open('testfile.txt','wb')
: for i in range(5):
: for j in range(5):
: f.write(str(b[i][j])+' ')
: f.write('n')
: f.close();
: 可是为什么文本里面只有空格没换行???

S*********g
发帖数: 5298
13
f.write('n'.join([' '.join(map(str, bi)) for bi in b])

★ 发自iPhone App: ChineseWeb 7.8
★ 发自iPhone App: ChineseWeb 7.8

【在 x**********d 的大作中提到】
: 哦!这也有点太严格了。。写成这样就对了
: f=open('testfile.txt','wb')
: for i in range(5):
: for j in range(5):
: f.write(str(b[i][j])+' ')
: f.write('n')
: f.close();
: 可是为什么文本里面只有空格没换行???

x**********d
发帖数: 693
14
啊!我程序里面写的是\n。。。帖子里写错了。。可是文本里面它就是没有换行。。。
。请问什么原因?

【在 s*i 的大作中提到】
: \n
:
: [发表自未名空间手机版 - m.mitbbs.com]

p***o
发帖数: 1252
15
try '\r\n' instead of '\n'

【在 x**********d 的大作中提到】
: 啊!我程序里面写的是\n。。。帖子里写错了。。可是文本里面它就是没有换行。。。
: 。请问什么原因?

f***c
发帖数: 338
16
这是高手来了,羡慕。
try this f.write('n') should be in the i-loop.
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b[i][j])+' ')
f.write('\n')
f.close();

【在 S*********g 的大作中提到】
: f.write('n'.join([' '.join(map(str, bi)) for bi in b])
:
: ★ 发自iPhone App: ChineseWeb 7.8
: ★ 发自iPhone App: ChineseWeb 7.8

x**********d
发帖数: 693
17
@@ 这么复杂,貌似一句话就实现了。。我研究一下。。请问那个bi是是是是什么,怎
么得到的?

【在 S*********g 的大作中提到】
: f.write('n'.join([' '.join(map(str, bi)) for bi in b])
:
: ★ 发自iPhone App: ChineseWeb 7.8
: ★ 发自iPhone App: ChineseWeb 7.8

x**********d
发帖数: 693
18
o了!!就这么搞!python的这缩进什么的也太BT了。。感谢大家的回复~~
f=open('testfile.txt','wb')
for i in range(5):
for j in range(5):
f.write(str(b[i][j])+' ')
f.write('\r\n')
f.close();

【在 p***o 的大作中提到】
: try '\r\n' instead of '\n'
i***r
发帖数: 1035
19
你极有可能是用了 tab 和 space 混合,或者用了不同的文本编辑软件。
我一律用space,2个。听说4个space比较通用。。。。。
i***r
发帖数: 1035
20
你的loop是对的吧?
仔细检查indentation,全部换成一样的。
可以用python来读自己的script,然后每行看是不是tab 和 space混了

【在 x**********d 的大作中提到】
: b是这么个东西:
: a_list =[1, 2, 3, 4, 5,
: 2, 3, 4, 5, 1,
: 3, 4, 5,1, 2,
: 4, 5, 1, 2, 3,
: 5, 1, 2, 3, 4,]
: a_array = numpy.array(a_list,dtype='float32')
: b=a_array.reshape(5,5)
: f=open('testfile.txt','wb')
: for i in range(5):

x**********d
发帖数: 693
21
我就用的visual studio,我没用tab的哎。。。开始不对,后来空格回车乱试了一气,
突然好了。。但是还不知道是怎么好的。。。

【在 i***r 的大作中提到】
: 你极有可能是用了 tab 和 space 混合,或者用了不同的文本编辑软件。
: 我一律用space,2个。听说4个space比较通用。。。。。

1 (共1页)
进入Programming版参与讨论
相关主题
这个程序怎么解决Python for Data Analysis
我觉得学C的话还是K&R最好我来说说python的优点吧
总结一下kaggle比赛lisper
jun rao说kafka已经开始用Java代码重写部分code了花了一个小时学习了python
Python 缩进的syntax请问运行python的一些技巧。
有人用Haskell吗correct indentation in python ?
python的一大缺点python indentation
问一下python初学者的问题总觉得python 用缩进定义逻辑block 这种思想很诡异
相关话题的讨论汇总
话题: range话题: str话题: python话题: cout