根据 Python 中的出现追加到列表
append to a list based on occurrence in Python
My Python func 获取列表中的每个元素并与原始元素进行比较,如果元素的字母是原始元素,则将相应的字母转换为破折号。这里有一个问题,如果 ORIGINAL 有两个重复的字母,元素有相同的字母重复 3 次,那么只用破折号替换前两个字母。
示例:
ORIGINAL = 'tyyyt'
element = 'yyttt'
result: element = '----t'
我暂定的解决方案是追加到一个看到的列表中,如果有重复,追加第二次。如何根据出现情况将字母附加到列表中?下面是我的代码,它没有按预期工作。谢谢。
ORIGINAL = 'rssrs'
word_list = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
result, modified_list = [], []
for word in word_list:
seen = []
for i in range(len(word)):
if word[i] in ORIGINAL and ORIGINAL.count(word[i]) == 1 and word[i] not in seen:
l = '-'
seen.append(word[i])
elif word[i] in ORIGINAL and ORIGINAL.count(word[i]) in (2,3):
l = '-'
seen.append(word[i])
else:
l = word[i]
result.append(l)
modified_list.append(''.join(result))
result.clear()
print(modified_list)
输出:
['-----', '-----', '-----', '-----']
期望的输出:
['----r', '---rr', '-----', '---s-']
你有一个好主意,虽然它比它需要的更复杂。一种更简单的方法是简单地将每个单词与原始单词的副本进行比较,如果存在该字母,则将其删除。这意味着一封信永远不会被删除太多次。例如:
def occurenceCheck(origin, other):
newWord = ""
replaceChar = "-"
for char in other:
if char in origin:
newWord += replaceChar
origin = origin.replace(char, "", 1)
else:
newWord += char
return newWord
original = "rssrs"
wordList = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
for word in wordList:
print(occurenceCheck(original, word))
运行 这给出了输出:
----r
---rr
-----
---s-
正如 Sam Poirer 所说,它可能更直接,但您的解决方案已接近完成。以下调整会产生所需的输出。
ORIGINAL = 'rssrs'
word_list = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
result, modified_list = [], []
for word in word_list:
seen = []
for i in range(len(word)):
if word[i] in ORIGINAL and ORIGINAL.count(word[i]) == 1 and word[i] not in seen:
l = '-'
seen.append(word[i])
elif (seen.count(word[i]) + 1) <= ORIGINAL.count(word[i]):
l = '-'
seen.append(word[i])
else:
l = word[i]
result.append(l)
modified_list.append(''.join(result))
result.clear()
print(modified_list)
My Python func 获取列表中的每个元素并与原始元素进行比较,如果元素的字母是原始元素,则将相应的字母转换为破折号。这里有一个问题,如果 ORIGINAL 有两个重复的字母,元素有相同的字母重复 3 次,那么只用破折号替换前两个字母。
示例:
ORIGINAL = 'tyyyt'
element = 'yyttt'
result: element = '----t'
我暂定的解决方案是追加到一个看到的列表中,如果有重复,追加第二次。如何根据出现情况将字母附加到列表中?下面是我的代码,它没有按预期工作。谢谢。
ORIGINAL = 'rssrs'
word_list = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
result, modified_list = [], []
for word in word_list:
seen = []
for i in range(len(word)):
if word[i] in ORIGINAL and ORIGINAL.count(word[i]) == 1 and word[i] not in seen:
l = '-'
seen.append(word[i])
elif word[i] in ORIGINAL and ORIGINAL.count(word[i]) in (2,3):
l = '-'
seen.append(word[i])
else:
l = word[i]
result.append(l)
modified_list.append(''.join(result))
result.clear()
print(modified_list)
输出:
['-----', '-----', '-----', '-----']
期望的输出:
['----r', '---rr', '-----', '---s-']
你有一个好主意,虽然它比它需要的更复杂。一种更简单的方法是简单地将每个单词与原始单词的副本进行比较,如果存在该字母,则将其删除。这意味着一封信永远不会被删除太多次。例如:
def occurenceCheck(origin, other):
newWord = ""
replaceChar = "-"
for char in other:
if char in origin:
newWord += replaceChar
origin = origin.replace(char, "", 1)
else:
newWord += char
return newWord
original = "rssrs"
wordList = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
for word in wordList:
print(occurenceCheck(original, word))
运行 这给出了输出:
----r
---rr
-----
---s-
正如 Sam Poirer 所说,它可能更直接,但您的解决方案已接近完成。以下调整会产生所需的输出。
ORIGINAL = 'rssrs'
word_list = ['srrsr', 'rsrrr', 'sssrr', 'ssssr']
result, modified_list = [], []
for word in word_list:
seen = []
for i in range(len(word)):
if word[i] in ORIGINAL and ORIGINAL.count(word[i]) == 1 and word[i] not in seen:
l = '-'
seen.append(word[i])
elif (seen.count(word[i]) + 1) <= ORIGINAL.count(word[i]):
l = '-'
seen.append(word[i])
else:
l = word[i]
result.append(l)
modified_list.append(''.join(result))
result.clear()
print(modified_list)