这是我的代码:
Top_5 = []
with open (("winscorefile.txt"),"r") as Top5_File:
for number in Top5_File:
Top_5.append(number)
Top_5.sort()
Top_5.reverse()
number_of_items = len(Top_5)
while number_of_items > 5:
Top_5.pop()
number_of_items = len(Top_5)
for data in Top_5:
print(data)
我的文本文件看起来像(它是分数和用户名的集合):
62 rob
100 rob
56 ROB
54 rob
76 bor
64 bor
52 a
44 p1
54 p1
46 p2
74 p1
52 p2
66 p2
76 p2
38 p2
60 p1
58 p1
我的文件旨在输出前5个最高得分。它正在执行此操作,但仅输出最高的两位数字。我不知道为什么它忽略了100分。
答案 0 :(得分:1)
您可以这样做
$ gradle clean test --console=plain -Dorg.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
...
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.parboiled.transform.AsmUtils (file:/Users/bountin/.gradle/caches/modules-2/files-2.1/org.parboiled/parboiled-java/1.1.7/2298c64ce8ee8e2fb37e97e16d7be52f0c7cf61f/parboiled-java-1.1.7.jar) to method java.lang.ClassLoader.findLoadedClass(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.parboiled.transform.AsmUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
...
- should test fine *** FAILED *** (2 seconds, 210 milliseconds)
java.lang.IllegalArgumentException: Error while instantiating 'org.apache.spark.sql.hive.HiveSessionStateBuilder':
at org.apache.spark.sql.SparkSession$.org$apache$spark$sql$SparkSession$$instantiateSessionState(SparkSession.scala:1075)
at org.apache.spark.sql.SparkSession$$anonfun$sessionState$2.apply(SparkSession.scala:142)
at org.apache.spark.sql.SparkSession$$anonfun$sessionState$2.apply(SparkSession.scala:141)
at scala.Option.getOrElse(Option.scala:121)
at org.apache.spark.sql.SparkSession.sessionState$lzycompute(SparkSession.scala:141)
at org.apache.spark.sql.SparkSession.sessionState(SparkSession.scala:138)
at org.apache.spark.sql.Dataset.<init>(Dataset.scala:171)
at org.apache.spark.sql.Dataset$.apply(Dataset.scala:62)
at org.apache.spark.sql.SparkSession.createDataset(SparkSession.scala:471)
at org.apache.spark.sql.SQLContext.createDataset(SQLContext.scala:377)
...
Cause: java.lang.ClassNotFoundException: java.lang.NoClassDefFoundError: org/apache/hadoop/hive/ql/session/SessionState when creating Hive client using classpath: file:/Users/bountin/classpath-gradle-test-failures/build/classes/java/test, file:/Users/bountin/classpath-gradle-test-failures/build/classes/scala/test/
Please make sure that jars for your version of hive and hadoop are included in the paths passed to spark.sql.hive.metastore.jars.
...
答案 1 :(得分:1)
您可以在空间上分割单行并将第一部分转换为整数。
然后,您可以将数字和此人作为元组添加到列表中:
Top_5 = []
Top5_File = """
62 rob
100 rob
56 ROB
54 rob
76 bor
64 bor
52 a
44 p1
54 p1
46 p2
74 p1
52 p2
66 p2
76 p2
38 p2
60 p1
58 p1
""".strip().split("\n")
for number in Top5_File:
number, person = number.strip().split(" ")
Top_5.append((int(number), person))
# sort only once, outside the loop!
Top_5.sort(reverse=True)
number_of_items = len(Top_5)
while number_of_items > 5:
Top_5.pop()
number_of_items = len(Top_5)
for data in Top_5:
print(data)
输出:
(100, 'rob')
(76, 'bor')
(76, 'p2')
(74, 'p1')
(66, 'p2')
如果您不想使用元组,可以使用:
for number in Top5_File:
Top_5.append(number.strip())
Top_5.sort(key=lambda x: int(x.split(" ")[0]), reverse=True)
number_of_items = len(Top_5)
while number_of_items > 5:
Top_5.pop()
number_of_items = len(Top_5)
for data in Top_5:
print(data)
输出:
100 rob
76 bor
76 p2
74 p1
66 p2
答案 2 :(得分:0)
打开文件后,您需要.read()或.readlines()
Top_5 = []
with open (("winscorefile.txt"),"r") as Top5_File:
for line in Top5_File.readlines():
line = line.strip() # remove white spaces
number, txt = line.split(' ') # txt is useless here
Top_5.append(int(number))
Top_5.sort()
Top_5.reverse()
for data in Top_5[:5]: # this grabs the first 5 elements including 0,1,2,3,4 elements
print(data)
编辑* 如果输入为:76 p2,则line.split('')应该返回两个对象组成的数组(有一个空格),因此看起来您的数据文件具有多个空格作为分隔符。试试这个:
for line in Top5_File.readlines():
line = line.strip() # remove white spaces
if ' ' in line: # check to make sure theres a space in there
splitted = line.split(' ')
number = splitted[0]
Top_5.append(int(number))
答案 3 :(得分:0)
for number in Top5_File:
Top_5.append(number)
Top_5.sort()
Top_5.reverse()
number_of_items = len(Top_5)
基本上number
是您的数据行,即第一个是62 rob
要提取此内容,您必须执行以下操作:
number.split(" ")
会将行拆分为包含以下内容的数组:["62", "rob"]
然后,您必须解析可以使用arr[0] = int(arr[0]
进行操作的int,现在您的行将采用以下形式:< / p>
arr = [62, "rob"]
现在,我建议将(score, name)
的元组添加到您的Top_5数组中,如下所示:
Top_5.append((arr[0], arr[1]))
然后,您将必须更改sort and reverse
,因为您将存储元组,而不仅仅是数组Top_5中的字符串。我相信可以使用key参数和lambda来完成。它看起来像:
sort(key = lambda x: x[0])
,对于反向操作也可以这样做。
我希望这会有所帮助!