- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有两个包含相同字符串但拆分不同的列表:
sentences = ["This is a sentence", "so is this"]
phrases = ["This is", "a sentence so", "is this"]
"a sentence so"
Sentencecount=0
Phrasecount=0
for i in sentences:
Sentencecount+=1
for n in phrases:
#code here should check each element with 'sentences' elements and split accordingly
Phrasecount += 1
#expected result: phrases = ["This is", "a sentence", "so", "is this"]
最佳答案
好吧,这更难——更有趣!--比我预料的要多。
from collections import deque
def align_wordlists(words1, words2):
# Split every element of the word lists
# >>> [e.split(" ") for e in ["This is", "a sentence"]]
# [["This", "is"], ["a", "sentence"]]
words1_split = [e.split(" ") for e in words1]
words2_split = [e.split(" ") for e in words2]
# Assert that the flattened lists are identical
assert [word for split in words1_split for word in split] == \
[word for split in words2_split for word in split]
# Create a queue and two tracking lists
Q = deque(enumerate(words2_split))
result = []
splits = []
# Keep track of the current sublist in words1
words1_sublist_id = 0
words1_sublist_offset = 0
# Keep iterating until the queue is empty
while Q:
sublist_id, sublist = Q.popleft()
sublist_len = len(sublist)
words1_sublist_len = len(words1_split[words1_sublist_id])
words1_remaining_len = words1_sublist_len - words1_sublist_offset
if sublist_len <= words1_remaining_len:
# The sublist fits entirely into the current segment in words 1,
# add sublist untouched to resulting list.
result.append(" ".join(sublist))
# Update the sublist tracking
if (words1_sublist_len - words1_sublist_offset - sublist_len) == 0:
# The sublist filled the remaining space
words1_sublist_id += 1
words1_sublist_offset = 0
else:
# The sublist only filled part of the remaining space
words1_sublist_offset += sublist_len
else:
# Only part of the current sublist fits.
# Split the segment at the point where the left
# part fits into the current segment of words1.
# Then add the remaining right list to the front
# of the queue.
left = " ".join(sublist[:words1_remaining_len])
right = sublist[words1_remaining_len:]
result.append(left)
Q.appendleft((sublist_id, right))
# Keep track of splits
splits.append(sublist_id)
# update indices
words1_sublist_id += 1
words1_sublist_offset = 0
# Combine splits into sublists to get desired result
for split in splits:
if isinstance(result[split], str):
result[split:split+2] = [[result[split], result[split + 1]]]
else:
result[split] = result[split] + [result[split + 1]]
del result[split + 1]
return result
>>> words1 = ["This is a sentence", "so is this"]
>>> words2 = ["This is", "a sentence so", "is this"]
>>> align_wordlists(words1, words2)
['This is', ['a sentence', 'so'], 'is this']
>>> words1 = ["This is a longer", "sentence with", "different splits"]
>>> words2 = ["This is", "a longer sentence", "with different splits"]
>>> align_wordlists(words1, words2)
['This is', ['a longer', 'sentence'], ['with', 'different splits']]
>>> words1 = ["This is a longer", "sentence with", "different splits"]
>>> words2 = ["This is", "a longer sentence with different splits"]
>>> align_wordlists(words1, words2)
['This is', ['a longer', 'sentence with', 'different splits']]
words1
和
words2
中的词组分成子列表。我们一开始就这样做,因为这样以后处理短语中的单个单词就更容易了。
def align_wordlists(words1, words2):
# Split every element of the word lists
# >>> [e.split(" ") for e in ["This is", "a sentence"]]
# [["This", "is"], ["a", "sentence"]]
words1_split = [e.split(" ") for e in words1]
words2_split = [e.split(" ") for e in words2]
# Assert that the flattened lists are identical
assert [word for split in words1_split for word in split] == \
[word for split in words2_split for word in split]
deque
,它是python
collections
库的一部分。
# Create a queue and two tracking lists
Q = deque(enumerate(words2_split))
result = []
splits = []
enumerate
。
# Keep track of the current sublist in words1
words1_sublist_id = 0
words1_sublist_offset = 0
# Keep iterating until the queue is empty
while Q:
sublist_id
是子列表在第二个单词列表中的位置的索引,
sublist
是单词的实际列表,即短语。此外,我们还计算短语的长度,稍后我们将需要。
sublist_id, sublist = Q.popleft()
sublist_len = len(sublist)
words1_sublist_id
为0,所以我们正在看第一个单词列表中的第一个组。
words1_sublist_len = len(words1_split[words1_sublist_id])
words1_remaining_len = words1_sublist_len - words1_sublist_offset
if sublist_len <= words1_remaining_len:
result
列表中(我在一个空格
join
上将短语组合成一个字符串)。
# The sublist fits entirely into the current segment in words 1,
# add sublist untouched to resulting list.
result.append(" ".join(sublist))
# Update the sublist tracking
if (words1_sublist_len - words1_sublist_offset - sublist_len) == 0:
# The sublist filled the remaining space
words1_sublist_id += 1
words1_sublist_offset = 0
else:
# The sublist only filled part of the remaining space
words1_sublist_offset += sublist_len
else:
# Only part of the current sublist fits.
# Split the segment at the point where the left
# part fits into the current segment of words1.
# Then add the remaining right list to the front
# of the queue.
left = " ".join(sublist[:words1_remaining_len])
right = sublist[words1_remaining_len:]
" "
部分是“完成的”,所以我将它转换成一个字符串。
left
部分还没有完成,我们仍然关心它被拆分成单个单词。)
join
部分推到
right
列表中,因为我们现在知道它在当前句子中完全表示。不过,我们对
left
部分一无所知:它可能适合下一个句子,也可能会溢出那个句子(参见示例4)。
result
部分,所以我们必须把它当作一个新的短语来对待:也就是说,我们只是把它添加到我们的工作队列的前面,以便在下一次运行时进行处理。
result.append(left)
Q.appendleft((sublist_id, right))
right
列表将不包括我们拆分的点,因此我们会跟踪拆分点。
# Keep track of splits
splits.append(sublist_id)
right
-列表中跟踪我们当前的位置。因为我们知道我们已经溢出了当前语句,所以我们可以简单地增加索引并重置偏移量。
# update indices
words1_sublist_id += 1
words1_sublist_offset = 0
# Combine splits into sublists to get desired result
for split in splits:
result
而不是
words1
,因为范围不包括在内。)
if isinstance(result[split], str):
result[split:split+2] = [[result[split], result[split + 1]]]
split+2
追加到列表中,并使用
split+1
删除现在追加的项目。
else:
result[split] = result[split] + [result[split + 1]]
del result[split + 1]
return result
关于python - 有人可以根据与另一个列表的比较来解释拆分列表元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859487/
假设我有这个变量 var image = "image.jpg"; 我正在尝试拆分变量图像的内容并将 _thumbs 插入其中以获得类似 image_thumbs.jpg 的内容。 我该如何解决这个问
我有一个包含多个问题和答案的单元格,其组织方式类似于 CSV。因此,为了将所有这些问题和答案分开,使用逗号作为分隔符的简单拆分应该很容易分开。 不幸的是,有些值使用逗号作为小数分隔符。有没有办法避免这
这是简单的代码: import std.algorithm; import std.array; import std.file; void main(string[] args) { aut
我正在尝试解析一个看起来像的 txt 文件 A - 19 B - 2 C - 3 我正在使用扫描仪方法读取它并在“- ”中拆分,以便我的输出看起来像: A 19 B 2 C 3 但是它似乎没有正确拆分
我有这些网址字符串 file:///home/we/Pictures/neededWord/3193_n.jpg file:///home/smes/Pictures/neededWord/jds_2
我正在解析一个 CVS 文件,如下所示: "07555555555",25.70,18/11/2010,01/03/2011,N,133,0,36,,896,537,547,,Mr,John,Doe,
我在脚本中使用以下行返回 $folder 处所有文件夹的所有路径地点。 dir -recurse $folder|?{$_.PSIsContainer}|select -ExpandProperty
我正在尝试将字符串格式化为word+word+word 例如 “超音乐节”变成“超+音乐+节日” 我尝试过使用以下代码 query.split(" ").join("+"); 或 query.repl
我叫 luis,住在 arg。我有一个问题,无法解决。 **IN BASH** pwd /home/labs-perl ls file1.pl file2.pl **IN PERL** my $ls
我想从包 javax.json 中拆分 JsonArray,但我找不到完成这项工作的便捷方法。我查看了文档,只能想到迭代 JsonArray 并使用 JsonArrayBuilder 手动添加项目。
我希望在第一个 ':' 处拆分字符串,以防止字符串的第二部分包含 ':' 时出现问题。我一直在研究正则表达式,但仍然遇到一些问题,有人可以帮我吗?谢谢。 最佳答案 您可以使用overload of s
我想拆分列表的列表 ((A,1,2,3),(B,4,5,6),(C,7,8,9))进入: (A,1) (A,2) (A,3) (B,4) (B,5) ... 我试过rdd.flatMapValues(
我有一个文本文件,其中每一行都有数据。它看起来像这样: number0;text0 number1;text1 number2;text2 ..等等 所以我通过 xmlhttprequest 将该文本
问题很简单——比如说,我得到了函数,它接收数组作为参数 void calc(double[] data) 如何将这些数据“拆分”成两个子数组并像这样传递给子函数 calc_sub(data(0, le
我想显示来自 EMAIL_TEXT 数据库列的数据,在定义的字符处拆分列。出于某种原因,我的结果只打印第一行到我拆分字符串的位置,跳过其余行。这是我希望在每个“|”之后拆分的数据。 这里是要拆分的数据
我有一个动态数组,我想排除字符串的第一部分,但我不知道第一部分之后会有多少对象,我想将它们全部包含在一个新字符串中。 string = "text.'''hi''','''who''' '''are'
我想拆分 URL 的某些特定部分,这是我目前所做的。 var query = window.location.pathname.split( '/' ); query = window.locati
我有一条消息携带 XML(订单),其中包含多个同质节点(比如产品列表)以及其他信息(比如地址、客户详细信息等)。我必须使用另一个外部服务提供的详细信息来丰富每个“产品”,并返回带有丰富“产品”的相同完
我有一个动态生成的大字符串,我正在拆分它。 var myString="val1, val, val3, val4..... val400" 我对此字符串进行了简单的拆分, myString= myS
这个问题在这里已经有了答案: Java String split removed empty values (5 个答案) 关闭 7 年前。 我正在尝试使用 split(";") 将字符串转换为数组
我是一名优秀的程序员,十分优秀!