当前位置: 首页 > news >正文

pandas教程:Essential Functionality 索引 过滤 映射 排序

文章目录

  • 5.2 Essential Functionality(主要功能)
  • 1 Reindexing(重新索引)
  • 2 Dropping Entries from an Axis (按轴删除记录)
  • 3 Indexing, Selection, and Filtering(索引,选择,过滤)
    • Selection with loc and iloc(用loc和iloc来选择)
  • 4 Integer Indexes(整数索引)
  • 5 Arithmetic and Data Alignment (算数和数据对齐)
      • Arithmetic methods with fill values (带填充值的算数方法)
      • Operations between DataFrame and Series (DataFrame和Series之间的操作)
  • 6 Function Application and Mapping (函数应用和映射)
  • 7 Sorting and Ranking (排序)
  • 8 Axis Indexes with Duplicate Labels (有重复label的轴索引)

5.2 Essential Functionality(主要功能)

接下来介绍pandas中的一些主要功能,这里只介绍一些经常用到的。

1 Reindexing(重新索引)

pandas中一个重要的方法是reindex,已实施在创建object的时候遵照一个新的index。如下例:

import pandas as pd
obj = pd.Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])
obj
d    4.5
b    7.2
a   -5.3
c    3.6
dtype: float64

series上调用reindex能更改index,如果没有对应index的话会引入缺失数据:

obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])
obj2
a   -5.3
b    7.2
c    3.6
d    4.5
e    NaN
dtype: float64

在处理时间序列这样的数据时,我们可能需要在reindexing的时候需要修改值。method选项能做到这一点,比如设定methodffill:

obj3 = pd.Series(['bule', 'purple', 'yellow'], index=[0, 2, 4])
obj3
0      bule
2    purple
4    yellow
dtype: object
obj3.reindex(range(6), method='ffill')
0      bule
1      bule
2    purple
3    purple
4    yellow
5    yellow
dtype: object

对于DataFrame,reindex能更改row index,或column indexreindex the rows:

import numpy as np
frame = pd.DataFrame(np.arange(9).reshape(3, 3),index=['a', 'c', 'd'],columns=['Ohio', 'Texas', 'California'])
frame
OhioTexasCalifornia
a012
c345
d678
frame2 = frame.reindex(['a', 'b', 'c', 'd'])
frame2
OhioTexasCalifornia
a0.01.02.0
bNaNNaNNaN
c3.04.05.0
d6.07.08.0

更改columns index:

states = ['Texas', 'Utah', 'California']
frame.reindex(columns=states)
TexasUtahCalifornia
a1NaN2
c4NaN5
d7NaN8

还可以使用loc更简洁的reindex

frame.loc[['a', 'b', 'c', 'd'], states]
TexasUtahCalifornia
a1.0NaN2.0
bNaNNaNNaN
c4.0NaN5.0
d7.0NaN8.0

2 Dropping Entries from an Axis (按轴删除记录)

对于series,drop回返回一个新的object,并删去你制定的axis的值:

obj = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
obj
a    0.0
b    1.0
c    2.0
d    3.0
e    4.0
dtype: float64
new_obj = obj.drop('c')
new_obj
a    0.0
b    1.0
d    3.0
e    4.0
dtype: float64
obj.drop(['d', 'c'])
a    0.0
b    1.0
e    4.0
dtype: float64

对于DataFrame,index能按行或列的axis来删除:

data = pd.DataFrame(np.arange(16).reshape(4, 4),index=['Ohio', 'Colorado', 'Utah', 'New York'],columns=['one', 'two', 'three', 'four'])
data
onetwothreefour
Ohio0123
Colorado4567
Utah891011
New York12131415

行处理:如果a sequence of labels(一个标签序列)来调用drop,会删去row labels(axis 0):

data.drop(['Colorado', 'Ohio'])
onetwothreefour
Utah891011
New York12131415

列处理:drop列的话,设定axis=1axis='columns':

data.drop('two', axis=1)
onethreefour
Ohio023
Colorado467
Utah81011
New York121415
data.drop(['two', 'four'], axis='columns')
onethree
Ohio02
Colorado46
Utah810
New York1214

drop也可以不返回一个新的object,而是直接更改series or dataframe in-place:

obj.drop('c', inplace=True)
obj
a    0.0
b    1.0
d    3.0
e    4.0
dtype: float64

3 Indexing, Selection, and Filtering(索引,选择,过滤)

series indexing(obj[...]) 相当于numpyarray indexing, 而且除了整数,还可以使用seriesindex

obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
obj
a    0.0
b    1.0
c    2.0
d    3.0
dtype: float64
obj['b']
1.0
obj[1]
1.0
obj[2:4]
c    2.0
d    3.0
dtype: float64
# 选中行
obj[['b', 'a', 'd']]
b    1.0
a    0.0
d    3.0
dtype: float64
obj[[1, 3]]
b    1.0
d    3.0
dtype: float64
obj[obj < 2]
a    0.0
b    1.0
dtype: float64

labelslicing(切片)的时候,和python的切片不一样的在于,会包括尾节点:

obj['b':'c']
b    1.0
c    2.0
dtype: float64

可以直接给选中的label更改值:

obj['b':'c'] = 5
obj
a    0.0
b    5.0
c    5.0
d    3.0
dtype: float64

而对于DataFrame,indexing可以通过一个值或序列,选中一个以上的列:

data = pd.DataFrame(np.arange(16).reshape((4, 4)),index=['Ohio', 'Colorado', 'Utah', 'New York'],columns=['one', 'two', 'three', 'four'])
data
onetwothreefour
Ohio0123
Colorado4567
Utah891011
New York12131415
data['two']
Ohio         1
Colorado     5
Utah         9
New York    13
Name: two, dtype: int64
data[['three', 'one']]
threeone
Ohio20
Colorado64
Utah108
New York1412

dataframeindexing有一些比较特别的方式。比如通过布尔数组:

data[:2]
onetwothreefour
Ohio0123
Colorado4567
data[data['three'] > 5]
onetwothreefour
Colorado4567
Utah891011
New York12131415

行选择的语法格式data[:2]是很方便的。给[]里传入一个list的话,可以选择列。

另一种方法是用boolean dataframe:

data < 5
onetwothreefour
OhioTrueTrueTrueTrue
ColoradoTrueFalseFalseFalse
UtahFalseFalseFalseFalse
New YorkFalseFalseFalseFalse
data[data < 5] = 0
data
onetwothreefour
Ohio0000
Colorado0567
Utah891011
New York12131415

Selection with loc and iloc(用loc和iloc来选择)

对于label-indexing on rows, 我们介绍特别的索引符,loc and iloc. 这两个方法能通过axis labels(loc)integer(iloc),来选择行或列。

一个列子,选中一行多列by label

data
onetwothreefour
Ohio0000
Colorado0567
Utah891011
New York12131415
data.loc['Colorado', ['two', 'three']]
two      5
three    6
Name: Colorado, dtype: int64

iloc实现相同的效果:

data.iloc[2, [3, 0, 1]]
four    11
one      8
two      9
Name: Utah, dtype: int64
data.iloc[2] # 一行
one       8
two       9
three    10
four     11
Name: Utah, dtype: int64
data.iloc[[1, 2], [3, 0, 1]]
fouronetwo
Colorado705
Utah1189

indexing函数也能用于切片,不论是single labelslists of labels:

data.loc[:'Utah', 'two']
Ohio        0
Colorado    5
Utah        9
Name: two, dtype: int64
data.iloc[:, :3][data.three > 5]
onetwothree
Colorado056
Utah8910
New York121314

注意:当设计pandas的时候,作者发现frame[:, col]这样的语法是比较冗长的,因为这是会被经常用到的一个功能。作者把一些indexing的功能(lable or integer)集成在了ix这个方法上。实际中,因为这种labelinteger都可以用的方式很方便,于是pandas team设计了lociloc来实现label-basedinteger-based indexing.

