求助python的for循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助python的for循环相关的知识,希望对你有一定的参考价值。

def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2

occurrences(string, string) -> int
"""
# add your code here
以上为给出的一段,然后让我们把code自己加在后面
题目是让我们计算在第二个字符串text2中,有哪几个字母是出现过在第一个字符串text1中的,出现了几次。结果是计算出出现次数的总和。
例如fooled和hello world, 最终结果应为7。(也就是1个'e', 3个'l', 2个'o', 1个'd')
程序会自动给text1, text2赋上随机的词,所以这个不用自己写。
求助!谢谢!

看题看错,我再想
==========
做出,运行环境python 3.2,>3.0可运行
import functools
def occurrences(text1, text2):
#一行流代码,利用字典统计text2中所含text1的数量
s_items=dict(map(lambda i:(i[0],text2.count(i[0])),dict( zip(text1,[0]*len(text1))).items()))
print(s_items) #输出过程 就是你所的“1个'e', 3个'l', 2个'o', 1个'd'”,这行可删掉
#合并输出统计总和
return functools.reduce(lambda x,y:x+y, s_items.values())
print(occurrences('fooled','hello world'))
==================
输出
'f': 0, 'e': 1, 'd': 1, 'o': 2, 'l': 3
7追问

谢了,不过我用的是2.7的。老师不让用3.0以上的版本。

追答

特别装了一个2.7

版本1,移植前贴的,包括显示过程
def occurrences(text1, text2):
s_items=dict(map(lambda i:(i[0],text2.count(i[0])),dict( zip(text1,[0]*len(text1))).items()))
print(s_items)
return reduce(lambda x,y:x+y, s_items.values())

版本2,壹行流
def occurrences_v2(text1, text2):
return reduce(lambda x,y:x+y, dict(map(lambda i:(i[0],text2.count(i[0])),dict( zip(text1,[0]*len(text1))).items())).values())

版本3,把count改了一下,效率应该提高不少
def occurrences_v3(text1, text2):
s=dict(zip( text1,[0]*len(text1)))
for c in text2:
if c in s:
s[c]+=1
return reduce(lambda x,y:x+y, s.values())
版本4, 仅统计,高效
def occurrences_v4(text1, text2):
count=0
for c in text2:
if c in text1:
count+=1
return count
版本5,上一个的壹行流
def occurrences_v5(text1, text2):
return reduce(lambda x,y: x+y, map(lambda x: 1 if x in text1 else 0, text2));

参考技术A 你好
我不是很了解,嗯 ,是只输出 出现次数 还是连字母一起输出
下面代码是输出的是["2 'o'", "3 'l'", "1 'e'", "1 'd'"] ,-版本2.5-,如果我理解不对的话也很好改。
import string
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2

occurrences(string, string) -> int
"""
List = []
n = 0
for i in text1:
n = text2.count(i)
if n != 0:
List.append(str(n) +' '+'\''+i+'\'')
l = []
for i in List:
if i not in l:
l.append(i)
print l
occurrences('fooled','hello world')追问

不用带字母的,就是输出出现次数的总数
例如fooled和hello world, 最终结果应为7。也就是hello world中出现了1个'e', 3个'l', 2个'o', 1个'd', 加起来的总数为7

追答

import string
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2

occurrences(string, string) -> int
"""
List = []
n = 0
for i in text1:
if i not in List:
List.append( i )
for i in List:
n = n + text2.count(i)
print n

参考技术B 刚试了下没有成功,帮不上你。

求助:foreach如何控制循环次数

  在 foreach 里面 设置计数器,当超过5的时候就跳出。

比如说:

定义一个数组,只显示前5个数

int[] str = 1, 2, 3, 4, 5, 6, 7, 8 ;
int i =0;
foreach (int s in str)

if (i < 5)

MessageBox.Show(s.ToString());
i++;

else

break;




这样说应该明白了吧??
参考技术A foreach ( $arr as $k=>$v)
if( 这里面写条件 比如 $i = 0 ;$i<$len; $i++)
执行的语句;


echo $v,<br/>;
return 结果;

以上是关于求助python的for循环的主要内容,如果未能解决你的问题,请参考以下文章

python中for循环的用法

python的for如何获得当前循环次数

python中for循环的用法

python中for循环语句

关于python的for循环

python为啥for循环只查到一次数据