python 语言。菜鸟求助。帮我修改一下代码。能够要求的功能。错误原因是K没定义。在线等。
#D.Givenalistofnumbers,returnalistwhere#alladjacent==elementshavebeenreducedtoasingle...
# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
temp = []
for i in range(len(nums)):
for j in range(len(nums)):
if i<j and nums[i]==nums[j]:
temp.append(j)
for k in temp:
nums.remove(K)
# +++your code here+++
return nums
代码如下。
# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
temp = []
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]==nums[j]:
temp.append(j)
for k in temp:
nums.remove(K)
# +++your code here+++
return nums 展开
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
temp = []
for i in range(len(nums)):
for j in range(len(nums)):
if i<j and nums[i]==nums[j]:
temp.append(j)
for k in temp:
nums.remove(K)
# +++your code here+++
return nums
代码如下。
# D. Given a list of numbers, return a list where
# all adjacent == elements have been reduced to a single element,
# so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
# modify the passed in list.
def remove_adjacent(nums):
temp = []
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]==nums[j]:
temp.append(j)
for k in temp:
nums.remove(K)
# +++your code here+++
return nums 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询