虽然ix indexing依然存在,但是已经过时,不推荐使用。

4 Integer Indexes(整数索引)

一些新手再用integerindex的时候,总是会被绊倒。因为这种方法和python用于listtupleindexing方法不同。

比如,你不希望下面的代码出现error

ser = pd.Series(np.arange(3.))
ser
0    0.0
1    1.0
2    2.0
dtype: float64
ser[-1]
---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)<ipython-input-61-3cbe0b873a9e> in <module>()
----> 1 ser[-1]/Users/xu/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)581         key = com._apply_if_callable(key, self)582         try:
--> 583             result = self.index.get_value(self, key)584 585             if not lib.isscalar(result):/Users/xu/anaconda/envs/py35/lib/python3.5/site-packages/pandas/indexes/base.py in get_value(self, series, key)1978         try:1979             return self._engine.get_value(s, k,
-> 1980                                           tz=getattr(series.dtype, 'tz', None))1981         except KeyError as e1:1982             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3332)()pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3035)()pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6610)()pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6554)()KeyError: -1

看到了,pandas在整数索引上可能会出错。这里我们有一个index包括0,1,2,但是猜测用户想要什么是很困难的:

ser
0    0.0
1    1.0
2    2.0
dtype: float64

另一方面,如果用非整数来做index,就没有歧义了:

ser2 = pd.Series(np.arange(3.), index=['a', 'b', 'c'])
ser2[-1]
2.0

为了保持连贯性,如果axis index里包含integer,那么选择数据的时候,就会是label-oriented. 为了更精确地选择,使用loc(for label)或ilco(for integers):

ser[:1]
0    0.0
dtype: float64
ser.loc[:1]
0    0.0
1    1.0
dtype: float64
ser.iloc[:1]
0    0.0
dtype: float64

5 Arithmetic and Data Alignment (算数和数据对齐)

pandas一个有用的feature就是,不同indexobejct之间的算数计算。如果两个object相加,但他们各自的index并不相同,最后结果得到的index是这两个index的合集:

s1 = pd.Series([7.3, -2.5, 3.4, 1.5], index=['a', 'c', 'd', 'e'])
s2 = pd.Series([2.1, 3.6, -1.5, 4, 3.1], index=['a', 'c', 'e', 'f', 'g'])
s1
a    7.3
c   -2.5
d    3.4
e    1.5
dtype: float64
s2
a    2.1
c    3.6
e   -1.5
f    4.0
g    3.1
dtype: float64
s1 + s2
a    9.4
c    1.1
d    NaN
e    0.0
f    NaN
g    NaN
dtype: float64

这种数据对齐的方式(internal data alignment)引入了很多缺失值在没有的位置上。这些缺失值会被用在之后的算数计算中。

DataFrame中,数据对齐同时发生在行和列上:

df1 = pd.DataFrame(np.arange(9.).reshape((3, 3)), columns=list('bcd'),index=['Ohio', 'Texas', 'Colorado'])
df2 = pd.DataFrame(np.arange(12.).reshape((4, 3)), columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])
df1
bcd
Ohio0.01.02.0
Texas3.04.05.0
Colorado6.07.08.0
df2
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.0

相加的结果就是两个DataFrame,行和列的合集:

df1 + df2
bcde
ColoradoNaNNaNNaNNaN
Ohio3.0NaN6.0NaN
OregonNaNNaNNaNNaN
Texas9.0NaN12.0NaN
UtahNaNNaNNaNNaN

因为'c''e'列都不在两个DataFrame里,所有全是缺失值。对于行,即使有相同的,但列不一样的话也会是缺失值。

如果两个DataFrame相加,而且没有columnrow,结果会全是null

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
df1
A
01
12
df2
B
03
14
df1 - df2
AB
0NaNNaN
1NaNNaN

Arithmetic methods with fill values (带填充值的算数方法)

