我有理解拼接的问题,我想要帮助.
请检查jsfiddle.
http://jsfiddle.net/fantill/TbpWf/1/
value = "c, a, b"
value = value.split(',').splice(1, 1).join(',')
alert(value);
Run Code Online (Sandbox Code Playgroud)
这个价值应该有回报'c, b'.
然而,它返回'a';
这种方法有什么问题?
非常感谢你.
我在Clojure中有一个字符串,我想在第n个和第(n + 1)个字符之间插入一个字符.例如:假设字符串是"aple",我想在"p"和"l"之间插入另一个"p".
(prn
(some-function "aple" "p" 1 2))
;; prints "apple"
;; ie "aple" -> "ap" "p" "le" and the concatenated back together.
Run Code Online (Sandbox Code Playgroud)
我发现这有点挑战,所以我想我错过了一些有用的功能的信息有人可以帮我写上面的"some-function",它带有一个字符串,另一个字符串,一个起始位置和一个结束位置并将第二个字符串插入起始位置和结束位置之间的第一个字符串?提前致谢!
嗨,我正在研究LIME编程,它是javascript的一个子集.
我需要使用javascript.splice从我的数组中删除某些元素,不幸的是,LIME不支持splice函数.
知道我如何创建自己的函数来从数组中删除元素?
谢谢你的时间.
编辑:管理创建一个简单的功能.
function removeElements(array, index)
{
var tempArray = new Array();
var counter = 0;
for(var i = 0; i < array.length; i++)
{
if(i != index)
{
tempArray[counter] = array[i];
counter++;
}
}
return tempArray;
}
Run Code Online (Sandbox Code Playgroud) 我想拼接范围[first, last],包括两个端点.我之前 first和之前都有元素的迭代器last.我能做到splice_after()但只能在线性时间内完成.
我相信这种拼接可以在恒定的时间内完成.我怎么能这样做std::forward_list?
如果问题不明确,这里显示我的问题的示例代码:
实时工作空间代码
#include <algorithm>
#include <forward_list>
#include <iostream>
#include <iterator>
using namespace std;
int main() {
forward_list<char> trg{'a','b','c'};
forward_list<char> src{'1','2','3','4'};
auto before_first = src.begin();
auto last = find(src.begin(), src.end(), '4');
cout << "before_first = " << *before_first << ", last = " << *last << "\n";
// trg.splice(trg.begin(), src, before_first, last); // no such splice
auto end = last;
++end; // Ouch! …Run Code Online (Sandbox Code Playgroud) 我有一个嵌套的JSON结构如下:
[{
"phone_id" : "1",
"phone_name" : "nokia",
"phone_img" : "/src/imgs/nokia.jpg",
"phone_comments" :
[
{
"comment_id" : "1",
"user_id" : "32508",
"comment_date" : "2001-02-01",
"user_comment" : "This was the first phone that was rock solid from Nokia"
},
{
"comment_id" : "2",
"user_id" : "32518",
"comment_date" : "2001-02-02",
"user_comment" : "Great phone before the smartphone age"
},
{
"comment_id" : "3",
"user_id" : "22550",
"comment_date" : "2002-04-01",
"user_comment" : "Reminds me of my grandpa's phone"
},
{
"comment_id" : "4", …Run Code Online (Sandbox Code Playgroud) 我是node.js的新手.
我试图将70000个项目插入数组,然后删除所有项目:
var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();
var a = []
stopwatch.start();
for (var i = 1 ; i < 70000 ; i++){
a.push((parseInt(Math.random() * 10000)) + "test");
}
for (var i = 1 ; i < 70000 ; i++){
a.splice(0,1);
}
stopwatch.stop();
console.log("End: " + stopwatch.elapsedMilliseconds + " : " + a.length);Run Code Online (Sandbox Code Playgroud)
它工作正常,输出是:
PS C:\Users\Documents\VSCode> node test.js
End: 51 : 0
Run Code Online (Sandbox Code Playgroud)
但是当我将项目数增加到72000时,结束需要太多时间:
var Stopwatch = require("node-stopwatch").Stopwatch;
var stopwatch = Stopwatch.create();
var a = []
stopwatch.start();
for …Run Code Online (Sandbox Code Playgroud)我正在尝试编写一个函数,给定一个数组和n,返回数组重复不超过n次的元素.我无法改变数组的顺序.
下面是我到目前为止的代码.令我困惑的是,它适用于给定数组中的大多数元素,但不适用于其他元素.我试图找到代码不起作用的元素的押韵或原因.
function deleteNth(arr,n){
arr.forEach(function (item, index) {
var count = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) {
count++;
while (count > n) {
var remove = arr.lastIndexOf(item);
arr.splice(remove, 1);
count--;
}
}
}
});
return arr;
}
var x = deleteNth([7, 26, 21, 41, 43, 2, 26, 24, 10, 26, 10, 10, 24, 35, 35,
35, 43, 26, 41, 7, 24, 24, 21, 24, 10, 35, 10, 7, 24, 7, …Run Code Online (Sandbox Code Playgroud)在perl中,splice函数从现有数组返回一个新的项目数组,同时从现有数组中删除这些项目.
my @newarry = splice @oldarray, 0, 250;
Run Code Online (Sandbox Code Playgroud)
@newarray现在将包含250条记录,@oldarray并且@oldarray减少250条记录.
是否存在C#集合类的等价物,即具有类似功能的Array,List,Queue,Stack?到目前为止,我只看到了需要两个步骤的解决方案(返回+删除).
更新 - 没有功能存在,所以我实现了一个extensio方法来支持Splice功能:
public static List<T>Splice<T>(this List<T> Source, int Start, int Size)
{
List<T> retVal = Source.Skip(Start).Take(Size).ToList<T>();
Source.RemoveRange(Start, Size);
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
通过以下单元测试 - 成功:
[TestClass]
public class ListTest
{
[TestMethod]
public void ListsSplice()
{
var lst = new List<string>() {
"one",
"two",
"three",
"four",
"five"
};
var newList = lst.Splice(0, 2);
Assert.AreEqual(newList.Count, 2);
Assert.AreEqual(lst.Count, 3);
Assert.AreEqual(newList[0], "one");
Assert.AreEqual(newList[1], "two");
Assert.AreEqual(lst[0], "three");
Assert.AreEqual(lst[1], "four");
Assert.AreEqual(lst[2], …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.6版,我正在学习NumPy 1.3版.
我已经尝试了下面的几个NumPy数组初始化和列拼接示例,并在最后添加了一些内联问题作为注释和结果列表.希望有人可以向我解释行为差异背后的原因.很多相互关联的问题和相当长的帖子,但每个例子都很小,随便回答一个或几个.
import numpy as np
print "Initializing a number of numpy arrays:\n"
Run Code Online (Sandbox Code Playgroud)
a)从元组列表初始化
a = np.zeros((3,),dtype=('i4,i4,a1'))
a[:] = [(1, 2, 'A'), (3, 4, 'B'),(5, 6, 'A')]
print "a: "
print a # print => [(1, 2, 'A') (3, 4, 'B') (5, 6, 'A')]
print repr(a) # print => array([(1, 2, 'A'), (3, 4, 'B'), (5, 6, 'A')],
# dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '|S1')]
print '\n'
Run Code Online (Sandbox Code Playgroud)
b)正常的元组列表
b = [];
b[:] = [(1, 2, 'A'), (3, …Run Code Online (Sandbox Code Playgroud) 我有一个简单的列表如下:
lst = ['11 12221/n']
Run Code Online (Sandbox Code Playgroud)
我想将第一项分成如下列表:
['11', '12221']
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎相对简单,但我不能让它工作.我的第一个方法是:
lst[0].split()
Run Code Online (Sandbox Code Playgroud)
但是当我打印列表时没有发生任何变化.我因此尝试过:
newLst=[]
for x in lst:
newList.append(x.split())
Run Code Online (Sandbox Code Playgroud)
但是我得到了
[['11', '12221\n']]
Run Code Online (Sandbox Code Playgroud)
我认为我必须从根本上误解列表理解,有人可以解释为什么我的代码不起作用以及应该如何完成它?
谢谢
javascript ×4
arrays ×2
python ×2
angularjs ×1
c# ×1
c++ ×1
c++11 ×1
clojure ×1
collections ×1
forward-list ×1
json ×1
lime ×1
list ×1
node.js ×1
numpy ×1
string ×1
v8 ×1
whitespace ×1