Python 如果第一个和最后一个元素相等,则填写列表
我在尝试转换列表时遇到问题 原始列表如下所示:Python 如果第一个和最后一个元素相等,则填写列表,python,list,Python,List,我在尝试转换列表时遇到问题 原始列表如下所示: [['a','b','c',''],['c','e','f'],['c','g','h']] [['a','b','c','e','f'],['a','b','c','g','h']] 现在我想让输出如下: [['a','b','c',''],['c','e','f'],['c','g','h']] [['a','b','c','e','f'],['a','b','c','g','h']] 当找到空白时,将三个列表合并为两个列表。 我需要写
[['a','b','c',''],['c','e','f'],['c','g','h']]
[['a','b','c','e','f'],['a','b','c','g','h']]
现在我想让输出如下:
[['a','b','c',''],['c','e','f'],['c','g','h']]
[['a','b','c','e','f'],['a','b','c','g','h']]
当找到空白时,将三个列表合并为两个列表。 我需要写一个函数来为我做这件事
以下是我尝试过的:for x in mylist:
if x[len(x) - 1] == '':
m = x[len(x) - 2]
for y in mylist:
if y[0] == m:
combine(x, y)
def combine(x, y):
for m in y:
if not m in x:
x.append(m)
return(x)
但是它没有按我想要的方式工作。试试这个:
mylist = [['a','b','c',''],['c','e','f'],['c','g','h']]
def combine(x, y):
for m in y:
if not m in x:
x.append(m)
return(x)
result = []
for x in mylist:
if x[len(x) - 1] == '':
m = x[len(x) - 2]
for y in mylist:
if y[0] == m:
result.append(combine(x[0:len(x)-2], y))
print(result)
你的问题出在
combine(x[0:len(x)-2], y)
输出:
[['a', 'b', 'c', 'e', 'f'], ['a', 'b', 'c', 'g', 'h']]
您的代码几乎可以工作,除非您从未对联合打印的结果执行任何操作,或将其添加到某个结果列表中,并且您没有删除元素。然而,对于较长的列表,这可能有点慢,因为它具有二次复杂性
相反,您可以使用字典将第一个元素映射到列表的其余元素。然后,您可以使用循环或列表理解来组合具有正确后缀的列表:
lst = [['a','b','c',''],['c','e','f'],['c','g','h']]
import collections
replacements = collections.defaultdict(list)
for first, *rest in lst:
replacements[first].append(rest)
result = [l[:-2] + c for l in lst if l[-1] == "" for c in replacements[l[-2]]]
# [['a', 'b', 'c', 'e', 'f'], ['a', 'b', 'c', 'g', 'h']]
如果列表可以有一个以上的占位符,如果这些可以出现在列表中间,那么事情会变得更复杂一些。您可以将其设置为递归函数。这可以通过使用索引而不是重复切片列表来提高效率
def replace(lst, last=None):
if lst:
first, *rest = lst
if first == "":
for repl in replacements[last]:
yield from replace(repl + rest)
else:
for res in replace(rest, first):
yield [first] + res
else:
yield []
for l in lst:
for x in replace(l):
print(x)
lst的输出=['a'、'b'、'c'、'b'、]、['c'、'b'、'e'、'f']、['c'、'g'、'b'、]、['b'、'x'、'y']:
所以你基本上想要合并两个列表?如果是,您可以使用以下两种方法之一: 使用+运算符或 扩展方法
然后你把它放到一个函数中。我用标准库做的,只有注释。请参考
mylist = [['a','b','c',''],['c','e','f'],['c','g','h']]
# I can't make sure whether the xlist's item is just one or not.
# So, I made it to find all
# And, you can see how to get the last value of a list as [-1]
xlist = [x for x in mylist if x[-1] == '']
ylist = [x for x in mylist if x[-1] != '']
result = []
# combine matrix of x x y
for x in xlist:
for y in ylist:
c = x + y # merge
c = [i for i in c if i] # drop ''
c = list(set(c)) # drop duplicates
c.sort() # sort
result.append(c) # add to result
print (result)
结果是
[['a', 'b', 'c', 'e', 'f'], ['a', 'b', 'c', 'g', 'h']]
试试我的解决办法
虽然它会改变列表的顺序,但它的代码非常简单
lst = [['a', 'b', 'c', ''], ['c', 'e', 'f'], ['c', 'g', 'h']]
lst[0].pop(-1)
print([list(set(lst[0]+lst[1])), list(set(lst[0]+lst[2]))])
不按我想要的方式工作意味着什么?它的作用是什么?你是说是一个标记,指示以下列表的前面应该连接第一个列表吗?是的,这就是我的意思,当循环查找“”时,应该将具有相同最后一个元素的每个列表放在“”及其第一个元素之前。因此,如果第一个列表是['a','c'],那么会发生什么情况?btw x[lenx-a]可以重写为x[-a]。并且x[0:lenx-a]可以重写为x[0:-a]。谢谢你用这个combinex[0:lenx-2],y为我解决它