对于上面那些缺失值,我们想要填上0:

df1 = pd.DataFrame(np.arange(12.).reshape((3, 4)), columns=list('abcd'))df2 = pd.DataFrame(np.arange(20.).reshape((4, 5)), columns=list('abcde'))df2.loc[1, 'b'] = np.nan
df1
abcd
00.01.02.03.0
14.05.06.07.0
28.09.010.011.0
df2
abcde
00.01.02.03.04.0
15.0NaN7.08.09.0
210.011.012.013.014.0
315.016.017.018.019.0

不使用添加方法的结果:

df1 + df2
abcde
00.02.04.06.0NaN
19.0NaN13.015.0NaN
218.020.022.024.0NaN
3NaNNaNNaNNaNNaN

使用fill_value

df1.add(df2, fill_value=0)
abcde
00.02.04.06.04.0
19.05.013.015.09.0
218.020.022.024.014.0
315.016.017.018.019.0

每一个都有一个配对的,以 r 开头,意思是反转:

1 / df1
abcd
0inf1.0000000.5000000.333333
10.2500000.2000000.1666670.142857
20.1250000.1111110.1000000.090909
df1.rdiv(1)
abcd
0inf1.0000000.5000000.333333
10.2500000.2000000.1666670.142857
20.1250000.1111110.1000000.090909

reindex(重建索引)的时候,也可以使用fill_value:

df1.reindex(columns=df2.columns, fill_value=0)
abcde
00.01.02.03.00
14.05.06.07.00
28.09.010.011.00

Operations between DataFrame and Series (DataFrame和Series之间的操作)

先举个numpy的例子帮助理解,可以考虑成一个二维数组和它的一行:

arr = np.arange(12.).reshape((3, 4))
arr
array([[  0.,   1.,   2.,   3.],[  4.,   5.,   6.,   7.],[  8.,   9.,  10.,  11.]])
arr[0]
array([ 0.,  1.,  2.,  3.])
arr - arr[0]
array([[ 0.,  0.,  0.,  0.],[ 4.,  4.,  4.,  4.],[ 8.,  8.,  8.,  8.]])

可以看到,这个减法是用在了每一行上。这种操作叫broadcasting,在Appendix A有更详细的解释。DataFrameSeries的操作也类似:

frame = pd.DataFrame(np.arange(12.).reshape((4, 3)),columns=list('bde'),index=['Utah', 'Ohio', 'Texas', 'Oregon'])
series = frame.iloc[0]
frame
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.0
series
b    0.0
d    1.0
e    2.0
Name: Utah, dtype: float64

可以理解为seriesindexdataframe的列匹配,broadcasting down the rows(向下按行广播):

frame - series
bde
Utah0.00.00.0
Ohio3.03.03.0
Texas6.06.06.0
Oregon9.09.09.0

如果一个index既不在DataFramecolumn中,也不再series里的index中,那么结果也是合集:

series2 = pd.Series(range(3), index=['b', 'e', 'f'])
frame + series2
bdef
Utah0.0NaN3.0NaN
Ohio3.0NaN6.0NaN
Texas6.0NaN9.0NaN
Oregon9.0NaN12.0NaN

如果想要广播列,去匹配行,必须要用到算数方法:

series3 = frame['d']
frame
bde
Utah0.01.02.0
Ohio3.04.05.0
Texas6.07.08.0
Oregon9.010.011.0
series3
Utah       1.0
Ohio       4.0
Texas      7.0
Oregon    10.0
Name: d, dtype: float64
frame.sub(series3, axis='index')
bde
Utah-1.00.01.0
Ohio-1.00.01.0
Texas-1.00.01.0
Oregon-1.00.01.0

