3 Replies - 588 Views - Last Post: 03 April 2016 - 01:26 PM Rate Topic: -----

#1 dwang08   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 03-April 16

How to manipulate lists/files to get strings and integers?

Posted 03 April 2016 - 01:04 PM

I'm having a lot of trouble understanding how to complete most of the tasks in this project for my school assignment.

The link to it is here:

http://courses.cs.pu...ojects:project3

Please help me understand, I've looked at the test cases, and I know that I need to iterate over lots of teams/results, but am not sure how to do it. If anyone can show me, I would gladly appreciate it. Someone who knows Python well vs someone with 2 months of experiences should be able to complete this project in an hour maybe, but I want to learn, so no need to give answers. Just sample code/language that I can understand.

Is This A Good Question/Topic? 0
  • +

Replies To: How to manipulate lists/files to get strings and integers?

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11774
  • View blog
  • Posts: 19,985
  • Joined: 19-March 11

Re: How to manipulate lists/files to get strings and integers?

Posted 03 April 2016 - 01:09 PM

I appreciate that you want to learn, so I'm going to help you with that. The first thing you're going to need to do if you want to learn is to learn how to ask questions. Pick one thing that's got you blocked, and ask an actionable question about it. "I don't know how to do it" is not a question, it's a statement.
Was This Post Helpful? 0
  • +
  • -

#3 dwang08   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 03-April 16

Re: How to manipulate lists/files to get strings and integers?

Posted 03 April 2016 - 01:19 PM

I'm using Python 3.5 (required at least 3.4)

For Task 3, I having trouble iterating over each result and count the points because I thought the points were supposed to be in str forming (which I assume we can just cast int to fix that), however, in the sample output, it showed a result in a series of lists, with one e.g ["Mexico, Spain, 3, 4"] all of those values as one string. But I think I am supposed to be able to extract the numbers 3 and 4 out of there, however, I don't know how to do that.

For tasks 1 and 2, I'm reading the files as

f = open("groupInfo.txt, "r")
groupName = f.readlines()
---here I'm not sure what to do next

same thing for task 2
f = open("round0results.txt", "r")
round0results = f.readlines()
for i in round0results:

Here I'm just having issues understanding how to sort a list and get the sample inputs that are shown on the website. The skeleton is on the website for download as well. I appreciate the help, I hope my questions are a bit more specific..

I just realized I said "I don't know how to do that". Rephrasing, I need to extract the scores: How do I do that?
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11774
  • View blog
  • Posts: 19,985
  • Joined: 19-March 11

Re: How to manipulate lists/files to get strings and integers?

Posted 03 April 2016 - 01:26 PM

Quote

I think I am supposed to be able to extract the numbers 3 and 4 out of there, however, I don't know how to do that.


The str.split() method will help you here - it breaks up a string into a list of strings. Assuming all of the lists have this structure, you can split the string on commas and access the resulting list by index.

Example:
>>> "Foo-bar-baz".split("-")
["Foo", "bar", "baz"]
>>> _[1]
"bar"



Quote

groupName = f.readlines()


readlines() returns a plural - a set of lines. groupName suggests a singular item. I suggest you explore what you get back from f.readlines and work from there. It will be a list of strings. Possibly the first of those strings is the one you want? (I'm actually not going to look at your assignment, I'm just going to answer the questions you ask as best I can)

View Postdwang08, on 03 April 2016 - 03:19 PM, said:

I just realized I said "I don't know how to do that". Rephrasing, I need to extract the scores: How do I do that?


I appreciate that correction. +1 for paying attention to what you're writing! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1