我试图使用以下标准生成nieghbours子列表:
使用拆分操作的邻居标准
我坚持编写迭代函数来生成结果。
这是我到目前为止所做的:
# initialize
neighlists = []
beginning = []
ending = []
newstart = []
inputlist = [[1], [2], [3, 4], [5, 6]]
# find the biggest element of the list
def find_biggest(inputlist):
biggest = max(inputlist, key=len)
return biggest
biggest = find_biggest(inputlist)
print 'The largest element in the list is: ', biggest
# Function to process biggest component
def process_biggest(biggest):
split_half = len(biggest)//2
part_one = [biggest[:split_half]] + [biggest[split_half:]]
yield part_one
for elem in process_biggest(biggest):
beginning = elem
# Function to process the rest
def process_therest(inputlist):
therest = []
for elem in inputlist:
if len(elem) < len(biggest) or elem is not biggest:
therest.append(elem)
yield therest
for elem in process_therest(inputlist):
ending = elem
# Function to construct new neighbour
def construct_neighbour(beginning,ending):
new_neigh = beginning + ending
yield new_neigh
for neighbour in construct_neighbour(beginning,ending):
# store the values in the neighlist table
neighlists.append(sorted(neighbour))
# use the neighbour as new inputlist
inputlist = neighbour
第一个例子:
第二个例子:
答案 0 :(得分:0)
这样的事情:
input = [[1], [2, 3, 4], [5, 6]]
def splitList(input):
length = len(input)
split = length/2
left = input[:split]
right = input[split:]
return [left, right]
def splitNeighbors(input):
lengths = map(len, input)
longest = max(lengths)
if longest > 1:
index = lengths.index(longest)
split = splitList(input[index])
output = input[:index] + split + input[index+1:]
return splitNeighbors(output)
return input
output = splitNeighbors(input)
print(output)