axis参数就是用来匹配轴的。在这个例子里是匹配dataframerow index(axis='index or axis=0),然后再广播。

6 Function Application and Mapping (函数应用和映射)

numpyufuncs(element-wise数组方法)也能用在pandasobject上:

frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])
frame
bde
Utah-0.8104350.194448-0.705901
Ohio-0.8862750.5536401.066754
Texas0.189898-0.056108-0.159926
Oregon0.4483030.439650-1.351029
np.abs(frame)
bde
Utah0.8104350.1944480.705901
Ohio0.8862750.5536401.066754
Texas0.1898980.0561080.159926
Oregon0.4483030.4396501.351029

另一个常用的操作是把一个用在一维数组上的函数,应用在一行或一列上。要用到DataFrame中的apply函数:

f = lambda x: x.max() - x.min()
frame.apply(f)
b    1.334579
d    0.609748
e    2.417783
dtype: float64

这里函数f,计算的是一个series中最大值和最小值的差,在frame中的每一列,这个函数被调用一次。作为结果的series,它的index就是framecolumn

如果你传入axis='column'用于apply,那么函数会被用在每一行:

frame.apply(f, axis='columns')
Utah      1.004883
Ohio      1.953030
Texas     0.349825
Oregon    1.799333
dtype: float64

像是sum, mean这样的数组统计方法,DataFrame中已经集成了,所以没必要用apply

apply不会返回标量,只会返回一个含有多个值的series

def f(x): return pd.Series([x.min(), x.max()], index=['min', 'max'])
frame
bde
Utah-0.8104350.194448-0.705901
Ohio-0.8862750.5536401.066754
Texas0.189898-0.056108-0.159926
Oregon0.4483030.439650-1.351029
frame.apply(f)
bde
min-0.886275-0.056108-1.351029
max0.4483030.5536401.066754

element-wisepython函数也能用。假设想要格式化frame中的浮点数,变为string。可以用apply map

format = lambda x: '%.2f' % x
frame.applymap(format)
bde
Utah-0.810.19-0.71
Ohio-0.890.551.07
Texas0.19-0.06-0.16
Oregon0.450.44-1.35

applymap的做法是,series有一个map函数,能用来实现element-wise函数:

frame['e'].map(format)
Utah      -0.71
Ohio       1.07
Texas     -0.16
Oregon    -1.35
Name: e, dtype: object

7 Sorting and Ranking (排序)

rowcolumn index来排序的话,可以用sort_index方法,会返回一个新的object:

obj = pd.Series(range(4), index=['d', 'a', 'b', 'c'])
obj.sort_index()
a    1
b    2
c    3
d    0
dtype: int64

DataFrame,可以用index或其他axis来排序:

frame = pd.DataFrame(np.arange(8).reshape((2, 4)),index=['three', 'one'],columns=['d', 'a', 'b', 'c'])
frame
dabc
three0123
one4567
frame.sort_index()
dabc
one4567
three0123
frame.sort_index(axis=1)
abcd
three1230
one5674

默认是升序,可以设置降序:

frame.sort_index(axis=1, ascending=False)
dcba
three0321
one4765

通过值来排序,用sort_values方法:

obj = pd.Series([4, 7, -3, 2])
obj.sort_values()
2   -3
3    2
0    4
1    7
dtype: int64

缺失值会被排在最后:

obj = pd.Series([4, np.nan, 7, np.nan, -3, 2])
obj.sort_values()
4   -3.0
5    2.0
0    4.0
2    7.0
1    NaN
3    NaN
dtype: float64

对于一个DataFrame,可以用一列或多列作为sort keys。这样的话,只需要把一列多多列的名字导入到sort_values即可:

frame = pd.DataFrame({'b': [4, 7, -3, 2], 'a': [0, 1, 0, 1]})
frame
ab
004
117
20-3
312
frame.sort_values(by='b')
ab
20-3
312
004
117

多列排序的话,传入一个list of names

frame.sort_values(by=['a', 'b'])
ab
20-3
004
312
117

ranking(排名)是给有效的数据分配数字。rank方法能用于seriesDataFramerank方法默认会给每个group一个mean rank(平均排名)。rank 表示在这个数在原来的Series中排第几名,有相同的数,取其排名平均(默认)作为值:

obj = pd.Series([7, -5, 7, 4, 2, 0, 4])
obj
0    7
1   -5
2    7
3    4
4    2
5    0
6    4
dtype: int64
obj.sort_values()
1   -5
5    0
4    2
3    4
6    4
0    7
2    7
dtype: int64
obj.rank()
0    6.5
1    1.0
2    6.5
3    4.5
4    3.0
5    2.0
6    4.5
dtype: float64

obj中,4和4的排名是第4名和第五名,取平均得4.5。7和7的排名分别是第六名和第七名,则其排名取平均得6.5。

rank也可以根据数据被观测到的顺序来设定:

obj
0    7
1   -5
2    7
3    4
4    2
5    0
6    4
dtype: int64
obj.rank(method='first')
0    6.0
1    1.0
2    7.0
3    4.0
4    3.0
5    2.0
6    5.0
dtype: float64

这里没有给0和2(指两个数字7)赋予average rank 6.5,而是给第一个看到的7(label 0)设置rank为6,第二个看到的7(label 2)设置rank为7。

也可以设置降序:

# Assign tie values the maximum rank in the group
obj.rank(ascending=False, method='max')
0    2.0
1    7.0
2    2.0
3    4.0
4    5.0
5    6.0
6    4.0
dtype: float64

dataframe 可以根据行或列来计算rank:

frame = pd.DataFrame({'b': [4.3, 7, -3, 2],'a': [0, 1, 0, 1],'c': [-2, 5, 8, -2.5]})
frame
abc
004.3-2.0
117.05.0
20-3.08.0
312.0-2.5
frame.rank(axis='columns') # columns表示列与列之间的排序(即每一行里数据间的排序)
abc
02.03.01.0
11.03.02.0
22.01.03.0
32.03.01.0

8 Axis Indexes with Duplicate Labels (有重复label的轴索引)

我们看到的所有例子都有unique axis labels(index values),唯一的轴标签(索引值)。一些pandas函数(reindex),需要label是唯一的,但这并是不强制的。比如下面有一个重复的索引:

obj = pd.Series(range(5), index=['a', 'a', 'b', 'b', 'c'])
obj
a    0
a    1
b    2
b    3
c    4
dtype: int64

indexis_unique特性能告诉我们label是否是唯一的:

obj.index.is_unique
False

数据选择对于重复label则表现有点不同。如果一个label有多个值,那么就会返回一个series, 如果是label只对应一个值的话,会返回一个标量:

obj['a']
a    0
a    1
dtype: int64
obj['c']
4

这个选择的逻辑也应用于DataFrame

df = pd.DataFrame(np.random.randn(4, 3), index=['a', 'a', 'b', 'b'])
df
012
a-0.314526-1.3138610.823529
a0.994028-0.442338-0.846985
b-1.340453-0.0316120.044791
b-0.919341-0.409164-1.297257
df.loc['b']
012
b-1.340453-0.0316120.044791
b-0.919341-0.409164-1.297257

相关文章:

pandas教程:Essential Functionality 索引 过滤 映射 排序

文章目录 5.2 Essential Functionality&#xff08;主要功能&#xff09;1 Reindexing&#xff08;重新索引&#xff09;2 Dropping Entries from an Axis (按轴删除记录)3 Indexing, Selection, and Filtering(索引&#xff0c;选择&#xff0c;过滤)Selection with loc and i…...

pyspark连接mysql数据库报错

使用pyspark连接mysql数据库代码如下 spark_conf SparkConf().setAppName("MyApp").setMaster("local")spark SparkSession.builder.config(confspark_conf).getOrCreate()url "jdbc:mysql://localhost:3306/test?useUnicodetrue&characterE…...

HK WEB3 MONTH Polkadot Hong Kong 火热报名中!

