python 格式化、set类型和class类基础知识练习(中)

简介: lambda 参数1,参数2,参数3:表达式 特点: 1.使用lambda关键字创建函数 2.没有名字的函数 3.匿名函数冒号后面的表达式有且只有一个,是表达式不是语句 4.自带return

12 可迭代参数 any



# 全部为false,返回false
def any(iterable):
    for ele in iterable:
        if ele:
            return False
    return True
li = [0,False,'']
print(any(li)) #False


13.enumerate 列出遍历数据和下标



li = ['a','b','c']
for index,item in enumerate(li,7):
    print(index,item)
# 改下标
7 a
8 b
9 c


14.set集合 不支持索引和切片,无序不重复



1.创建集合1

set1 = {'1','2'}
set2 = {'11','1'}
# 添加 add
set1.add('3')
# 清空 clear()
set1.clear()
# 取差集 difference
set1.difference(set2) #set1取set1中有的
# 取交集
set1.intersection(set2)
# 取并集
set1.union(set2)
set1 | set2
# 末尾移除
set1.pop()
# 指定移除
set1.discard(3)
# 更新 update 合并一起去重
set1.update(set2)


15 练习题1 三组数据求和



# 1-10,20-30,35-40
def threeSum(a1,a2,a3):
    return sum(a1+a2+a3)
a1 = list(range(1,11))
a2 = list(range(20,31))
a3 = list(range(35,41))
print(threeSum(a1,a2,a3))


16 练习题2 大小和尚多少个



def computers():
    for i in range(1,101):
        for j in range(1,34):
            if i+j==100 and 3*j+i/3 ==100:
                print('大和尚有{}个,小和尚有{}个'.format(j,i))
                pass
computers()
# 大和尚有25个,小和尚有75个


17 练习题3 找出独一无二的数据



li = [1,1,1,2,2,2,2,3,2,2,3,4,2,1,1]
def findUnionNumber(li):
    for item in li:
        if li.count(item)==1:
            return item
            pass
print(findUnionNumber(li))


1.字典统计每个元素的次数


dict ={}
for key in li:
    dict[key] = dict.get(key,0)+1
print(dict)


2.collection包下Counter类统计


from collections import Counter
a = [1, 2, 3, 1, 1, 2]
result = Counter(a)
print(result)


3.pandas包下的value_counts方法统计


import pandas as pd
a = pd.DataFrame([[1,2,3],
                  [3,1,3],
                  [1,2,1]])
result = a.apply(pd.value_counts)
print(result)


18.利用set找出独一无二的数据


li = [1,2,3,3,2,3,4,4,5,1,2,1]
def uniqueNum(li):
    set1 = set(li)
    for i in set1:
        li.remove(i)
    set2 = set(li)
    for j in set2:
        set1.remove(j)
    return set1
print(uniqueNum(li))


19 面向对象编程 oop


# 面向过程编程 根据业务从上到下开始编程
# 类的结构
# 类名 属性 方法
class People:
    name = 'zhan',
    age = 20,
    def eat(self):
        print('正在吃饭')
# 创建对象
people = People()
people.eat()


相关文章
|
5月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
352 1
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
399 100
|
6月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
561 99
|
6月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
6月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
6月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
6月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
7月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
383 92
|
8月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
687 0

推荐镜像

更多