如何去除TCL中的字符串中的空格?(How to strip whitespace in string in TCL?)
我需要从TCL中的字符串中去掉前导和尾随空格。 怎么样?
I need to strip leading and trailing whitespace from a string in TCL. How?
原文:https://stackoverflow.com/questions/4657544
更新时间:2024-03-27 14:03
最满意答案
您需要执行以下操作。 将
s
拆分为单词列表。 然后从停用词列表中创建一个哈希值。 然后遍历列表,如果值不是哈希值 - 留下它。
s = "I am 20 years old. I live in New York in United States of America." stop = ["am", "old", "in", "of"] arr = s.split(' ') h = {i: 1 for i in stop} result = [] for i in arr: if i not in h: result.append(i) print ' '.join(result)
You need to do the following. Split the
s
to a list of words by. Then create a hash from the list of stop words. Then iterate through the list and if the value not in hash - leave it.
s = "I am 20 years old. I live in New York in United States of America." stop = ["am", "old", "in", "of"] arr = s.split(' ') h = {i: 1 for i in stop} result = [] for i in arr: if i not in h: result.append(i) print ' '.join(result)
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
从任何顺序的python嵌套列表中删除重复的字符串(Removing duplicate strings from python nested list present in any order)[2023-03-23]
你可以使用enumerate : s = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]] new_s = [a for i, a in enumerate(s) if not any(all(c in h for c in a) for h in s[:i])] 输出: [['hello', 'bye', 'start'], ['john', 'r ... -
您需要执行以下操作。 将s拆分为单词列表 。 然后从停用词列表中创建一个哈希值。 然后遍历列表,如果值不是哈希值 - 留下它。 s = "I am 20 years old. I live in New York in United States of America." stop = ["am", "old", "in", "of"] arr = s.split(' ') h = {i: 1 for i in stop} result = [] for i in arr: if i not in ...
-
您可以使用快速查找set跟踪以前看到的字符串,并使用简单的列表解析来添加先前看到的set找不到的元素。 prev = set() final = [] for i in x: final.append([j for j in i if j not in prev]) prev = prev.union(set(i)) print(final) 输出: [['a', 'b', 'c'], ['e', 'f', 'g'], ['q']] You can keep track of previous ...
-
根据文档 , strip([chars]) : 返回删除了前导和尾随字符的字符串副本。 chars参数是一个字符串,指定要删除的字符集。 因此, "将不会被dat: "13/08/08取代dat: "13/08/08将被替换为时间tim: 12:05:51+22"因为这里"在最后: >>> dat = 'dat: "13/08/08' >>> tim = 'tim: 12:05:51+22"' >>> dat.strip('"') 'dat: "13/08/08' >>> tim.strip('"') 't ...
-
string s是不可变的,因此item.replace('*','')返回带有替换字符的字符串,它不会替换它们(它不能,因为string s是不可变的)。 您可以枚举您的列表,然后将返回的字符串分配回列表 - 示例 - for lst in testList: for j, item in enumerate(lst): lst[j] = item.replace('*', '') 你也可以通过列表理解轻松完成 - testList = [[item.replace('*', ' ...
-
正如nhahtdh指出的那样,由于在字符串的开始和结尾处有一个\n ,因此预计会出现空字符串,但如果它们打扰了您,则可以非常快速和高效地对它们进行filter 。 >>> filter(None, ['', 'text', 'more text', '']) ['text', 'more text'] As nhahtdh pointed out, the empty string is expected since there's a \n at the start and end of the str ...
-
使用remove: myStrings.remove("Alpha"); 请注意,这只会从列表中删除第一次出现的“Alpha”。 Use remove : myStrings.remove("Alpha"); Note that this would only remove the first occurrence of "Alpha" from your list.
-
当您执行x.split() ,输出不会对x本身生效,它会生成一个字符串列表,因为string s不可变: lst = s.split() , 然后加入你想要的物品: e.append(' '.join(lst[2:4])) When you do x.split(), the output does not take effect on x itself, it results in a list of strings, since strings are not mutable: lst = s.spli ...