HK Web3 Month 11月除了香港金融科技周外&#xff0c;HK Web3 Month又是一大盛事&#xff0c;从10月29日开始开幕直到11月18日结束。此次将齐聚世界各地的Web3产业从业者、开发者、社群成员和学生来参与本次盛会。除外&#xff0c;超过75位产业知名的讲者与超过50场工作坊将为…...

“第六十三天”

这两天怎么做的这么别扭&#xff0c;为什么我的vs 的strlen函数包括终止字符了&#xff1b; 哦哦&#xff0c;明白了&#xff0c;fgets函数读取在未达到指定字长&#xff0c;或者遇见空白符之前&#xff0c;会读取前面的所有字符&#xff0c;所以会读取换行符&#xff0c;而get…...

常用排序算法实现

时间复杂度 O ( 1 ) O(1) O(1) void func1(int n){int count 100;count; } void func2(int n){int count 100;for(int i 0; i < count;i){} } int func3(int n){return n; }O ( n ) O(n) O(n) void func1(int n){int count 100;for(int i 0; i < n;i){count;} …...

使用表单登录方法模拟登录通信人家园,要求发送登录请求后打印出来的用户名下的用户组类别

目标网站&#xff1a;https://www.txrjy.com/forum.php 一、进入网页&#xff0c;右键“检查” 二、输入用户名和密码&#xff0c;点击“登录”&#xff0c;点击“Network”,上划加载项找到蓝色框中的内容 三、点击第一个加载项&#xff0c;找到URL 四、相关代码&#xff1a; …...

Redis 的缓存击穿,穿透,雪崩及其解决方案

1 缓存穿透 什么是缓存穿透&#xff1f; 大量请求的 key 是不合理的&#xff0c;根本不存在于缓存中&#xff0c;也不存在于数据库中 。导致这些请求直接到了数据库上&#xff0c;根本没有经过缓存这一层&#xff0c;对数据库造成了巨大的压力&#xff0c;可能直接就被这么多…...

JWT原理分析——JWT

了解为什么会有JWT的出现&#xff1f; 首先不得不提到一个知识叫做跨域身份验证&#xff0c;JWT的出现就是为了更好的解决这个问题&#xff0c;但是在没有JWT的时候&#xff0c;我们一般怎么做呢&#xff1f;一般使用Cookie和Session&#xff0c;流程大体如下所示&#xff1a;…...

Jprofiler/ VisualVM 定位内存溢出OOM

下载,接受协议下一步下一步,最后选择与IDEA集成OK ej-technologies - Java APM, Java Profiler, Java Installer Builder IDEA配置参数: # F:\study\spring-test\dump 为dump文件保存路径-XX:HeapDumpOnOutOfMemoryError -XX:HeapDumpPathF:\study\spring-test\dumppackage …...

NOIP2023模拟13联测34 competition

题目大意 有一场题目数量为 m m m的比赛&#xff0c;有一个团队想要来参加。 这个团队有 n n n个选手&#xff0c;编号为 i i i的选手能做第 l i ∼ r i l_i \sim r_i li​∼ri​道题&#xff0c;每题他都有 100 % 100\% 100%的概率做出来。 这个团队会随机派出一只队伍来参…...

Intel oneAPI笔记(2)--jupyter官方文档(oneAPI_Intro)学习笔记

前言 本文是对jupyterlab中oneAPI_Essentials/01_oneAPI_Intro文档的学习记录&#xff0c;包含对SYCL、DPC extends SYCL、oneAPI Programming models等介绍和SYCL代码的初步演示等内容 oneAPI编程模型综述 oneAPI编程模型提供了一个全面而统一的开发人员工具组合&#xff0…...

用 QT 开发软件会吃官司吗?

之前我写过我们现在使用 QT 开发跨平台软件&#xff0c;有朋友留言&#xff0c;QT 虽好&#xff0c;当心收到律师函。今天就来聊聊这个话题。 在开始这个话题之前&#xff0c;我们先把使用盗版 QT 排除在外&#xff0c;只讨论在合法且遵从版权协议的前提下&#xff0c;能否使用…...

远程运维用什么软件?可以保障更安全?

远程运维顾名思义就是通过远程的方式IT设备等运行、维护。远程运维适用场景包含因疫情居家办公&#xff0c;包含放假期间出现运维故障远程解决&#xff0c;包含项目太远需要远程操作等等。但远程运维过程存在一定风险&#xff0c;安全性无法保障&#xff0c;所以一定要选择靠谱…...

数据结构与算法C语言版学习笔记(2)-线性表、顺序存储结构的线性表

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 数据结构部分的知识框架一、线性表的定义和特点1.定义2.特点 二、线性表的实际案例引入1.案例一&#xff1a;多项式的加减乘除2.案例二&#xff1a;当多项式是稀疏多…...

【vite】vite.defineConfig is not a function/npm无法安装第三方包问题

当使用vite命令 npm init vite-app 项目名称时配置 import vue from vitejs/plugin-vueexport default defineConfig({plugins: [vue()] })会报错vite.defineConfig is not a function 还有就是npm下载的时候也会报错 原因vite插件vitejs/plugin-vue和vite版本问题 解决 调…...

234. 回文链表 --力扣 --JAVA

题目 给你一个单链表的头节点 head &#xff0c;请你判断该链表是否为回文链表。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 解题思路 判断链表是否为回文链表取决于链表中各个节点的值&#xff0c;所以可以通过存储各节点的值进行对比判断&…...

【JAVA学习笔记】65 - 文件类,IO流--节点流、处理流、对象流、转换流、打印流

项目代码 https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter19/src/com/yinhai 文件 一、文件&#xff0c;流 文件,对我们并不陌生&#xff0c;文件是保存数据的地方,比如大家经常使用的word文档,txt文件,excel文件..都是文件。它既可以保存一张图片…...

R语言 复习 习题图片

这是日天土申哥不知道从哪淘来的R语言复习知识点图片&#xff0c;大部分内容都是课后习题的答案 加油吧&#xff0c;骚年&#xff0c;考个好分数...

c语言 结构体 简单实例

结构体 简单例子 要求&#xff1a; 结构体保存学生信息操作 代码 #include <stdio.h>//定义结构体 struct student{int ID;char name[20];char sex;char birthday[8];int grade; };int main(){int number;printf("请输入学生个数&#xff1a;");scanf(&quo…...

【ChatGPT】ChatGPT的自定义指令

ChatGPT的自定义指令 关于ChatGPT自定义指令的常见问题解答概述可用性如何使用您的数据自定义指令设置将应用于所有新聊天。启动新聊天可查看更改iOS & AndroidWeb 示例常见问题使用自定义指令的好处字符限制我的ChatGPT数据导出中是否包含自定义指令&#xff1f;当我删除我…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

SkyWalking 10.2.0 SWCK 配置过程

SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外&#xff0c;K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案&#xff0c;全安装在K8S群集中。 具体可参…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止

<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet&#xff1a; https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

Java多线程实现之Thread类深度解析

Java多线程实现之Thread类深度解析 一、多线程基础概念1.1 什么是线程1.2 多线程的优势1.3 Java多线程模型 二、Thread类的基本结构与构造函数2.1 Thread类的继承关系2.2 构造函数 三、创建和启动线程3.1 继承Thread类创建线程3.2 实现Runnable接口创建线程 四、Thread类的核心…...

Spring Security 认证流程——补充

一、认证流程概述 Spring Security 的认证流程基于 过滤器链&#xff08;Filter Chain&#xff09;&#xff0c;核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤&#xff1a; 用户提交登录请求拦…...

小木的算法日记-多叉树的递归/层序遍历

&#x1f332; 从二叉树到森林&#xff1a;一文彻底搞懂多叉树遍历的艺术 &#x1f680; 引言 你好&#xff0c;未来的算法大神&#xff01; 在数据结构的世界里&#xff0c;“树”无疑是最核心、最迷人的概念之一。我们中的大多数人都是从 二叉树 开始入门的&#xff0c;它…...