简体   繁体   English

给定一个排序的整数列表,将元素平方并按排序顺序给出输出

[英]Given a sorted list of integers, square the elements and give the output in sorted order

Can anybody help me with this.任何人都可以帮我解决这个问题。 I don't know what am I doing wrong over here我不知道我在这里做错了什么

list = [-9, -2, 0, 2, 3]
squared_list=[]


for element in list:
    squaring = list[element] * list[element]
    squared_list.append(squaring)

squared_list.sort()
print(squared_list)

使用squaring = element * element反对squaring = list[element] * list[element]因为对于第一个元素,例如,您正在咨询 list[-9] 并且该索引已出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果有一个排序的整数列表,那么如果我在Python中使用内置的set函数删除重复项,它将更改元素的顺序。 为什么? - If there is a sorted list of integers, then if i use the inbuilt set function in Python to remove duplicates, it changed the order of elements. WHY? 给定一个长度为 N 的有序整数列表,确定元素 x 是否在列表中 - Given a sorted list of integers of length N, determine if an element x is in the list 在按升序排序的整数列表中添加缺失的数字 - Add missing numbers in a list of integers sorted in ascending order 在列表中添加已排序的元素 - Adding the sorted elements in list 给定一个排序列表,返回尽可能多的元素以达到总数 - Given a sorted list, return as many elements to reach a total 按排序顺序排列字典元素 - arranging dictionary elements in sorted order 将字符串列表['3','1','2']转换为有序整数列表[1,2,3] - Convert a list of strings [ '3', '1', '2' ] to a list of sorted integers [1, 2, 3] 使用split()访问排序列表的元素 - Accessing the elements of a sorted list with split() 获取索引的排序列表,以按给定键排序的字典列表 - Get sorted list of indices, for a list of dictionaries sorted by a given key 如何对反向排序的整数列表重新编号? - How to renumber reverse-sorted list of integers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM