Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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_Python 3.x_List_Indexing - Fatal编程技术网

Python 如何获取列表中每个索引的平均值,并获取大于平均值的索引?

Python 如何获取列表中每个索引的平均值,并获取大于平均值的索引?,python,python-3.x,list,indexing,Python,Python 3.x,List,Indexing,我有一张名为“你好”的名单: ['Apple is one of the most healthy fruits ever - it grows in many countries', 'Jenny likes strawberry', 'He had an accident in New York Downtown', 'One of the safest cities is the city Tokyo in Japan', 'Some drugs are better than o

我有一张名为“你好”的名单:

['Apple is one of the most healthy fruits ever - it grows in many countries',
 'Jenny likes strawberry',
 'He had an accident in New York Downtown',
 'One of the safest cities is the city Tokyo in Japan',
 'Some drugs are better than others',
 'Ice-cream is famous, Italian ice-cream is the most famous',
 'German cars are known to be the best cars']
我想知道这些句子的平均字符,然后把比平均字符长的句子去掉。如何做到这一点?

您可以使用汇总和列表理解来完成此操作

avg_len = sum(len(str) for str in strings) / len(strings)
filtered = [str for str in strings if len(str) >= avg_len]
请注意,/运算符返回一个浮点,您可能希望根据自己的偏好对其进行四舍五入。

您可以使用“求和”和“列表理解”进行此操作

avg_len = sum(len(str) for str in strings) / len(strings)
filtered = [str for str in strings if len(str) >= avg_len]

请注意,/运算符返回一个浮点,您可能希望根据自己的偏好对其进行四舍五入。

您可以使用列表理解来列出每个长度的长度,然后得到其总和并除以长度:

average = sum([len(x) for x in sentences])/len(sentences)
然后,您可以遍历列表并删除超过该限制的句子

sentences = [sentence for sentence in sentences if len(sentence) > average]

您可以使用列表理解功能列出每个长度的长度,然后得到其总和并除以长度:

average = sum([len(x) for x in sentences])/len(sentences)
然后,您可以遍历列表并删除超过该限制的句子

sentences = [sentence for sentence in sentences if len(sentence) > average]

试着把那些长度大于原始列表中平均句子长度的句子列成一个新的列表

new_sentence_list = []
for i in original_sentence_list:
   if len(i) > mean(len(i) for i in original_sentence_list):
     new_sentence_list.append(i)

试着把那些长度大于原始列表中平均句子长度的句子列成一个新的列表

new_sentence_list = []
for i in original_sentence_list:
   if len(i) > mean(len(i) for i in original_sentence_list):
     new_sentence_list.append(i)

不清楚你在问什么,举例说明你想做什么不清楚你在问什么,举例说明你想做什么