0

大家好,所以我在这里有一个程序,让用户输入一系列数字,然后将其覆盖到列表中

total = int(input("How many numbers do you want to enter? "))

myList = []
for i in range (total):
    n = int(input("Enter a number >> "))
    myList.append(n)

print ("The list is")
for i in range(total):
    print (myList[i])

它工作正常并且与程序的其余部分兼容,但我需要的是从 myList 创建一个新列表,但是新列表只能包含属于原始数据的 5%- 95% 子范围的数据

我不太确定该怎么做...使用 python 3.2.3 IDLE

4

2 回答 2

0

您可以使用一点随机性从列表中选择任意数量的值:

total = int(input("How many numbers do you want to enter? "))
import random as ran
vList=[]
myList = []
for i in range (total):
    n = int(input("Enter a number >> "))
    myList.append(n)

print myList

x=ran.uniform(1, len(myList))
x=int(round(x))
for i in range(x):
    y=ran.choice(myList)
    myList.remove(y)
    vList.append(y)

print vList

那应该工作:)

于 2012-11-16T23:38:35.043 回答
0

这对你有用吗?

In [72]: L = range(100)

In [73]: L[int(len(L)*0.05):int(len(L)*0.95)]
Out[73]: 
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94]
于 2012-11-16T16:31:46.340 回答