我使用嵌套的while
循环解决了以下问题。有没有办法以简单的Pythonic方式解决问题?
问题:
定义一个过程,该过程接收来自
1
-9
的一串数字,并输出包含以下参数的列表:
- 字符串中的每个数字都应插入列表中。
- 如果字符串中的数字
x
小于或等于前面的数字y
,则应将数字x
插入子列表中。- 继续将以下数字添加到子列表中,直到达到大于
z
的数字y
。- 然后将此号码
z
添加到正常列表中并继续。他们将后续数据与前一数字进行比较。
例如:
string = '543987' result = [5,[4,3],9,[8,7]] string= '987654321' result = [9,[8,7,6,5,4,3,2,1]] string = '455532123266' result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]]
我的代码:
def numbers_in_lists(string):
array = []
for i in string:
array.append(int(i))
temp_list = []
final_list = [array[0]]
i = 0
while i+1 < len(array):
if array[i] >= array[i+1]:
j = 0
while j+1 < len(array[i:]) and array[i] >= array[i:][j+1]:
temp_list.append(array[i:][j+1])
j += 1
final_list.append(temp_list)
i += len(temp_list)
temp_list = []
else:
final_list.append(array[i+1])
i += 1
#print final_list
return final_list
string = '543987'
print numbers_in_lists(string)
[5, [4, 3], 9, [8, 7]]
答案 0 :(得分:1)
你当然可以简化方法;我会这样做:
def numbers_in_lists(string):
"""Describe what it does here!"""
output = []
sublist = []
for num in map(int, string):
if not output or num > output[-1]:
if sublist:
output.append(sublist)
sublist = []
output.append(num)
else:
sublist.append(num)
if sublist:
output.append(sublist)
return output
使用中:
>>> numbers_in_lists('543987')
[5, [4, 3], 9, [8, 7]]
>>> numbers_in_lists('987654321')
[9, [8, 7, 6, 5, 4, 3, 2, 1]]
>>> numbers_in_lists('455532123266')
[4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]]
答案 1 :(得分:1)
def custom_func(input_string):
if not input_string:
return []
value = input_string[0]
output_list = [int(value)]
for char in input_string[1:]:
if char > value:
# Encountered a value higher than the past maximum
output_list.append(int(char))
else:
if value == str(output_list[-1]):
# First value that is lesser than the past maximum
output_list.append([int(char)])
else:
# nth value which is lesser than the past maximum
output_list[-1].append(int(char))
value = char # updating the past maximum
return output_list
答案 2 :(得分:1)
这是解决方案,您应该考虑。
num = '455532123266'
final_list = [];
prev = 0
sublist = []
for n in num:
n = int(n)
if (n > prev):
if (sublist != []):
final_list.append(sublist)
sublist = []
final_list.append(n)
else:
sublist.append(n)
prev = n
if sublist != []:
final_list.append(sublist)
print final_list
[4, 5, [5, 5, 3, 2, 1], 2, 3, [2], 6, [6]]
答案 3 :(得分:0)
问题定义不明确,因为示例输入并未完全阐明2个问题:
因此,这是我的看法,它不一定是最pythonic,但我无法想出更多的pythonic解决方案。
def numbers_in_lists(string):
current_min = -1 # larger than any digit
rv = []
current_list = []
def push_sublist():
if current_list:
largest = current_list.pop(0)
rv.extend([largest, current_list])
for digit in map(int, string):
if digit > current_min:
push_sublist()
current_list = []
current_min = digit
current_list.append(digit)
push_sublist() # if remaining
return rv
我怀疑,这个问题一点也不清楚。考虑到示例代码如何工作,我设计了一个新算法
def numbers_in_lists(string):
current_min = -1 # larger than any digit
rv = []
current_list = []
def push_sublist():
if current_list:
largest = current_list.pop(0)
rv.append(largest)
if current_list:
rv.append(current_list)
for digit in map(int, string):
if digit > current_min:
push_sublist()
current_list = []
current_min = digit
current_list.append(digit)
push_sublist() # if remaining
return rv
答案 4 :(得分:0)
我的是懒惰的人:
def num_and_sublist_generator(iterable):
start = -1
sublist = []
for n in iterable:
if n > start:
if sublist:
yield sublist
sublist = []
start = n
yield n
else:
sublist.append(n)
if sublist:
yield sublist
def numbers_in_lists(string):
return list(num_and_sublist_generator(map(int, string)))
答案 5 :(得分:0)
我会考虑以更实用的方式进行。
def solve(input):
pairs = zip(input, input[1:])
sublist = [False] + map(lambda x: x[0] > x[1], pairs)
result = []
for (val, to_sublist) in zip(input, sublist):
if not to_sublist:
result.append(val)
else:
if isinstance(result[-1], list):
result[-1].append(val)
else:
result.append([val])
return result
这允许分离检查to_sublist谓词和结果数据结构的实际构建。据我所知,第一个数字永远不会进入子列表,所以它始终以False开头。
答案 6 :(得分:0)
我能想到的最简单的解决方案是在实际的问题陈述之后:
def numbers_in_lists(string):
output = []
sublist = []
for number in [int(c) for c in string]: # Every number in the string should be inserted into the list.
if output and number <= output[-1]:
"""
- If a number x in the string is less than or equal
to the preceding number y, the number x should be
inserted into a sublist.
- Continue adding the following numbers to the sublist
until reaching a number z that is greater than the number y.
"""
sublist.append(number)
else:
"""
- Then add this number z to the normal list and continue.
"""
if sublist:
output.append(sublist)
sublist = []
output.append(number)
if sublist:
output.append(sublist)
return output
答案 7 :(得分:0)
另一种做法......虽然可能会令人困惑
_val = int(string[0])
my_lis = [_val]
temp_li = []
for i in string[1:]:
_cval = int(i)
if _cval > _val:
if temp_li:
my_lis.append(temp_li[:])
temp_li = []
my_lis.append(_cval)
_val = _cval
else:
temp_li.append(_cval)
_val = temp_li[0]
if temp_li: my_lis.append(temp_li[:])# last value
print string
print my_lis