python - 根据另一个列表的元素更改一个列表中的元素
问题描述
我有两个清单。第一个列表是代表句子的单词列表:
x = ['i', 'can', 'tell', 'you', 'about', 'the', 'keynote.', 'help', 'you', 'browse', 'sessions.', 'or', 'recommend', 'specific', 'office', 'hours', 'and', 'app', 'reviews.', 'which', 'would', 'you', 'like?']
第二个列表的长度与第一个列表相同,由数字(0、1、2、3 或 4)组成:
y = ['0', '2', '2', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '0', '0', '0', '0', '0', '0', '3', '0', '0']
我需要根据第一个列表中的标点符号将第一个和第二个列表分开。它看起来像:
x1 = ['i', 'can', 'tell', 'you', 'about', 'the', 'keynote.']
y1 = ['0', '2', '2', '0', '0', '0', '0']
x2 = ['help', 'you', 'browse', 'sessions.']
y2 = ['0', '0', '0', '0']
x3 = ['or', 'recommend', 'specific', 'office', 'hours', 'and', 'app', 'reviews.']
y3 = ['1', '1', '1', '0', '0', '0', '0', '0']
x4 = ['which', 'would', 'you', 'like?']
x5 = ['0', '3', '0', '0']
从这些列表中,我需要创建两个列表。在新的第一个列表中,元素将是句子,在第二个新列表中,元素将是数字。如果一个带有数字的列表有任何数字,那么 0 将其放入第二个新列表中,否则将其放入零。它看起来像:
x_new = ["i can tell you about the keynote.", "help you browse sessions.", "or recommend specific office hours and app reviews.", "which would you like?"]
y_new = [2, 0, 1, 3]
注意:如果一个带有数字的列表(例如y1)有多个不同的数字(例如['0', '2', '2', '1', '1', '1', '0']那么取具有更多并发的数字(在此示例中为'1')。如果数字的并发相同(['0', '2', '2', '1', '1', '0', ' 0'])在列表中取第一个非零数字(在本例中为'2')。
这是我的代码:
x = ['i', 'can', 'tell', 'you', 'about', 'the', 'keynote.', 'help', 'you', 'browse', 'sessions.', 'or', 'recommend', 'specific', 'office', 'hours', 'and', 'app', 'reviews.', 'which', 'would', 'you', 'like?']
y = ['0', '2', '2', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '0', '0', '0', '0', '0', '0', '3', '0', '0']
x_new = []
y_new = []
i = 0
k = 0
punctuation_marks = ('.', '?', '!')
# searching punctuation marks
while i < len(x):
for k in range(i, len(x)):
# if a punctuation mark has found
if x[k].endswith(punctuation_marks):
# create list's element for a sentence
x_new += x[i:k]
# cheaking for promises before the punctuation mark
for l in range(i, k):
if y[l] == '1':
y_new += '1'
break
else:
y += '0'
break
break
i = k
但它进入无限循环,我无法弄清楚它有什么问题。另外,我不知道如何计算脚本中数字的并发(见上面的注释)。我的代码只是找到数字列表中的第一个元素。
解决方案
如果您检查起点,那么您的代码将更有效率。
试试这个代码。
x = ['i', 'can', 'tell', 'you', 'about', 'the', 'keynote.', 'help', 'you', 'browse', 'sessions.', 'or', 'recommend', 'specific', 'office', 'hours', 'and', 'app', 'reviews.', 'which', 'would', 'you', 'like?']
y = ['0', '2', '2', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '0', '0', '0', '0', '0', '0', '3', '0', '0']
x_new = []
y_new = []
punctuation_marks = ('.', '?', '!')
s = 0
for i in range(len(x)):
if x[i].endswith(punctuation_marks):
x_new.append(' '.join(x[s:i+1]))
y_new.append(max(list(map(int, y[s:i+1]))))
s = i + 1
推荐阅读
- java - 带有子查询的Spring数据jpa多表连接
- asp.net-core - ASP.NET Core 中的 TraceId、RequestId 和 TraceIdentifier
- .net - 点网单元测试框架工具,用于在点网源代码上自动生成单元测试方法
- javascript - 模态不使用道具关闭
- matlab - 从命令行调用 Matlab 会遇到未知类型的意外异常,为什么?
- c# - 结合字符串中的日期和时间并转换为日期时间
- r - 将列表列表传递给函数的正确方法,缺少和/或不相关的元素
- rust - 映射数字范围 Rust
- plot - 如何限制以前的情节?
- database - 从 AB BA 中删除重复数据