本站使用了 Pjax 等基于 JavaScript 的开发技术,但您的浏览器已禁用 JavaScript,请开启 JavaScript 以保证网站正常显示!

Python语法学习_格式化

list

多层嵌套列表转换成单层

list =[1,[2,3,[4,5]]] convert to list = [1,2,3,4,5,6]

def flatten(nums):
    res = []
    for i in nums:uuts
        if isinstance(i, list):
            res.extend(flatten(i))
        else:
            res.append(i)
    return res
# print(flatten(["junk",["nested stuff"],[],[[]]]))  

比较两个list之间的差异

python比较两个list之间的差异、相同(差集、交集、并集)
https://blog.51cto.com/lj23for1/2139933?source=dra

listA = ['zhangsan', 'lisi', 'wangwu']
listB = ['zhangsan', 'lisi', 'zhaoliu']
# 1.差集
# listA对应listB的差集(A和B哪个不相同)
set(listA).difference(set(listB))
# set(['wangwu'])
# listB对应listA的差集(B和A哪个不相同)
set(listB).difference(set(listA))
# set(['zhaoliu'])
# 2.交集
set(listA).intersection(set(listB))
# set(['lisi', 'zhangsan'])
# 3.并集
set(listA).union(set(listB))
set(['lisi', 'zhaoliu', 'zhangsan', 'wangwu'])

list根据多个条件进行排序

按照日期和场次进行升序排序

l = [['周四001','2019-12-26 23:00'],
['周四002','2019-12-26 23:00'],
['周四003','2019-12-26 20:00'],
['周四005','2019-12-26 21:00'],
['周四004','2019-12-26 21:00']]
nl = sorted(l,key = lambda x:(x[1],x[0]),reverse=False)
print(nl)

list中的元素去重

https://www.cnblogs.com/liu--huan/articles/10959247.html

# 方法一
data = [1,2,3,2,1,5,4,7]
data = list(set(data))
print(data)
# 方法二
data = [1,2,3,2,1,5,4,7]
new_data = []
for i in data:
    if i not in new_data:
        new_data.append(i)
print(new_data)

删除列表中相邻的重复值

l = ['a', 'b', 'c', 'c','d','d' ,'e', 'e', 'e', 'f']
for i in range(len(l)-1,0,-1):
    if l[i] == l[i-1]:
        del l[i]
print(l)
# ['a', 'b', 'c', 'd', 'e', 'f']

将列表中的元素顺序打乱

from random import shuffle
x = [1,2,3,4,5]
shuffle(x)
print(x)

字符串

字符串去除空格

str = ' a b c '
# strip去除左右
print(str.strip())
# lstrip去除左
print(str.lstrip())
# rstrip去除右
print(str.rstrip())
# replace()去除所有
print(str.replace(' ',''))

字符串替换多次

str = '[-1]'
# 去除左右括号
print(str.replace('[','').replace(']',''))

取字符串某个字符前面或后面的值


# 取'-'两边的数值
str = '21-33'
print(str[0:str.index('-')])
print(str[str.index('-')+1:])

# 使用分割函数
a = '20.5.48'
b = a.split('.')
print(b)

字母和数字的转换

# ord:字母to数字   chr:数字to字母
for i in range(ord('a'),ord('z')):
    print (i,chr(i))

time/date

获取某一天是星期几

参考网址:http://www.jb51.net/article/68962.htm

import time,datetime
date = '2019-12-24'
date =  datetime.datetime.strptime(date,'%Y-%m-%d')
# day = date.weekday() # 使用该方法可以获取到星期对应的id
# print(day)   # 0,1,2,3,4,5,6 => 一,二,三,四,五,六,日

def get_week_day(date):
  week_day_dict = {
    0 : '周一',
    1 : '周二',
    2 : '周三',
    3 : '周四',
    4 : '周五',
    5 : '周六',
    6 : '周日',
  }
  day = date.weekday()
  return week_day_dict[day]
print(get_week_day(date))

字符串和日期之间的转换

import datetime
date_str = '2019-12-12'
date = datetime.datetime.strptime(date_str,'%Y-%m-%d')  # string to datetime.datetime
date_str = date.strftime("%Y-%m-%d") # date to string

日期+1天

import datetime
n = 1
start_date_str = '2019-12-12'
start_date = datetime.datetime.strptime(start_date_str,'%Y-%m-%d')
delta = datetime.timedelta(days=int(n))
date = start_date +delta
print(date)

将时间差换算成小时

import datetime

l = ['2017-01-02 23:00', '2017-01-03 12:20', '2017-01-08 22:00',
'2017-01-08 23:00','2017-01-14 03:00']

day1 = datetime.datetime.strptime(l[1],'%Y-%m-%d %H:%M')
day2 = datetime.datetime.strptime(l[0],'%Y-%m-%d %H:%M')
print(day1,day2)
a = day1 - day2
hour = ((a.days * 24 * 60 ) + a.seconds/60)/60
print(hour)
print(type(hour))

Pandas

datetime格式化

# RangeIndex: 90 entries, 0 to 89
# Data columns (total 13 columns):
# id             90 non-null int64
# type_          90 non-null object
# match_no_id    90 non-null int64
# datetime    90 non-null datetime64[ns]

df['new_datetime'] = df['datetime'].apply(lambda x : x.strftime('%Y-%m-%d %H'))

# datetime                      new_datetime
# 2020-01-19 00:00:00.000038 -> 2020-01-19 00

帮助小微企业构建在线化、智能化的经营管理体系,让经营管理更轻松!

推广

 继续浏览关于 listreplacestriptimeweeksortedsplitpandas 的文章

 本文最后更新于:2020/01/21 15:39:49,可能因经年累月而与现状有所差异,有任务疑问可以随时与258771198@qq.com沟通,邮箱永久有效

 引用转载请注明:魔码科技 > Python > Python语法学习_格式化

发现共鸣

如是说:使人成熟的是经历,而不是岁月。