Python 支持循环,它的语法与其他语言(如 JavaScript 或 Java)有些不同。下面的代码块演示如何在 Python 中使用for循环来遍历列表中的元素:
下面的这段代码可以通过行替换的行来指定打印的。你在打印的“,”,将输出显示在同一个打印的位置(“如果行显示”的多个字符),上面有一行代码表现:
当你的一行是多行文本中而不是,可以使用上述形式的代码提供了构建时函数。Python 重新显示了通过循环的内容:
注意,只有当对象的大小是确定的,或者对象实现了_reversed_()方法的时候反向的功能才有效。
01 使用tryexcept的for循环
清单 1 的 StringToNums 说明了如何对一组从字符串转换获得的求和。
- 清单1 StringToNums.py
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#397300">line</span> = <span style="color:#880000">'1 2 3 4 10e abc'</span>
sum = <span style="color:#880000">0</span>
invalidStr = <span style="color:#880000">"" </span>
<span style="color:#333333"><strong>print</strong></span> ( <span style="color:#880000">'String of numbers:'</span> , <span style="color:#397300">line</span> )
<span style="color:#333333"><strong>for</strong></span> str in <span style="color:#397300">line</span>。<span style="color:#333333"><strong>拆分</strong></span>(<span style="color:#880000">“”</span>):
<span style="color:#333333"><strong>尝试</strong></span>:
sumsum = sum + <span style="color:#397300">eval</span> (str) <span style="color:#bc6060">除了t:</span>
invalidStrinvalidStr
= invalidStr + str + <span style="color:#880000">' ' </span>
<span style="color:#333333"><strong>print</strong></span> ( <span style="color:#880000">'sum:'</span> , sum)
<span style="color:#333333"><strong>if</strong></span> (invalidStr != <span style="color:#880000">""</span> ):
<span style="color:#333333"><strong>print</strong></span> ( <span style="color:#880000">'无效字符串:'</span> ,invalidStr)
<span style="color:#333333"><strong>else</strong></span> :
<span style="color:#333333"><strong>print</strong></span> ( <span style="color:#880000">'所有子字符串都是有效数字'</span> )
</span></span>
11首先初始化变量linesum、invalidStr和invalidStr的内容。接下来将行中的内容分割为单词,然后通过变量尝试将块个个数的单数累加到sum中。当前str的内容附加到变量invalidStr。
当循环执行结束时,清单 1 打印出分数字的显示和,并在后面的非分数字。它的输出如下所示:
02指数成绩单
清单 2 说明了如何计算第 2 个。
- 清单2 Nth_exponet.py
<span style="color:#444444"><span style="background-color:#f6f6f6">maxPower = <span style="color:#880000">4</span>
maxCount = <span style="color:#880000">4 </span>
<span style="color:#333333"><strong>def </strong></span> <span style="color:#880000"><strong>pwr </strong></span>(num) :
prod = <span style="color:#880000">1 </span>
<span style="color:#333333"><strong>for</strong></span> n <span style="color:#333333"><strong>in</strong></span> range( <span style="color:#880000">1</span> ,maxPower+ <span style="color:#880000">1</span> ):
prodprod = prod*num
print(num, <span style="color:#880000">'to the power'</span> ,n, <span style="color:#880000">'equals'</span> ,prod)
print( <span style="color:#880000">'-----------'</span> )
<span style="color:#333333"><strong>for</strong></span> num <span style="color:#333333"><strong>in</strong></span> range( <span style="color:#880000">1</span> ,maxCount+ <span style="color:#880000">1</span> ):
功率(数)
</span></span>
清单 2 中有一个 pwr 函数,其参数为一个分数。这个函数的循环中可打印出参数的 1 到 n 次方,n 的取值范围在 1 到 maxCount+1 之间。
代码的第二部分通过一个for循环调用pwr()函数从1到maxCount+1的值。它的输出如下所示:
03 天然的循环
1.py如何打印连续清单3的连续比前3条(从前1条开始),每一行的长度都在大1。
- 清单3 三角1.py
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#397300">max</span> = <span style="color:#880000">8 </span>
<span style="color:#333333"><strong>for </strong></span> <span style="color:#333333"><strong>x</strong></span> in <span style="color:#397300">range</span> ( <span style="color:#880000">1</span> , <span style="color:#397300">max</span> + <span style="color:#880000">1</span> ):
<span style="color:#333333"><strong>for </strong></span> <span style="color:#333333"><strong>y</strong></span> in <span style="color:#397300">range</span> ( <span style="color:#880000">1</span> , <span style="color:#333333"><strong>x</strong></span> + <span style="color:#880000">1</span> ):
<span style="color:#333333"><strong>print</strong></span> ( <span style="color:#333333"><strong>y</strong></span> , <span style="color:#880000">''</span> , end= <span style="color:#880000">''</span> )
<span style="color:#333333"><strong>print</strong></span> ()
</span></span>
清单3首先通过初始化最大变量为8,然后打印x从1到max+1执行循环。内层有一个可能从1到x+1的循环变量y,并y的循环值。表现:
04 在for循环中使用split()函数
Python 支持这些各种字符串函数,包括split()函数函数。在需要将文本分解(即“分割”),然后使用相关循环和单词时,split( )函数非常有用。
join()函数lit()函数简单与另外两个或多个单词“连接为一行。通过使用split()函数,你可以直接删除中间多余的空格,然后调用函数join(),”使文本行中每个单词之间只有一个空格。
1. 使用split()函数做比较单词
清单4的Compare2.py说明了如何通过split()函数将文本字符串中的每个单词与另一个单词进行比较。
- 清单4 Compare2.py
<span style="color:#444444"><span style="background-color:#f6f6f6">x = <span style="color:#880000">'这是一个包含 abc 和 Abc 的字符串'</span>
y = <span style="color:#880000">'abc'</span>
相同 = <span style="color:#880000">0</span>
casematch = <span style="color:#880000">0 </span>
<span style="color:#333333"><strong>for</strong></span> w <span style="color:#333333"><strong>in</strong></span> x.split():
<span style="color:#333333"><strong>if</strong></span> (w == y):
相同相同 = 相同 + <span style="color:#880000">1 </span>
<span style="color:#333333"><strong>elif</strong></span> (w.lower() == y.lower()):
casematchcasematch = casematch + <span style="color:#880000">1 </span>
<span style="color:#333333"><strong>if</strong></span> (相同 > <span style="color:#880000">0</span> ):
print( <span style="color:#880000">'找到相同的匹配项:'</span> , 相同的)
<span style="color:#333333"><strong>if</strong></span> (casematch > <span style="color:#880000">0</span> ):
print( <span style="color:#880000">'找到案例匹配:'</span> , casematch)
<span style="color:#333333"><strong>if</strong></span> (casematch == <span style="color:#880000">0</span> <span style="color:#333333"><strong>和</strong></span> 相同 == <span style="color:#880000">0</span> ):
print( <span style="color:#880000">'没有找到匹配项'</span> )
</span></span>
4 通过sp()函数对字符串x中的每个单词和单词abc进行写匹配。加1。
清单 4 的输出如下所示:
2. 使用split()函数打印指定格式的文本
清单5FixedColumnCount1.py 说明了如何打印一组固定丰富的字符串。
- 清单5 FixedColumnCount1.py
<span style="color:#444444"><span style="background-color:#f6f6f6">import <span style="color:#397300">string</span>
wordCount = <span style="color:#880000">0</span>
str1 = <span style="color:#880000">'这是一个包含一组单词的字符串' </span>
<span style="color:#397300">print</span> ( <span style="color:#880000">'左对齐字符串:'</span> )
<span style="color:#397300">print</span> ( <span style="color:#880000">'----------------- ------'</span> )
<span style="color:#333333"><strong>for</strong></span> w <span style="color:#333333"><strong>in</strong></span> str1.split():
<span style="color:#397300">print</span> ( <span style="color:#880000">'%-10s'</span> % w)
wordCountwordCount = wordCount + <span style="color:#880000">1 </span>
<span style="color:#333333"><strong>if</strong></span> (wordCount % <span style="color:#880000">2</span> == <span style="color:#880000">0</span> ):
<span style="color:#397300">print</span> ( <span style="color:#880000">""</span> )
<span style="color:#397300">print</span> ( <span style="color:#880000">"\n"</span> )
<span style="color:#397300">print</span> ( <span style="color:#880000">'右对齐字符串:'</span> )
<span style="color:#397300">print</span> ( <span style="color:#880000">'---------- --------------'</span> )
wordCount = <span style="color:#880000">0 </span>
<span style="color:#333333"><strong>for</strong></span> w <span style="color:#333333"><strong>in</strong></span> str1.split():
<span style="color:#397300">print</span> ( <span style="color:#880000">'%10s'</span> % w)
wordCountwordCount = wordCount + <span style="color:#880000">1 </span>
<span style="color:#333333"><strong>if</strong></span> (wordCount % <span style="color:#880000">2</span> == <span style="color:#880000">0</span> ):
<span style="color:#397300">print</span> ()
</span></span>
5首先初始化变量wordCount和str1,然后为循环执行两个循环。第一个循环对str1的每个单词进行左列打印,第二个为循环对str1的每个单词进行右列打印。在每个循环中一个循环中当wordCount是偶数的时候就打印输出一次换行,这样每两个连续的单词之后就换行。清单5的输出如下所示:
3. 使用split()函数打印固定丰富的文本
清单6的FixedColumnWidth1.py说明了如何打印固定宽度的文本。
- 清单6 FixedColumnWidth1.py
<span style="color:#444444"><span style="background-color:#f6f6f6">导入字符串
左 = 0
对 = 0
列宽 = 8 <span style="color:#880000">str1 = ' </span>
这是一个 <span style="color:#333333"><strong>包含一组</strong></span> 单词 <span style="color:#333333"><strong>的</strong></span><span style="color:#333333"><strong>字符串</strong></span><span style="color:#333333"><strong>,</strong></span> 它将 被 <span style="color:#333333"><strong>分成</strong></span><span style="color:#333333"><strong>固定</strong></span> 的 <span style="color:#333333"><strong>列</strong></span><span style="color:#333333"><strong>宽</strong></span> ' <span style="color:#880000">
strLen = len(str1)
print('</span><span style="color:#333333"><strong>左</strong></span>对齐 <span style="color:#333333"><strong>列</strong></span>:<span style="color:#880000">')
print(' </span><span style="color:#888888">----------------------') </span>
rowCount = <span style="color:#397300">int</span> (strLen/columnWidth)
<span style="color:#333333"><strong>for</strong></span> i <span style="color:#333333"><strong>in </strong></span> <span style="color:#333333"><strong>range</strong></span> ( <span style="color:#880000">0</span> ,rowCount):
<span style="color:#333333"><strong>left</strong></span> = i*columnWidth
<span style="color:#333333"><strong>右</strong></span> = (i+ <span style="color:#880000">1</span> )*columnWidth <span style="color:#880000">-1</span>
字 = str1[<span style="color:#333333"><strong>左</strong></span>:<span style="color:#333333"><strong>右</strong></span>]
打印(<span style="color:#880000">“%-10s”</span> %字)
# <span style="color:#333333"><strong>检查</strong></span> <span style="color:#333333"><strong>“</strong></span><span style="color:#880000">部分行”</span>
if <span style="color:#333333"><strong>(</strong></span> rowCount*columnWidth < strLen):
<span style="color:#333333"><strong>left</strong></span> = rowCount*columnWidth <span style="color:#880000">-1</span> ;
右 = strLen
字 = str1[左:右]
print("%-10s" % word)
</span></span>
清单6初始化值整型是变量列宽和字符串类型str1。strLen是str1的长度,变量rowCountstrLen以除columnWidth的最后一个变量。之后通过循环包含每行columnWidth个字符。代码的部分输出清单 6 的输出内容如下:
4. 使用split()函数比较文本字符串
清单 7 的 CompareStrings1.py 说明了如何判断一个文本字符串中的单词是否出现在另一个文本字符串中。
- 清单7 CompareStrings1.py
<span style="color:#444444"><span style="background-color:#f6f6f6">text1 = 'a b <span style="color:#397300">c</span> d'
text2 = 'a b <span style="color:#397300">c</span> e d'
<span style="color:#333333"><strong>if</strong></span> (text2.find <span style="color:#397300">(</span> text1) >= <span style="color:#880000">0</span> ):
<span style="color:#397300">print</span> ('text1 <span style="color:#333333"><strong>is</strong></span> a substring of text2')
<span style="color:#333333"><strong>else</strong></span> :
<span style="color:#397300">print</span> ('text1 <span style="color:#333333"><strong>is</strong></span> not a substring of text2') <span style="color:#333333"><strong>对于 text1中的</strong></span> w <span style="color:#333333"><strong>,</strong></span>
subStr = <span style="color:#880000">True</span>
。<span style="color:#397300">split</span> ():
<span style="color:#333333"><strong>if</strong></span> (text2.find (w) == - <span style="color:#397300">1 </span><span style="color:#880000">)</span> :
subStr = <span style="color:#880000">False </span>
<span style="color:#333333"><strong>break </strong></span>
<span style="color:#333333"><strong>if</strong></span> (subStr == <span style="color:#880000">True</span> ):
<span style="color:#397300">print</span> (' text1 <span style="color:#333333"><strong>中的</strong></span><span style="color:#880000">每个</span> 单词 <span style="color:#333333"><strong>都是text2</strong></span><span style="color:#333333"><strong>中</strong></span> 的单词 ')
<span style="color:#333333"><strong>else</strong></span> :
<span style="color:#397300">print</span> ('<span style="color:#880000">不是</span> text1 <span style="color:#333333"><strong>中</strong></span>的 每个单词 <span style="color:#333333"><strong>都是text2</strong></span><span style="color:#333333"><strong>中</strong></span> 的单词 ')
</span></span>
清单7首先判断两个字符串text1和text2,然后通过条件逻辑字符串text2是否包含text1(并输出相应信息)。
清单7 一个循环后的字符串text1中的所有单词,并判断其是否出现在text2中。如果发现有匹配的失败,就设置subStr为False循环,并通过break语句跳出,提前准备好用于循环的执行。最后变量subStr的值打印的信息。清单7的输出如下所示:
05 用基础的循环显示字符串中的字符
8 的StringChars1.py 说明了如何打印一个文本字符串中的字符清单。
- 清单8 StringChars1.py
<span style="color:#444444"><span style="background-color:#f6f6f6">text = <span style="color:#880000">'abcdef' </span>
<span style="color:#333333"><strong>for</strong></span> ch <span style="color:#333333"><strong>in</strong></span> text:
<span style="color:#397300">print</span> ( <span style="color:#880000">'char:'</span> ,ch, <span style="color:#880000">'ord value:'</span> ,ord(ch))
<span style="color:#397300">print</span>
</span></span>
清单 8 的代码通过打印一个用于循环字符串的文本,并直接将它的所有简单字符以及字符的显示值排序(ASCII 码)。清单 8 的输出:
06 join()函数
另一个省略空格的方法是使用join()函数,示例如下:
split()函数将字符串“分割”为英雄的单词,同时作为一个代码的空格。接下来的函数使用一个空格分隔文本将字符串文本1中的存储连接。部分使用字符串XYZ替换空格作为分隔符,执行相同的连接操作。上述代码的输出如下:
作者简介:奥斯瓦尔德·坎佩萨托(Oswald Campesato),深入研究学习、Java、Android和TensorFlow。他是25本书的作者/合者。