输入一个列表x={5,6,7,-9,10,12,-13,14,-15}先将由小到大排序,将值为负数的元素平方运算,排序用sorted
时间: 2024-05-27 21:12:33 浏览: 11
()函数实现,代码如下:
x = [5, 6, 7, -9, 10, 12, -13, 14, -15]
x_sorted = sorted(x)
for i in range(len(x_sorted)):
if x_sorted[i] < 0:
x_sorted[i] **= 2
print(x_sorted) # [-225, 25, 36, 49, 100, 144, 196, 196, 225]
相关问题
python将列表的列表按照最后一列排序,并将出现最多次数的10个最后一列输入到文件中
假设我们有一个列表的列表`data`,每个子列表有多个元素,我们需要按照最后一列进行排序,并将出现最多次数的10个最后一列写入到文件中。
首先,我们可以使用`sorted`函数对列表的列表进行排序,具体方法是指定`key`参数为最后一列:
```
sorted_data = sorted(data, key=lambda x: x[-1])
```
接下来,我们可以使用`collections.Counter`模块对最后一列进行计数,并取出前10个最常出现的元素:
```
import collections
counter = collections.Counter([x[-1] for x in data])
top10 = counter.most_common(10)
```
最后,我们可以将这10个元素写入到文件中:
```
with open('output.txt', 'w') as f:
for item in top10:
f.write(str(item[0]) + '\n')
```
完整代码如下:
```
import collections
# 假设我们有一个列表的列表data
data = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15], [16,17,18], [19,20,15], [1,2,6], [4,5,6], [7,8,9]]
# 将data按照最后一列排序
sorted_data = sorted(data, key=lambda x: x[-1])
# 统计最后一列出现次数,并取出前10个最常出现的元素
counter = collections.Counter([x[-1] for x in data])
top10 = counter.most_common(10)
# 将top10写入文件
with open('output.txt', 'w') as f:
for item in top10:
f.write(str(item[0]) + '\n')
```
、源代码的单词及其种别码如下: 类别 单词 编码 关键字 if 1 then 2 do 3 while 4 begin 5 end 6 标识符 以字母开头,后跟字母或者数字 7 常数 所有非负整数 8 运算符 + 9 - 10 * 11 / 12 = 13 > 14 < 15 界符 ; 16 ( 17 ) 18 { 19 } 20 2、采用你熟悉的语言编写,编写能够识别上面单词的词法分析程序。
这里提供一个Python实现的词法分析程序示例:
```python
# 定义关键字和对应的编码
keywords = {
'if': 1,
'then': 2,
'do': 3,
'while': 4,
'begin': 5,
'end': 6
}
# 判断一个字符串是否是标识符
def is_identifier(word):
if not word.isalpha():
return False
return True
# 判断一个字符串是否是常数
def is_constant(word):
try:
int(word)
return True
except ValueError:
return False
# 定义运算符、界符和对应的编码
operators = {
'+': 9,
'-': 10,
'*': 11,
'/': 12,
'=': 13,
'>': 14,
'<': 15
}
delimiters = {
';': 16,
'(': 17,
')': 18,
'{': 19,
'}': 20
}
# 词法分析函数
def lex(input_string):
tokens = [] # 存储识别出的token
i = 0 # 当前读取到的位置
while i < len(input_string):
c = input_string[i] # 当前读取到的字符
if c in operators: # 如果是运算符
tokens.append((operators[c], c)) # 添加到token列表
i += 1
elif c in delimiters: # 如果是界符
tokens.append((delimiters[c], c)) # 添加到token列表
i += 1
elif c.isalpha(): # 如果是字母,可能是关键字或标识符
word = c
j = i + 1
while j < len(input_string) and (input_string[j].isalpha() or input_string[j].isdigit()):
word += input_string[j]
j += 1
if word in keywords: # 如果是关键字
tokens.append((keywords[word], word)) # 添加到token列表
else: # 如果是标识符
tokens.append((7, word)) # 添加到token列表
i = j
elif c.isdigit(): # 如果是数字,可能是常数
word = c
j = i + 1
while j < len(input_string) and input_string[j].isdigit():
word += input_string[j]
j += 1
tokens.append((8, word)) # 添加到token列表
i = j
else: # 如果是其他字符,忽略掉
i += 1
return tokens
```
上面的`lex`函数接受一个字符串作为输入,返回一个二元组的列表,每个二元组表示一个token,第一个元素是该token的编码,第二个元素是该token的字符串表示。例如,对于输入字符串`"if x > 0 then y = 1; end"`,输出结果为:
```
[(1, 'if'), (7, 'x'), (14, '>'), (8, '0'), (2, 'then'), (7, 'y'), (13, '='), (8, '1'), (16, ';'), (6, 'end')]
```