Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 给定排序数组和目标值,返回索引_Python - Fatal编程技术网

Python 给定排序数组和目标值,返回索引

Python 给定排序数组和目标值,返回索引,python,Python,给定排序数组和目标值,返回索引。如果不是,则返回按顺序插入的索引位置 我的问题是:-如果target不同,那么如何将target附加到列表中的第I个元素之前?我在问题[1]中明确提到了这一点,请使用模块中的一个函数。它们同时处理命中和未命中。我不明白您要附加什么。你能澄清一下吗?听起来你想要list.insert。元素将在索引处将元素插入nums。索引处和索引后的所有内容都将向右移动一个位置。我已编辑了问题@TeodoricoLevoff class Solution(object):

给定排序数组和目标值,返回索引。如果不是,则返回按顺序插入的索引位置


我的问题是:-如果target不同,那么如何将target附加到列表中的第I个元素之前?我在问题[1]中明确提到了这一点,请使用模块中的一个函数。它们同时处理命中和未命中。

我不明白您要附加什么。你能澄清一下吗?听起来你想要list.insert。元素将在索引处将元素插入nums。索引处和索引后的所有内容都将向右移动一个位置。我已编辑了问题@TeodoricoLevoff
class Solution(object):
    def searchInsert(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: int
    """

        print target
        print nums
        for i in nums:
            print i
            if i==target:
                return nums.index(i)

            else:
                 if i>=target:
                    nums.append('target')-----[1](How to append the target here 
                                                   before the 'i' element to which 
                                                   compared)

                    else:
                        nums.append('target')

                print False