如果您想了解在特定索引处删除char-python和根据索引删除指定得元素的知识,那么本篇文章将是您的不二之选。我们将深入剖析在特定索引处删除char-python的各个方面,并为您解答根据索引删除指
如果您想了解在特定索引处删除char-python和根据索引删除指定得元素的知识,那么本篇文章将是您的不二之选。我们将深入剖析在特定索引处删除char-python的各个方面,并为您解答根据索引删除指定得元素的疑在这篇文章中,我们将为您介绍在特定索引处删除char-python的相关知识,同时也会详细的解释根据索引删除指定得元素的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 在特定索引处删除char-python(根据索引删除指定得元素)
- AngularJS:在特定索引之后重复
- arrays – 在特定索引处交换数组值?
- arrays – 在给定索引处添加值而不删除
- C variadic模板:在索引处删除/替换类型
在特定索引处删除char-python(根据索引删除指定得元素)
我有一个包含两个“ 0”(str)的字符串,我只想删除索引4处的“ 0”(str)
我尝试调用.replace,但是显然删除了所有的“ 0”,并且我找不到能为我删除第4位字符的函数。
有人对我有提示吗?
答案1
小编典典使用切片,重建字符串减去要删除的索引:
newstr = oldstr[:4] + oldstr[5:]
AngularJS:在特定索引之后重复
<label ng-repeat="stones in rocks"> <a href="#">Rock {{$index}}</a> <i></i> </label>
现在我希望的是< i class =“icon-trash”>< / i>只有在索引3之后重复.那是从第四块石头开始,我希望看到回收站.我该如何实现这一目标?
解决方法
<label ng-repeat="stones in rocks"> <a href="#">Rock {{$index}}</a> <ing-show="$index > 2"></i><!--$index is 0-based--> </label>
从版本1.1.5开始,您可以将不需要的元素保留在DOM之外
<ing-if="$index > 2"></i>
arrays – 在特定索引处交换数组值?
var posts = [ ["url": "url0","calledindex": 0],["url": "url1","calledindex": 1],["url": "url2","calledindex": 2],["url": "url3","calledindex": 3] ]
鉴于连接的异步性质(这是我想要的,最快的图像首先加载),图像可能以不同的顺序加载,例如:
url0 url2 url3 url1
但是,如果图像无序加载,则需要根据加载图像的时间重新组织原始的帖子数组.因此,鉴于以上示例,帖子现在应该如下所示:
var posts = [ ["url": "url0","calledindex": 3],"calledindex": 1] ]
在Swift中是否有任何方法可以将特定索引处的数组值与来自不同索引的同一数组的值交换?我首先尝试使用swap函数:
// Index the images load var loadedindex = 0 func connectionDidFinishLoading(connection: NSURLConnection) { // Index of the called image in posts let calledindex = posts["calledindex"] as! Int // Index that the image actually loaded let loadedindex = loadedindex // If the indicies are the same,the image is already in the correct position if loadedindex != calledindex { // If they're not the same,swap them swap(&posts[calledindex],&posts[loadedindex]) } }
然后,我尝试了类似的东西,没有交换功能:
// The post that was actually loaded let loadedPost = posts[calledindex] // The post at the correct index let postAtCorrectIndex = posts[loadedindex] posts[calledindex] = postAtCorrectIndex posts[loadedindex] = loadedPost
但是,在这两种情况下,都没有正确交换数组值.我意识到这是一个逻辑错误,但我没有看到错误究竟在哪里.
据我所知,它第一次正确交换,但新字典有一个不正确的calledindex值,导致它交换回原来的位置.
这个假设可能是完全错误的,我意识到我很难描述这种情况,但我会尽量提供尽可能多的澄清.
我做了一个测试用例,你可以download the source code here.它的代码是:
var allPosts:Array<Dictionary<String,AnyObject>> = [ ["imageURL": "http://i.imgur.com/aLsnGqn.jpg","postTitle":"0"],["imageURL": "http://i.imgur.com/vgTXEYY.png","postTitle":"1"],["imageURL": "http://i.imgur.com/OXzDEA6.jpg","postTitle":"2"],["imageURL": "http://i.imgur.com/ilOKOx5.jpg","postTitle":"3"],] var lastIndex = 0 var threshold = 4 var activeConnections = Dictionary<NSURLConnection,Dictionary<String,AnyObject?>>() func loadBatchInForwardDirection(){ func createConnection(i: Int){ allPosts[i]["calledindex"] = i var post = allPosts[i] let imageURL = NSURL(string: post["imageURL"] as! String) if imageURL != nil { let request = NSMutableuRLRequest(URL: imageURL!,cachePolicy: .ReloadIgnoringLocalCacheData,timeoutInterval: 60) let connection = NSURLConnection(request: request,delegate: self,startImmediately: true) if connection != nil { activeConnections[connection!] = post } } } let startingIndex = lastIndex; for (var i = startingIndex; i < startingIndex + threshold; i++){ createConnection(i) lastIndex++ } } func connection(connection: NSURLConnection,didReceiveData data: NSData) { if activeConnections[connection] != nil { let dataDict = activeConnections[connection]!["data"] if dataDict == nil { activeConnections[connection]!["data"] = NSMutableData(data: data) } else { (activeConnections[connection]!["data"] as! NSMutableData).appendData(data) } } } var loadedindex = 0 func connectionDidFinishLoading(connection: NSURLConnection) { let loadedPost = activeConnections[connection]! activeConnections.removeValueForKey(connection) let data = loadedPost["data"] as? NSData let calledindex = loadedPost["calledindex"] as! Int println(calledindex) swap(&allPosts[calledindex],&allPosts[loadedindex]) //(allPosts[calledindex],allPosts[loadedindex]) = (allPosts[loadedindex],allPosts[calledindex]) loadedindex++ done(loadedindex) } func done(index: Int){ if index == 4 { println() println("Actual: ") println(allPosts[0]["postTitle"] as! String) println(allPosts[1]["postTitle"] as! String) println(allPosts[2]["postTitle"] as! String) println(allPosts[3]["postTitle"] as! String) } } func applicationDidFinishLaunching(aNotification: NSNotification) { loadBatchInForwardDirection() println("Loaded: ") } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application }
输出是:
Loaded:
1
0
2
3Actual:
0
1
2
3
但是,预期的“实际”输出应为:
1 0 2 3
值得注意的是,使用元组代码会导致稍微不稳定的结果,但没有任何与实际顺序相匹配的结果.您可以通过取消注释该行来了解我的意思.
var xs = [1,2,3] (xs[1],xs[2]) = (xs[2],xs[1])
但是你实际上有什么问题与交换?以下应该可以正常工作:
swap(&xs[1],&xs[2])
arrays – 在给定索引处添加值而不删除
myArray=(5 6 13 14)
例如,我想在索引2处添加9,我想得到这个:
myArray=(5 6 9 13 14)
而不是这个:
myArray=(5 6 9 14)
显然,做myArray [2] = 9不起作用.我可以添加它并将其他所有内容从1移到左边,但我正在尝试做一些优化的事情,所以如果有一个没有循环的解决方案,我想知道它:)
解决方法
n=2 myArray=( "${myArray[@]:0:n}" 9 "${myArray[@]:n}")
渐渐地,编写循环并不是更有效,但至少shell会在内部处理循环.
C variadic模板:在索引处删除/替换类型
1)删除索引处的项目:
// Removes item at index N,e.g. Remove<2,a,b,c,d> results in <a,d> template<std::size_t N,typename ...Args> struct Remove { // ??? };
2)替换索引处的项目:
// Replaces item at index N with T,e.g. Replace<2,x,typename T,typename ...Args> struct Replace { // ??? };
3)更换范围内的物品
// Replaces items in range [N1,N2] with T,e.g. ReplaceRange<2,3,x> template<std::size_t N1,std::size_t N2,typename ...Args> struct ReplaceRange { // ??? };
我想让它像这样使用
class is_true { public: bool operator()() const { return true; } }; class is_false { public: bool operator()() const { return false; } }; class And { }; class Or { }; Filter f<is_true,And,is_true,Or,is_false>();
现在,我想将其折叠为:
< FilterOr < Filterand <is_true,is_true>,is_false > >
哪里
template<typename Lhs,typename Rhs> class Filterand { public: bool operator()() const { return Lhs() && Rhs(); } }; template<typename Lhs,typename Rhs> class FilterOr { public: bool operator()() const { return Lhs() || Rhs(); } };
所以,我正在尝试执行以下操作:
class Filter { public: template <typename ...Args> void apply() { holder_ = FilterHolder<typename FoldOperator<Or,FilterOr,typename FoldOperator<And,Filterand,Args...>::type >::type >(); } }
FoldOperator基本上删除了运算符参数,并用Filter类替换运算符,例如对于参数< is_true,我想删除参数(is_true)并用Filter替换运算符(And):Filterand< is_true,is_true>其中参数与从列表中删除的参数相同.所以,我需要替换/删除模板来执行它.
提前致谢.
解决方法
我使用std :: tuple来编组周围的一切.
对于Replace,我使用std :: conditional为每个类型索引选择T或正确的元组元素.
对于Remove,我将tuple_catting的decltype作为一个空元组或包含正确元组元素的元组用于非删除类型索引.
namespace detail{ template<std::size_t N,typename Tuple,std::size_t... Idx> auto replace (std::index_sequence<Idx...>) -> std::tuple<std::conditional_t<N == Idx,T,std::tuple_element_t<N,Tuple>>...>; template<std::size_t N,std::size_t... Idx> auto remove (std::index_sequence<Idx...>) -> decltype(std::tuple_cat( std::declval< std::conditional_t<(N == Idx),std::tuple<>,std::tuple<std::tuple_element_t<Idx,Tuple>> > >()... )); } template <std::size_t N,typename... Args> using Replace = decltype(detail::replace<N,std::tuple<Args...>> (std::index_sequence_for<Args...>{})); template <std::size_t N,typename... Args> using Remove = decltype(detail::remove<N,std::tuple<Args...>> (std::index_sequence_for<Args...>{}));
ReplaceRange留给读者练习.
Live Demo
关于在特定索引处删除char-python和根据索引删除指定得元素的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于AngularJS:在特定索引之后重复、arrays – 在特定索引处交换数组值?、arrays – 在给定索引处添加值而不删除、C variadic模板:在索引处删除/替换类型的相关信息,请在本站寻找。
本文标签: