gpt4 book ai didi

python - 比较两个列表并确定出现差异的字符串位置

转载 作者:行者123 更新时间:2023-12-04 09:16:09 27 4
gpt4 key购买 nike

我正在尝试编译两个喜欢列表,但将它们之间的差异缩小到单个字符串。
在下面的示例中,我有两个列表:一个包含分组词 lst_a ,还有一个没有 lst_b .
一单lst_b长度也在缩短。
我要给lst_b lst_a的分组,但是当两个列表之间出现差异时,我只想在 lst_b 中有一个不同的刺痛。
这是我的尝试:

# my two original lists
lst_a = ['black blue purple', 'yellow green pink gold', 'silver red', 'white orange brown']
lst_b = ['black', 'blue', 'purple', 'yellow', 'green', 'pink', 'gold', 'silver', 'white', 'orange', 'brown']

# my lists for determining the number of words in each string
number_a = []
number_b = []

# my lists to help create new_lst_b
new_lst_b = []
index = 0

# how many strings are in lst_a
for item in lst_a:
number_a.append(len(item.split()))

# take the number of strings in lst_a and apply create new_lst_b with the same number of stings
for i in number_a:
new_lst_b.append(' '.join(lst_b[index:(index + i)]))
index += i

# how many strings are in new_lst_b
for item_b in new_lst_b:
number_b.append(len(item_b.split()))

# print the position of each string in lst_a and new_lst_b
lst_a_pos = [f"{i}, {v}" for i, v in enumerate(lst_a)]
lst_b_pos = [f"{i}, {v}" for i, v in enumerate(new_lst_b)]

# Finding the difference between lst_a_pos and lst_b_pos
def Diff_lst(lst_a_pos, lst_b_pos):
return list(set(lst_a_pos) - set(lst_b_pos))
diff_lst_b = Diff_lst(lst_b_pos, lst_a_pos)

# Sorting the results of diff_lst_b without including the position
diff_lst_b_sorted = sorted(diff_lst_b, key = lambda x: int(x.split(', ')[0]))

print(new_lst_b)
print(diff_lst_b_sorted)
print(number_a)
print(number_b)
这是输出:
['black blue purple', 'yellow green pink gold', 'silver white', 'orange brown']
['2, silver white', '3, orange brown']
[3, 4, 2, 3]
[3, 4, 2, 2]
这是我想要的输出:
final_lst_b =  ['black blue purple', 'yellow green pink gold', 'silver', ' white orange brown']
我想要做的是找到两个列表之间差异开始的字符串,并计算 new_lst_b 中的单词数。字符串与 lst_a 不同字符串。
我试图用下面的代码计算两个字符串之间的差异,但失败了。
def Diff_item(lst_a[2], new_lst_b[2]):
return list(set(lst_a[2]) - set(new_lst_b[2]))
diff_item_b = Diff_lst(new_lst_b[2], lst_a[2])
我的想法是也许我可以重新计算 lst_bnew_number_b应该是 [3, 4, 1, 3]无论如何,非常感谢任何帮助/方向!

最佳答案

# my two original lists
lst_a = ['black blue purple', 'yellow green pink gold', 'silver red', 'white orange brown']
lst_b = ['black', 'blue', 'purple', 'yellow', 'green', 'pink', 'gold', 'silver', 'white', 'orange', 'brown']

#create a new_list of same length as lst_a
new_list = ['']*len(lst_a)

#split the words in lst_a so you can access each word separately
a = [_.split(' ') for _ in lst_a]

#check each word against lst_b and if it is present, form the new list
for i, l in enumerate(a):
new_list[i] = ' '.join([j for j in l if j in lst_b])

#finally print the new list that contains only items in lst_b
print(new_list)
上面代码的输出是:
['black blue purple', 'yellow green pink gold', 'silver', 'white orange brown']
让我知道这是否适合您。如果您需要任何帮助,请告诉我。感谢您发布问题。为此编写代码很有趣。
下面的代码(全套)可能是您正在寻找的。可能有一些选项供您优化。我没有花足够的时间去寻找他们。但是,它确实为您提供了您想要的。
# my two original lists
lst_a = ['black blue purple', 'yellow green pink gold', 'silver red', 'white orange brown']
lst_b = ['black', 'blue', 'purple', 'temp', 'yellow', 'green', 'pink', 'gold', 'silver', 'white', 'orange', 'brown']
#note: I added 'temp' as a new color to the list

#create a new_list of same length as lst_a
new_list = ['']*len(lst_a)

#split the words in lst_a so you can access each word separately
a = [_.split(' ') for _ in lst_a]

#check each word against lst_b and if it is present, form the new list
for i, l in enumerate(a):
new_list[i] = ' '.join([j for j in l if j in lst_b])

#this section is the addition to find the missing color from lst_b
#convert all the colors in lst_a into single items so you can scan against lst_b
s = [j for i in a for j in i]
#remove any duplicate colors from lst_a
#Easier to process the smaller list
s = list(dict.fromkeys(s))

#now create a list of all items that are in lst_b but not in lst_a
temp = [x for x in lst_b if x not in s]

#now add list temp to new_list to create the full list

new_list += temp

#finally print the new list that contains only items in lst_b
print(new_list)
新列表的输出:
缺少的颜色将显示在最后。希望这是你想要的。
['black blue purple', 'yellow green pink gold', 'silver', 'white orange brown', 'temp']
从 lst_b 实现缺失项的另一种方法:
#now create a list of all items that are in lst_b but not in lst_a
temp = [x for x in lst_b if x not in s]
将其替换为:
temp = list(set(lst_b) - set(s))

关于python - 比较两个列表并确定出现差异的字符串位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63199189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com