- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
import pickle
def startover():
#Tells the user what number corresponds with what class.
print('1 = classA, 2 = classB, 3 = classC')
#accepts an input for what class the user would like to view.
Class=int(input('Choose the class you would like to view.'))
#If class a is selected, open class a.
if Class == int('1'):
ClassA=open('Class6A.txt','rb')
clsa=pickle.load(ClassA)
print(clsa)
#If class b is selected, open class b.
if Class == int('2'):
ClassB=open('Class6B.txt','rb')
clsb=pickle.load(ClassB)
print(clsb)
#If class c is selected, open class c.
if Class == int('3'):
ClassC=open('Class6C.txt','rb')
sorttype=int(input('How do you want to sort this data? 1=alphabetical 2=High to low by highest number 3=High to low by average number.'))
clsc=pickle.load(ClassC)
print(clsc)
file=('Class6A.txt','rb')
startover()
我希望能够按字母顺序对用户选择的代码进行排序,以便他们可以根据代码输出的分数轻松找到名字。
Class6a.txt 里面
:€}q (X WHITE SAMq]q(KK KeX WILLIAMS GABRIELLEq]q(K
KKeX
NATHAN WILSONq]q(KK KeX SMITH ISABELLEq]q(KK KeX
CLARK BRANDONq ]q
(KKKeX PATEL SAPNAq]q(KKKeX
BROWN ADAMq
]q(KK
KeX JONES CHLOEq]q(KKKeX
DAVIS RYANq]q(K
KK eX MILLER NOELq]q(KKKeu.
Class6A.txt用python打开:{'WILLIAMS GABRIELLE': [10, 6, 2], 'JONES CHLOE': [7, 5, 5], 'MILLER NOEL': [1, 8, 6], 'CLARK BRANDON': [8, 6, 1], 'NATHAN WILSON': [7, 9, 6], 'SMITH ISABELLE': [2, 9, 6], 'WHITE SAM': [1, 9, 5], 'BROWN ADAM': [8, 10, 6], 'PATEL SAPNA': [7, 8, 7], 'DAVIS RYAN': [10, 6, 9]}
最佳答案
演示:
import pickle
d = {'WILLIAMS GABRIELLE': [10, 6, 2], 'JONES CHLOE': [7, 5, 5],\
'MILLER NOEL': [1, 8, 6], 'CLARK BRANDON': [8, 6, 1],\
'NATHAN WILSON': [7, 9, 6], 'SMITH ISABELLE': [2, 9, 6],\
'WHITE SAM': [1, 9, 5], 'BROWN ADAM': [8, 10, 6], 'PATEL SAPNA': [7, 8, 7],\
'DAVIS RYAN': [10, 6, 9]}
with open("Class6A.txt", "wb") as fp:
pickle.dump(d, fp)
import pickle
import collections
def getAverage(data):
""" Create Average of student data """
records = dict()
for i, j in data.iteritems():
records[i] = (j, float(sum(j))/len(j))
return records
def startover():
#. Tells the user what number corresponds with what class.
print('1 = classA, 2 = classB, 3 = classC')
#. accepts an input for what class the user would like to view.
while 1:
try:
class_no = int(raw_input('Choose the class you would like to view.'))
if class_no not in [1,2,3]:
print "Enter vlaid Class Number."
continue
break
except ValueError:
print "Enter valid Class Numner i.e.digit"
continue
#. If class a is selected, open class a.
if class_no==1:
file_name = 'Class6A.txt'
#If class b is selected, open class b.
elif class_no==2:
file_name = 'Class6B.txt'
#If class c is selected, open class c.
elif class_no==3:
file_name = 'Class6C.txt'
while 1:
try:
sorttype = int(raw_input('How do you want to sort this data? 1=alphabetical\
2=High to low by highest number 3=High to low by average number.'))
if sorttype not in [1,2,3]:
print "Enter vlaid option from the list.."
continue
break
except ValueError:
print "Enter valid option Number i.e. digit"
continue
# fp = open(file_name, 'rb')
# data = pickle.load(fp)
# fp.close()
with open(file_name, "rb") as fp:
data = pickle.load(fp)
#- Add Average value in data.
records = getAverage(data)
if sorttype==1:
student_names = records.keys()
student_names = sorted(student_names)
elif sorttype==3:
student_names = []
tmp = collections.defaultdict(list)
for i, j in records.iteritems():
tmp[j[1]].append(i)
tmp1 = tmp.keys()
tmp1 = sorted(tmp1)
tmp1.reverse()
for i in tmp1:
student_names.extend(tmp[i])
elif sorttype==2:
student_names = []
print "Try your self."
print "Result:"
print "Name\t Marks\t Average"
for i in student_names:
print "%s\t%s\t%s"%(i, records[i][0], records[i][1])
startover()
输出:
1 = classA, 2 = classB, 3 = classC
Choose the class you would like to view.1
How do you want to sort this data? 1=alphabetical 2=High to low by highest number 3=High to low by average number.1
Result:
Name Marks Average
BROWN ADAM [8, 10, 6] 8.0
CLARK BRANDON [8, 6, 1] 5.0
DAVIS RYAN [10, 6, 9] 8.33333333333
JONES CHLOE [7, 5, 5] 5.66666666667
MILLER NOEL [1, 8, 6] 5.0
NATHAN WILSON [7, 9, 6] 7.33333333333
PATEL SAPNA [7, 8, 7] 7.33333333333
SMITH ISABELLE [2, 9, 6] 5.66666666667
WHITE SAM [1, 9, 5] 5.0
WILLIAMS GABRIELLE [10, 6, 2] 6.0
--------------------
1 = classA, 2 = classB, 3 = classC
Choose the class you would like to view.1
How do you want to sort this data? 1=alphabetical 2=High to low by highest number 3=High to low by average number.3
Result:
Name Marks Average
DAVIS RYAN [10, 6, 9] 8.33333333333
BROWN ADAM [8, 10, 6] 8.0
PATEL SAPNA [7, 8, 7] 7.33333333333
NATHAN WILSON [7, 9, 6] 7.33333333333
WILLIAMS GABRIELLE [10, 6, 2] 6.0
SMITH ISABELLE [2, 9, 6] 5.66666666667
JONES CHLOE [7, 5, 5] 5.66666666667
WHITE SAM [1, 9, 5] 5.0
MILLER NOEL [1, 8, 6] 5.0
CLARK BRANDON [8, 6, 1] 5.0
if-elif-else
当我们想检查同一个变量的多个条件时,使用 if elif else。
演示:
>>> a = 51
>>> if a<0:
... print "Number is negative."
... elif a==0:
... print "Number is Zero."
... else:
... print "Number is greater than 0"
...
Number is greater than 0
异常处理:
使用 try expet
在 type casting
期间捕获 Value Error
异常。
演示:
>>> a = raw_input("Enter option:")
Enter option:f
>>> a
'f'
>>> int(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'f'
>>> try:
... int(a)
... except ValueError:
... print "Enter digit only.."
...
Enter digit only..
注意:
在 Python 2.x 中使用 raw_input
为 Python 3.x 使用输入
关于python - 如何使用 python 将保存在 .txt 文件中的分数和名称按字母顺序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29747443/
我正在尝试做这样的事情:Name[i] = "Name"+ (i+1) 在 forloop 中,这样数组的值将是:Name[0] = Name1,Name[1] = Name2,Name[2] = N
我读了here,在GSP中我们可以这样写: ${params.action} 从GSP中,我们可以使用${params.action}作为参数调用Javascript函数(请参阅here)。 是否有其
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
我创建了一条与此类似的路线: Router::connect("/backend/:controller/:action/*"); 现在我想将符合此模式的每个 Controller 路由重命名为类似
我在 Visual Studio 2013 项目中收到以下警告: SQL71502 - Procedure has an unresolved reference to object 最佳答案 这可以
任何人都可以指导我使用名称/值 .NET 集合或 .NET 名称/值字典以获得最佳性能吗?请问最好的方法是什么?我的应用程序是 ASP.NET、WCF/WF Web 应用程序。每个集合应该有 10 到
我在 Zend Framework 2 中有一个默认模块: namespace Application\Controller; use Zend\Mvc\Controller\AbstractActi
这是表格: 关于javascript - 在 javascript 中,这是一个有效的结构吗? : document. 名称.名称.值?,我们在Stack Overflow上找到一个类似的
HtmlHelper.ActionLink(htmlhelper,string linktext,string action) 如何找出正确的路线? 如果我有这个=> HtmlHelper.Actio
我需要一些有关如何将 Controller 定义传递给嵌套在 outer 指令中的 inner 指令的帮助。请参阅http://plnkr.co/edit/Om2vKdvEty9euGXJ5qan一个
请提出一个数据结构来表示内存中的记录列表。每条记录由以下部分组成: 用户名 积分 排名(基于积分)- 可选字段- 可以存储在记录中或可以动态计算 数据结构应该支持高效实现以下操作: Insert(re
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我想要一个包含可变数量函数的函数,但我希望在实际使用它们之前不要对它们求值。我可以使用 () => type 语法,但我更愿意使用 => type 语法,因为它似乎是为延迟评估而定制的。 当我尝试这样
我正在编写一个 elisp 函数,它将给定键永久绑定(bind)到当前主要模式的键盘映射中的给定命令。例如, (define-key python-mode-map [C-f1] 'pytho
卡在R中的错误上。 Error in names(x) <- value : 'names' attribute must be the same length as the ve
我有字符串,其中包含名称,有时在字符串中包含用户名,后跟日期时间戳: GN1RLWFH0546-2020-04-10-18-09-52-563945.txt JOHN-DOE-2020-04-10-1
有人知道为什么我会收到此错误吗?这显示将我的项目升级到新版本的Unity3d之后。 Error CS0103: The name `Array' does not exist in the curre
由于 Embarcadero 的 NNTP 服务器从昨天开始就停止响应,我想我可以在这里问:我使用非数据库感知网格,我需要循环遍历数据集以提取列数、它们的名称、数量行数以及每行中每个字段的值。 我知道
在构建Android应用程序的子项目中,我试图根据根build.gradle中的变量设置版本代码/名称。 子项目build.gradle: apply plugin: 'com.android.app
示例用例: 我有一个带有属性“myProperty”的对象,具有 getter 和 setter(自 EcmaScript 5 起支持“Property Getters 和 Setters”:http
我是一名优秀的程序员,十分优秀!