replace

Better way to remove multiple words from a string?

戏子无情 提交于 2020-03-13 06:31:59
问题 bannedWord = ['Good','Bad','Ugly'] def RemoveBannedWords(toPrint,database): statement = toPrint for x in range(0,len(database)): if bannedWord[x] in statement: statement = statement.replace(bannedWord[x]+' ','') return statement toPrint = 'Hello Ugly Guy, Good To See You.' print RemoveBannedWords(toPrint,bannedWord) The output is Hello Guy, To See You. Knowing Python I feel like there is a better way to implement changing several words in a string. I searched up some similar solutions using

php中正则表达式详解

巧了我就是萌 提交于 2020-03-13 06:24:14
概述 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配、替换、截取匹配的字符串。常用的语言基本上都有正则表达式,如JavaScript、java等。其实,只有了解一种语言的正则使用,其他语言的正则使用起来,就相对简单些。文本主要围绕解决下面问题展开。 有哪些常用的转义字符 什么是限定符与定位符 什么是单词定位符 特殊字符有哪些 什么是逆向引用以及怎样使用逆向引用 匹配模式 php中怎样使用正则表达式 php中哪些方面需要用到正则 怎样进行邮箱匹配,url匹配,手机匹配 怎样使用正则替换字符串中某些字符 贪婪匹配与惰性匹配区别 正则表达式之回溯与固态分组 正则优缺点有哪些 正则表达式的基本知识汇总 行定位符(^与$) 行定位符是用来描述字符串的边界。 “$” 表示行结尾 “^” 表示行开始如 "^de" ,表示以de开头的字符串 "de$" ,表示以de结尾的字符串。 单词定界符 我们在查找的一个单词的时候,如an是否在一个字符串”gril and body”中存在,很明显如果匹配的话,an肯定是可以匹配字符串“gril and body”匹配到,怎样才能让其匹配单词,而不是单词的一部分呢?这时候,我们可以是哟个单词定界符\b。 \ban\b 去匹配”gril and body”的话,就会提示匹配不到。 当然还有一个大写的\B,它的意思,和\b正好相反

F. SUM and REPLACE

跟風遠走 提交于 2020-03-11 14:48:36
F. SUM and REPLACE 题意 定义D数组为一个数的约数个数 ,接下来有两种操作 1操作对于区间[l,r],将al-ar用D(ai)替代。 2操作求区间和。 思路 此题有一个小技巧就是:1e6以内的数都可以在6次求约数个数操作内变为2和1 ,而且1e6以内的最大因字数。 而2的因子个数是2,1的因子个数是1,如果需要更新的位置 所存的数 <=2则就不需要更新了 。 代码实现 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e5+10; ll s[4*maxn];//存放因子个数 ll sum[4*maxn];//存放和 ll mx[4*maxn]; //维护区间最大值 ll node[maxn]; void pushup(int rt){ sum[rt] = sum[rt<<1]+sum[rt<<1|1]; mx[rt] = max(mx[rt<<1],mx[rt<<1|1]); } void build(int l,int r,int rt){ if(l==r){ sum[rt] = mx[rt] = node[l]; return ; } int m = (l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1

51job词云

不想你离开。 提交于 2020-03-09 11:50:19
  爬取的51job上的python岗位任职要求,形成了词云: # coding:utf-8 import jieba #分词 import matplotlib.pyplot as plt #数据可视化 import wordcloud from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS #词云 import numpy as np #科学计算 from PIL import Image #处理图片 #打开文本 text = open("workinfo.txt","rb").read() #rb二进制读取, textfile=text.decode("utf-8") #按照utf-8解码 textfile=textfile.replace("span","").replace("style","").replace("font","").replace("nbsp","") textfile=textfile.replace("line","").replace("height","").replace("color","").replace("family","") textfile=textfile.replace("size","").replace("宋体","").replace("rgb",""

in R, use gsub to remove all punctuation except period

喜欢而已 提交于 2020-03-09 09:38:27
问题 I am new to R so I hope you can help me. I want to use gsub to remove all punctuation except for periods and minus signs so I can keep decimal points and negative symbols in my data. Example My data frame z has the following data: [,1] [,2] [1,] "1" "6" [2,] "2@" "7.235" [3,] "3" "8" [4,] "4" "$9" [5,] "£5" "-10" I want to use gsub("[[:punct:]]", "", z) to remove the punctuation. Current output > gsub("[[:punct:]]", "", z) [,1] [,2] [1,] "1" "6" [2,] "2" "7235" [3,] "3" "8" [4,] "4" "9" [5,]

mysql 操作总结 INSERT和REPLACE

若如初见. 提交于 2020-03-08 11:42:08
--他人总结的 用于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的SELECT语句,另外一种就是更新语句,也叫做数据操作语句。 言外之意,就是对数据进行修改。在标准的SQL中有3个语句,它们是INSERT、UPDATE以及DELETE。在MySQL中又多了一个REPLACE语句,因此,本文以MySQL为背景来讨论如何使有SQL中的更新语句。   一、INSERT和REPLACE   INSERT和REPLACE语句的功能都是向表中插入新的数据。这两条语句的语法类似。它们的主要区别是如何处理重复的数据。   1. INSERT的一般用法   MySQL中的INSERT语句和标准的INSERT不太一样,在标准的SQL语句中,一次插入一条记录的INSERT语句只有一种形式。   INSERT INTO tablename(列名…) VALUES(列值);   而在MySQL中还有另外一种形式。   INSERT INTO tablename SET column_name1 = value1, column_name2 = value2,…;   第一种方法将列名和列值分开了,在使用时,列名必须和列值的数一致。如下面的语句向users表中插入了一条记录:   INSERT INTO users(id, name, age) VALUES(123, '姚明', 25);

xargs中的参数位置

此生再无相见时 提交于 2020-03-08 05:41:16
find . -size +50M -print0 | xargs -0 -I {} cp {} largefolder/ 使用-I参数,后面跟的符号表明用该符号替代find传递过来的值。 man xargs -I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1. 参考: find pwd grep cp xargs组合 xargs cp 的问题 来源: https://www.cnblogs.com/lbsx/archive/2010/10/10/1847306.html

Python dict.set

这一生的挚爱 提交于 2020-03-08 05:38:04
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 举例: >>> d = { 'Michael': 95, 'Bob': 75, 'Tracy': 85} >>> d[ 'Michael'] 95 dict可以按照名字和成绩的对照表,按照名字查成绩。使用 key-value的形式进行存储,速度极高。 dict还可以通过key放入,举例: >>> d[ 'Adam'] = 67 >>> d[ 'Adam'] 67 由于一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉, 如果key不存在,dict就会报错。 要避免key不存在的错误,有两种办法,一是通过 in 判断key是否存在: >>> 'Thomas' in d False 二是通过dict提供的 get() 方法,如果key不存在,可以返回 None ,或者自己指定的value: >>> d.get( 'Thomas') >>> d.get( 'Thomas', - 1) % 后面的参数可以自己定义此例子是-1 - 1 要删除一个key,用 pop(key) 方法,对应的value也会从dict中删除: >>> d.pop( 'Bob') 75 >>> d {

数据转换

社会主义新天地 提交于 2020-03-07 11:49:16
一、移除重复数据 1.1 删除重复行 import pandas as pd df_1 = pd.DataFrame({'k1': ['one', 'two','one']*2, 'k2': ['1','2', '3']*2}) series_1 = df_1.duplicated() df_2 = df_1.drop_duplicates() # 删除重复行,默认判断所有行,默认保留第一次出现的数据 df_3 = df_1[series_1] df_4 = df_1.drop_duplicates(['k1']) # 保留最后一次出现的数据 df_5 = df_1.drop_duplicates(['k1'], keep='last') # 删除重复行,只判读K1列 print('df_1\n', df_1) print('\nseries_1\n', series_1) print('\ndf_2\n', df_2) print('\ndf_3\n', df_3) print('\ndf_4\n', df_4) print('\ndf_5\n', df_5) 二、替换值 利用fillna方法填充缺失数据可以看做值替换的一种特殊情况。 前面已经看到,map可用于修改对象的数据集, 而replace则提供了多种实现该功能的更简单、更灵活的⽅式。 import pandas as

vue设置全局query参数

怎甘沉沦 提交于 2020-03-07 11:18:12
router.beforeEach((to, from, next) => { // 设置全局店铺ID shopid const shopid = from.query.shopid // 如果没有shopid 先跳转到总店 shopid为1 if (!shopid && !to.query.shopid) { router.replace({ path: '/', query: { 'shopid': 1 } }) return false } // 此处通过修改push给query增加shopid的方式, 会重置跳转数据. // 这会影响到replace跳转, 导致变成正常push跳转. // 如果原跳转是replace方式, 需要在query里主动添加shopid. 避免被重置数据. if (shopid && (shopid !== to.query.shopid)) { // 如果传递了shopid 走正常路由 // 否则先手动添加shopid参数 if (!to.query.shopid) { to.query.shopid = shopid router.push({ path: to.path, query: to.query }) return false } } // 设置页面标题 const title = to.meta && to.meta.title