python练习 2020/07/08

18.要求实现一函数,该函数用于求两个集合的差集,结果集合中包含所有属于第一个集合但不属于第二个集合
的元素
def func(set1,set2):
    dif_list=[]
    for i in set1:
        if i not in set2:
            dif_list.append(i)

    return dif_list

set1={1,2,3,4}
set2={2,3,4,5}
print(func(set1,set2))

19.找出一段句子中最长的单词及其索引位置,以list返回
import string
s = "i am a good boy,huhongqiang!"
for i in s:
    if i in string.punctuation:
        s=s.replace(i,' ')
        print(s)

max_word=sorted(s.split(),key=len,reverse=True)[0]
print(max_word)
 

max_word_length=len(max_word)

for i in range(len(s)):
    if s[i:i+max_word_length]==max_word:
        max_length_index=i

print(max_length_index,max_word_length)

#用函数
s = "i am a good boy,huhongqiang!"
def find_word_index(s):
    for i in s:
        if not i.isalpha() and not i.isspace():
            s=s.replace(i,' ')
            #print(s)

    max_word=sorted(s.split(),key=len,reverse=True)[0]
    max_word_length=len(max_word)
    for i in range(len(s)):
        if s[i:i+max_word_length]==max_word:
            max_index=i
    return max_index,max_word

print(find_word_index(s))


20.返回序列中的最大数
def find_max_number(s):
    if not isinstance(s,(str,list,tuple)):
        return False
    max_number=0.0
    if isinstance(s,(list,tuple)):
        for i in s:
            if i>max_number:
                max_number=i
    else:
        for i in s:
            if float(i)>max_number:
                max_number=float(i)

    return max_number

print(find_max_number('12345673'))
print(find_max_number((1,3,5,7,5)))
print(find_max_number([3,5,6,4,6]))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.apache.spark.api.python.PythonException: Traceback (most recent call last): File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 830, in main process() File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/python/lib/pyspark.zip/pyspark/worker.py", line 820, in process out_iter = func(split_index, iterator) File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/rdd.py", line 5405, in pipeline_func return func(split, prev_func(split, iterator)) File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/rdd.py", line 5405, in pipeline_func return func(split, prev_func(split, iterator)) File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/rdd.py", line 828, in func return f(iterator) File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/rdd.py", line 3964, in combineLocally merger.mergeValues(iterator) File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/python/lib/pyspark.zip/pyspark/shuffle.py", line 256, in mergeValues for k, v in iterator: File "/Users/zzs/PycharmProjects/pythonProject/venv/lib/python3.10/site-packages/pyspark/python/lib/pyspark.zip/pyspark/util.py", line 81, in wrapper return f(*args, **kwargs) File "/Users/zzs/PycharmProjects/pythonProject/pyspark项目练习/项目练习2.py", line 7, in <lambda> json_str_file = file_rdd.flatMap(lambda x: x.spilt("|")) AttributeError: 'str' object has no attribute 'spilt' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:561) at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:767) at org.apache.spark.api.python.PythonRunner$$anon$3.read(PythonRunner.scala:749) at org.apache.spark.api.python.BasePythonRun
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值