我有德语文字,我想将所有变音符号(ä,Ä,ü,Ü,ö,Ö)替换为ae,oe,ue等。
我可以单独进行操作(通过将每个替换保存到一个新文件中):
gsub(pattern = '[ä]', replacement = "ae",text)
gsub(pattern = '[ü]', replacement = "ue",text)
gsub(pattern = '[ö]', replacement = "oe",text)
Run Code Online (Sandbox Code Playgroud)
但是我可以在一个命令中做到这一点吗(包括用Ae,Oe和Ue等替换大写字母)?
我可以用正则表达式来做吗?
在我的代码中,我使用了贪婪的算法,以便使用最少的硬币数量.例如:我必须返回0.41美元,我可以使用的最小硬币数量是4:
1 - 0,25;
1 - 0.10;
1 - 0.05;
1 - 0.01;
Run Code Online (Sandbox Code Playgroud)
有4种类型的硬币:0.25,0.10,0.05,0.01.
这是我的代码:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf("Enter the sum, that you want to return you:");
float sum = GetFloat();
float quaters = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
int count_q = 0,count_d = 0,count_n = 0,count_p = 0;
while(sum<0){
printf("Incorrect, enter the positive float number");
sum = GetFloat();
}
while(sum > 0){
if(sum - quaters >=0){
sum …
Run Code Online (Sandbox Code Playgroud) 假设我有以下结构:
t = [['I will','take','care'],['I know','what','to','do']]
Run Code Online (Sandbox Code Playgroud)
正如你在第一个列表我有看到'I will'
,我希望他们分裂成两个元素'I'
和'will'
,ST结果是:
[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]
Run Code Online (Sandbox Code Playgroud)
快速和肮脏的算法如下:
train_text_new = []
for sent in t:
new = []
for word in sent:
temp = word.split(' ')
for item2 in temp:
new.append(item2)
train_text_new.append(new)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更易读且可能更有效的算法来解决这个问题。
假设我有一个列表:[9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9,False]
我想将所有零移到最后.
这是我的代码:
def move_zeros(array):
new = [int(x) if x is 0.0 else x for x in array ]
new.sort(key=lambda item: item is 0)
return new
Run Code Online (Sandbox Code Playgroud)
但是int(x)
不能为0.0
没有第二行而工作我无法移动0.0
到最后.能帮我找到解决方案0.0
吗?
EDITED
我通过eding False
到列表更新了原始帖子
我创建了一个程序,计算棱镜的体积和矩形的面积.但我想让用户决定他想用这个程序多长时间:
#include <iostream>
using namespace std;
int main()
{
bool run = true;
while(run = true){
double len,width,area,volume,height;
char check;
cout << "Please, enter length,width and height of a prism(rectangular)!";
cin >> len >> width >> height;
area = width*len;
volume = area*height;
cout << "The area of rectangle is equal to: " << area << "\n" << "The volume of rectangular prism is equal to: " << volume << "\n";
cout << "Do you want to try again?(y/n)\n";
cin …
Run Code Online (Sandbox Code Playgroud)