PYTHON小知识点(1)

MENU

  1. 列表类型转换
  2. 列表排序
  3. 一行输入
  4. 函数参数问题
  5. 注意,python中没有++,--!!!
  6. 元素快速生成
  7. 随机数简单使用
  8. continue

1.map函数使用

map() 函数语法:

new_list=map(function,iterable1,iterable2,...)

即map是一个映射函数,将iterable序列中的每一个元素调用function函数,并返回。这可以用于快速运算,例如:

        我们输入了一个list,其中元素均为str类型,需要将其全部转换成整型int,可以:

#元素字符串转换成数字
exp_list=[]
for i in range(0,3):
    exp=input("请输入你想要的数组")
    exp_list.append(exp)
print(type(exp_list[0]))
print(exp_list)

new=list(map(int,exp_list))
print(type(new[0]))
print(new)

结果:

注意,map中的function 不需要打扩号,同时map返回为map类型,需要转换为自己所需要的类型。

2.列表排序函数:List.sort()

函数原型:

list.sort(cmp=None, key=None, reverse=False)
  • cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
  • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。

如:需要对数据进行排序

LIST=[1,2,3,5,4]
LIST.sort(reverse = True)
print(LIST)  

即可得到结果:

3.输入一行数据

使用for 循环加入


line = input("请输入一行字符:")
list_dig=[]
count_letters = count_digits = count_others = 0
for charr in line:
    if charr.isalpha():
        count_letters += 1
    elif charr.isdigit():
        count_digits += 1
        list_dig.append(charr)
    else:
        count_others += 1
print(f"英文字母数量为:{count_letters}")
print(f"数字数量为:{count_digits}")
print(f"其他字符数量为:{count_others}")

listnew=list(map(int,list_dig))
print(listnew)

4.函数参数

参考:htps://blog.youkuaiyun.com/yyykj/article/details/103122665

a.使用键值对

def getuser(
    pw='1234',
    user='root',
    host='127.0.0.1',
    port='3206',
    db='tests')

b.默认调用值

def getuser(user, pw, host, port, db, charset='utf8'):
    "打印得到的数据库配置"
    print('host: ', host)
    print('port: ', port)
    print('user: ', user)
    print('pw: ', pw)
    print('db: ', db)
    print('charset: ', charset)


demo_get_conf2('root', '1234', '127.0.0.1', '3306', 'tests')

c.不定长构成元组:*arg

与元组型相比,不需要输入数据类型为元组即可

def demo_get_conf3(host, port, *cnf):
    "打印得到的数据库配置"
    print('host: ', host)
    print('port: ', port)
    print('user: ', cnf[0])
    print('pw: ', cnf[1])
    print('db: ', cnf[2])
    print('charset: ', cnf[3])


demo_get_conf3('127.0.0.1', '3306', 'root', '1234', 'tests', 'utf8')

d.不定长字典:**arg

按照键值对输入数据

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# 演示获得数据库配置参数,使用可变长字典参数
def demo_get_conf7(host, port, **cnf):
    "打印得到的数据库配置"
    print('host: ', host)
    print('port: ', port)
    print('user: ', cnf['user'])
    print('pw: ', cnf['pw'])
    print('db: ', cnf['db'])
    print('charset: ', cnf['charset'])


demo_get_conf7('127.0.0.1', '3306', user='new_user', pw='5678', db='tests', charset='utf8mb4')

e.声明函数参数,返回值类型

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

##num:List[int],指num是列表类型,列表元素为int类型
##->指明返回值类型为List[int]型

其中,nums是变量名称

6.a=[i]*5

dp=[0]*5
print(dp)
print(type(dp))

如果改为:

dp=[()]*5
print(dp)
print(type(dp))

得到:

7.随机数

需要引入random库

for a in range(1,10):
    l[a]=random.random()*a
    sum[a]=l[a]+sum[a-1]

8.continue:

与c&&c++相同,终结当前循环,进入下一循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值