我正在尝试在两个由引号分隔的短语之间插入逗号。我通常可以使用"the teaching approach that you used during the class" "the way you answered to student's questions"
来完成它,但出于某种原因,在这种情况下,它不起作用。
打印我的对象导致
the teaching approach that you used during the class, the way you answered to student's questions
我需要在一系列可能包含两个或更多短语的表达式中插入逗号。在这种情况下,所需的输出将是
select: hide;
编辑:
具体来说:评论中建议的对象是长度为2的向量。我将其转换为单个向量并使用指示的gsub。
感谢任何帮助
答案 0 :(得分:1)
字符串是length
的向量2.我们用paste
连接它们(toString
是paste(., collapse=", ")
toString(str1)
如果是单个字符串
gsub('" +"', ", ", mystring)
#[1] "the teaching approach that you used during the class, the way you answered to student's questions"
str1 <- c("the teaching approach that you used during the class",
"the way you answered to student's questions" )
mystring <- 'the teaching approach that you used during the class" "the way you answered to student\'s questions'
答案 1 :(得分:1)
我已经意识到这个问题的答案很简单,但是创建OP
提到的样本R
很困难。当然,它将为# How to create that sample mystring?
# Start with single quotes (') and end with single quotes (').
# Make a note: There is single quotes as student's. Hence you need
# escape character before ' in student's.
mystring <- 'the teaching approach that you used during the class" "the way you answered to student\'s questions'
#Check if mystring matches with that of OP
cat(mystring)
# the teaching approach that you used during the class" "the way you answered to student's questions
的新用户提供学习机会。
gsub('(\\"\\s+\\")',", ",mystring)
[1] "the teaching approach that you used during the class, the way you answered to student's questions"
答案:
"
正则表达式:仅隔空格查找x=[ 1151.41 1155.26 1159.11 1162.96 1166.8 1170.65 1174.5 1178.35
1182.19 1186.03 1189.88 1193.72 1197.57 1201.41 1205.25 1209.08
1212.92 1216.77 1220.6 1224.44 1228.27 1232.11 1235.94 1239.78
1243.61 1247.44 1251.27 1255.1 1258.94 1262.77 1266.6 1270.42
1274.25 1278.08 1281.91 1285.73 1289.55 1293.37 1297.2 1301.02
1304.84 1308.66 1312.48 1316.3 1320.12 1323.94 1327.75 1331.57
1335.39 1339.2 1343.01 1346.83 1350.64 1354.45 1358.26 1362.07
1365.88 1369.69 1373.49 1377.31 1381.11 1384.92 1388.72 1392.52
1396.33 1400.13 1403.93 1407.73 1411.53 1415.34 1419.13 1422.93
1426.73 1430.52 1434.32 1438.12 1441.91 1445.7 1449.49 1453.29
1457.08 1460.87 1464.66 1468.45 1472.24 1476.03 1479.81 1483.6
1487.38 1491.17 1494.96 1498.74]
y=[ 3.34835702 3.37753701 3.46421441 3.47561446 3.52906995
3.7430628 3.9445303 4.08069468 4.26772632 4.57141717
4.62653264 4.95543734 5.00090221 5.03820472 4.96421951
4.94593035 4.71712245 4.73136333 4.5449431 4.54198878
4.68326867 4.72099151 4.79832143 4.93313301 5.23041882
5.67948661 6.14714543 6.650372 7.289351 7.85672892
8.52692445 9.19116051 10.00674694 10.54570038 10.87682289
11.35534123 11.42260359 11.19421866 10.80316156 10.50632887
9.62797798 9.1298809 8.28793321 7.73275794 7.0211812
6.81838292 6.39480371 6.08652915 5.72148564 5.51527137
5.21654715 5.25030395 5.28647568 5.2812212 5.36064974
5.41940211 5.10942821 5.10866681 5.03248779 4.87465847
4.62568491 4.60337401 4.57096411 4.56132562 4.55625841
4.81601962 4.91783646 5.40526849 5.7393016 6.44435143
7.27609291 8.17028904 9.53128053 10.71589492 11.74175004
12.82955492 13.1016751 13.1616784 12.64563727 11.91554063
10.84031597 9.74302278 8.93611092 8.01037116 7.37684127
7.08713295 6.77556246 6.8143279 6.85773728 6.87895641
7.02718543 7.10869694]
weights=[ 0.01244515 0.0056474 0.00926648 0.00674491 0.00553186 0.00589553
0.00576142 0.00527653 0.00599983 0.00787368 0.00708644 0.00357355
0.00303342 0.00916585 0.00275235 0.00328612 0.00256573 0.00370105
0.00428572 0.00332175 0.00352448 0.00506917 0.00357058 0.00351362
0.0040452 0.00512055 0.00217692 0.00228909 0.00237594 0.00141826
0.00278388 0.00107677 0.00139026 0.00171414 0.0015294 0.00124306
0.00139236 0.00160101 0.00113102 0.00125891 0.00131655 0.00172122
0.00261752 0.00199811 0.00259736 0.00315851 0.00434901 0.00293013
0.00338841 0.00230605 0.00272563 0.00596109 0.00420324 0.00510059
0.00326837 0.00373911 0.00418471 0.00413105 0.00581997 0.0047461
0.00297461 0.00713362 0.00438222 0.00304455 0.00672188 0.00444613
0.00375129 0.00312274 0.00429026 0.00340182 0.00453605 0.00175878
0.0021822 0.0018159 0.0014724 0.00155655 0.00093935 0.00067046
0.00074241 0.00097113 0.0008329 0.00107734 0.00133742 0.00119752
0.00142113 0.00174815 0.00254059 0.0033123 0.0030637 0.00280913
0.00314901 0.00259063]
import os
import matplotlib.pyplot as plt
import pandas as pd
from lmfit.models import LinearModel
from lmfit.models import GaussianModel
import numpy as np
plotme = True
# import calibrated file
filename = 'file.csv'
data = pd.read_csv('file.csv', sep=",", index_col=[0], header=0)
samplename = os.path.splitext(os.path.basename(filename))[0]
# section out the data to use
data = data.set_index('x_ref', drop=False)
fingerprint = data[data.index > 1150]
fingerprint = fingerprint[fingerprint.index < 1500]
# define the x, y, and weights
x = x
y = y
wv = weights
lin_mod1 = LinearModel(prefix='lin1_')
pars = lin_mod1.guess(y, x=x)
gauss1 = GaussianModel(prefix='one_')
pars.update(gauss1.make_params())
pars['one_center'].set(1200)
pars['one_sigma'].set(2)
pars['one_amplitude'].set(10, min=0)
gauss2 = GaussianModel(prefix='two_')
pars.update(gauss2.make_params())
pars['two_center'].set(1275)
pars['two_sigma'].set(2)
pars['two_amplitude'].set(10, min=0)
gauss3 = GaussianModel(prefix='three_')
pars.update(gauss3.make_params())
pars['three_center'].set(1450)
pars['three_sigma'].set(2)
pars['three_amplitude'].set(10, min=0)
gauss4 = GaussianModel(prefix='unknown_')
pars.update(gauss4.make_params())
pars['unknown_center'].set(1355)
pars['unknown_sigma'].set(2)
pars['unknown_amplitude'].set(10, min=0)
gauss5 = GaussianModel(prefix='unknown2_')
pars.update(gauss5.make_params())
pars['unknown2_center'].set(1510)
pars['unknown2_sigma'].set(2)
pars['unknown2_amplitude'].set(10, min=0)
gauss6 = GaussianModel(prefix='six_')
pars.update(gauss6.make_params())
pars['six_center'].set(1550)
pars['six_sigma'].set(2)
pars['six_amplitude'].set(10, min=0)
gauss7 = GaussianModel(prefix='seven_')
pars.update(gauss7.make_params())
pars['seven_center'].set(1600)
pars['seven_sigma'].set(2)
pars['seven_amplitude'].set(10, min=0)
mod = gauss1 + gauss2 + gauss3 + gauss4 + gauss5 + gauss6 + gauss7 + lin_mod1
init = mod.eval(pars, x=x)
out = mod.fit(y, pars, x=x, weights=wv)
print(out.fit_report())
if plotme:
plt.subplot(2, 1, 1)
plt.plot(x, y, 'bo')
# plt.plot(x, init, 'k--')
plt.plot(x, out.best_fit, 'r-')
comps = out.eval_components(x=x)
plt.plot(x, comps['one_'], '--')
plt.plot(x, comps['two_'], '--')
plt.plot(x, comps['three_'], '--')
plt.plot(x, comps['unknown_'], '--')
plt.plot(x, comps['six_'], '--')
plt.plot(x, comps['unknown2_'], '--')
plt.plot(x, comps['seven_'], '--')
plt.plot(x, comps['lin1_'], '--')
plt.subplot(2, 1, 2)
plt.title('residuals')
plt.plot(x, out.residual, '-')
plt.subplots_adjust(top=0.95, bottom=0.10, left=0.10, right=0.95, hspace=0.50, wspace=0.35)
plt.show()
。如果找到,则替换为“,”。