Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python help

Name: bob 2011-03-22 13:12

So im using python (an older version) and i have a list of country names and an associated numbers, I need to sort these countries from highest to lowest in terms of that number,

Also I need to associate this list with another list, and there are some countries in the second list that are not in the first... durr?????

Name: Anonymous 2011-03-22 13:16

Congratulations, you discovered the Travelling Sussman Problem (names as such because Sussman kept a log file of his travels around the world that he wanted to keep sorted). That's a widely known problem that many beginners discover on their adventure with programming (as the rumor has it, Sussman discovered it when he was 11). Unfortunately, it's NP-hard and you can't do it in a language like python.

Name: Anonymous 2011-03-22 13:17

s:names:named

Name: Anonymous 2011-03-22 13:21

Well I know this can be done in python, its a python assignment and My proff demonstrated it to us....

Name: Anonymous 2011-03-22 13:23

Also I need to associate this list with another list, and there are some countries in the second list that are not in the first... durr?????
What does that mean?
The first question can be done in CL rather easily:
CL-USER> (sort '((99 . country-b) (2 . country-a)) #'< :key #'car)
((2 . COUNTRY-A) (99 . COUNTRY-B))


The second one I don't really understand? Do you need to translate the country names based on some association list or hash table? That would very trivial thing to do.

Name: Anonymous 2011-03-22 13:24

Well then your ``proff'' should see this: http://en.wikipedia.org/wiki/Sussman_Anomaly

Name: Anonymous 2011-03-22 13:32

My problem is i have a huge text file of a few hundred, i can make a lists of lists... but sorting gives me aphabetical...

File one looks like
countryA    200
countryB    120
countryB C  300

And then i have another file wich has the average lat and long for the countries on the earth...
country a 22 00 E 33 00 N

Name: Anonymous 2011-03-22 13:36

He showed us the final result...That he did in python... And i have an idea how to do it, i just dont know the commands and the syntax's...

Name: Anonymous 2011-03-22 13:40

>>8
Read SICP. It teaches you everything you need to know about Python.

Name: Anonymous 2011-03-22 13:46

How about you give the exact problem with example inputs and outputs?
Not that people will answer you in Python.
If you just wanted to translate a list of countries to something they're associated with, that would be trivial, just map over the list.

(mapcar #'translate-country countries)

;; translate-country would look up a key in a hashtable (as an example, it would be anything else) and replace it with a value
(defun translate-country (country)
  (gethash countries *country-to-whatever-hashtable*))

Name: >>10 2011-03-22 13:47

(gethash country *country-to-whatever-hashtable*)Fixed

Name: Anonymous 2011-03-22 13:48

sort -n -k2 hugefile
do it in a system call or whatever

Name: Anonymous 2011-03-22 13:48

>>11
`>implying you can fix lisp

Name: Anonymous 2011-03-22 13:50

>>13
It was a spelling error and you fail at quoting. Oh wait, you're just doing that to troll.

Name: NO EXCEPTIONS 2011-03-22 13:57

>>1

>>> countries = [('USA', 2), ('Murka', 1), ('Merika', 11), ('United States', 0)]
>>> countries
[('USA', 2), ('Murka', 1), ('Merika', 11), ('United States', 0)]

>>> sorted_countries = sorted(countries, key = lambda pair: pair[1])
>>> sorted_countries
[('United States', 0), ('Murka', 1), ('USA', 2), ('Merika', 11)]

>>> additional_data = {'United States': '123123', 'Best Korea': '123123213213'}
>>> extended_countries = [(name, value, additional_data.get(name, '')) for name, value in sorted_countries]
>>> extended_countries
[('United States', 0, '123123'), ('Murka', 1, ''), ('USA', 2, ''), ('Merika', 11, '')]

Name: Anonymous 2011-03-22 13:57

>>15
Stop doing his homework in the same language he requested!

Name: Anonymous 2011-03-22 13:58

>>16
Don't sagespace.

Name: Anonymous 2011-03-22 14:01

<a href="mailto:sage">
I didn't.

Name: Anonymous 2011-03-22 14:02

sorted(countries, key = lambda pair: pair[1])
from operator import itemgetter
sorted(countries, key=itemgetter(1))


is more Pythonic!

Name: Anonymous 2011-03-22 14:05

>>19
That's not even indented.

Name: Anonymous 2011-03-22 16:38

>>16
If it makes you feel better i still have to change that a bit to make it to import files and get that tuple....

Name: Anonymous 2011-03-22 16:42

>>17
You'll find that the correct term is ``space sage''.

Name: Anonymous 2011-03-22 18:25

So im using python...
Stopped reading right there.

Name: Anonymous 2011-03-22 18:51

>>23
So im
Stopped reading right there.

Name: Anonymous 2011-03-22 20:37

>>23
>>24

required to learn for my major....

Name: Anonymous 2011-03-22 21:47

>>25
Don't worry about him, he's an asshole. Python is a perfectly tractable language and will serve you fine should you ever learn to program.
Good luck with your English major, and be sure to drop by if you encounter any more problems!

Don't change these.
Name: Email:
Entire Thread Thread List