2

I'm doing my programming coursework, and I've come across an issue

gamecentre1 = [winnerscore, winner]


organise = []

organise.extend([gamecentre1])

from operator import attrgetter, itemgetter

sorted(organise, key= itemgetter(0))

print(organise)


f = open("gameresults.txt","w")

f.write("Here are the winners of the games: \n")

f.write(str(organise))

f.write("\n")

f.close()

I'm trying to add two variables to a list, and add that list to another list. Then, I want to organise that larger list based off of the integer variable of the sublist (the integer is the winnerscore). But, the problem is that I haven't been able to organise them properly, and I worry that since I have to append the same list with the same variables to the larger list without overwriting the existing list in it, the larger list will just have the same values over and over again.

This is because I want to store the variables every time the program runs, without getting rid of the values of the variable from the previous game.

How do I do this?

azalea
  • 11,402
  • 3
  • 35
  • 46
  • `sorted` returns a new list, that you're totally ignoring. – jonrsharpe Nov 29 '18 at 21:47
  • `import attrgetter` is also a redundant import, you can get rid. And your imports, unless there is a definitive reason for otherwise, should be right at the top of your script. I don't know how the mark scheme works for your coursework but those details probably should count (easy wins!) – roganjosh Nov 29 '18 at 21:50
  • 1
    Also, please take some time to read [Difference between append vs. extend list methods in Python](https://stackoverflow.com/questions/252703/difference-between-append-vs-extend-list-methods-in-python) because you seem to be using the terms interchangeably. – roganjosh Nov 29 '18 at 21:52
  • Try using print(sorted(organise)) as sorted(organise) is different from organise – Alex Tănăsescu Nov 29 '18 at 21:56
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 29 '18 at 21:56
  • I don't understand the question. Do you just want to create a list and append to it only the unique values? or Do you need to write to a txt file without overwriting? What is your desired output? – Ricky Kim Nov 29 '18 at 22:01

1 Answers1

0

I'm trying to add two variables to a list, and add that list to another list. Then, I want to organise that larger list based off of the integer variable of the sublist

It sounds like what you want is a nested list - a big list of small lists, such that each small list is independent of the others. The key problem in your code right now that's blocking you from accomplishing this is extend() - this is essentially list concatenation, which isn't what you want. For example,

x = [1, 2]
x.extend([3, 4])  # x == [1, 2, 3, 4]

Instead, try using append(), which adds its argument as the next value in the list. For example:

x = []
x.append([3, 4])  # [[3, 4]]
x.append([1, 2])  # [[3, 4], [1, 2]]

Now in the above example, we have a list x that's two elements long, and each of those elements is itself a list of two elements. Now we can plug that into sort:

y = sorted(x, key=lambda i: i[0])
print(y)
# [[1, 2], [3, 4]]

(the lambda i: i[0] is really just a more elegant way of what you're doing with itemgettr(0) - it's a lambda, a small inline function, that takes one argument i and returns the 0th element of i. In this case, the i that gets passed in is one of the smaller lists).

Now you have a sorted list of smaller lists, and you can do whatever you need with them.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53