Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › python: how to sort a list and print only words with two characters?
New Posts  All Forums:Forum Nav:

python: how to sort a list and print only words with two characters? - Page 2

post #11 of 17
This works:
Code:
>>> original = ['this', 'is', 'my', 'list', 'of', 'strings']
>>> filter(lambda x: len(x) == 2, original)
['is', 'my', 'of']

or if you want the full function (I haven't tested this but it should work):
Code:
def siftTwo(original):
    return filter(lambda x: len(x) == 2, original)

Edited by 5outh - 8/18/12 at 5:10pm
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #12 of 17
Quote:
Originally Posted by Chris++ View Post

The beauty of Python is that there is only one right way to do stuff, and that is the most intuitive there is.

Are you joking? This is one of the most false statements I've ever read. You will never find a programming language where there is only one right way to accomplish a task.
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #13 of 17
Quote:
Originally Posted by 5outh View Post

This works:
Code:
>>> original = ['this', 'is', 'my', 'list', 'of', 'strings']
>>> filter(lambda x: len(x) == 2, original)
['is', 'my', 'of']
or if you want the full function (I haven't tested this but it should work):
Code:
def siftTwo(original):
    return filter(lambda x: len(x) == 2, original)

I went about it in a much simple way. However a couple more lines of code.
Code:
original = ['this', 'is', 'my', 'list', 'of', 'strings']
others = []

for index in original:
    if len(index) == 2:
        others.append(index)
print others
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #14 of 17
Quote:
Originally Posted by 5outh View Post

Are you joking? This is one of the most false statements I've ever read. You will never find a programming language where there is only one right way to accomplish a task.

There is one right way in Python, the so called "pythonic" code has been proven to be more efficient, reducing the amount of function calls and in many cases reducing the memory footprint.

You may argue that this is minimal and irrelevant with today's hardware, and while I will agree on a trivial example as this one, on a large scale project it will add up.

As a final argument for my statement... and excerpt from the Zen of Python (a text that describes Python's philosophy) "There should be one-- and preferably only one --obvious way to do it.".
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #15 of 17
Thread Starter 
thanks so much guys. I truly learned a lot more from this lol
    
CPUMotherboardGraphicsRAM
Phenom II x4 955BE @3.8ghz Asus M4A79XTD Evo Sapphire 5870 2GB E6 4GB Mushkin Blackline 1600mhz 
Hard DriveOptical DriveCoolingOS
640gb WD | 250gb WD LG dvd reader Prolimatech Megahalems Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
Dell S2209W 22" Lenovo Keyboard (k4803A) SilverStone Decathlon DA700 CM 690 II Advanced 
MouseAudio
Logitech G500 Cochlear Implants and JVC HA-RX700 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom II x4 955BE @3.8ghz Asus M4A79XTD Evo Sapphire 5870 2GB E6 4GB Mushkin Blackline 1600mhz 
Hard DriveOptical DriveCoolingOS
640gb WD | 250gb WD LG dvd reader Prolimatech Megahalems Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
Dell S2209W 22" Lenovo Keyboard (k4803A) SilverStone Decathlon DA700 CM 690 II Advanced 
MouseAudio
Logitech G500 Cochlear Implants and JVC HA-RX700 
  hide details  
Reply
post #16 of 17
Quote:
Originally Posted by Chris++ View Post

There is one right way in Python, the so called "pythonic" code has been proven to be more efficient, reducing the amount of function calls and in many cases reducing the memory footprint.
You may argue that this is minimal and irrelevant with today's hardware, and while I will agree on a trivial example as this one, on a large scale project it will add up.
As a final argument for my statement... and excerpt from the Zen of Python (a text that describes Python's philosophy) "There should be one-- and preferably only one --obvious way to do it.".

Bizarre. I mean, I don't understand why functions such as 'filter' even exist in that case. What's the point of having these functions if they're not really supposed to be used? I guess it doesn't make sense to me.

This is basically the exact instance I would see anyone using 'filter', I'm having a hard time figuring out its utility if you're supposed to iterate using a list comprehension. But I guess that's just Python. I've heard the term "pythonic" several times before, but I always assumed that was a way to express how idiomatic code is, not how "correct" it is. Huh.

Edit: As an aside, the most obvious way for me to tackle this problem was to use 'filter', as I'm coming from a strong functional programming background using Haskell, and in Haskell, we use 'filter' a lot!
Edited by 5outh - 8/20/12 at 12:57pm
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Wings
(9 items)
 
  
CPUMotherboardGraphicsRAM
Core i7 2600k Gigabyte Z68X-UD3H MSI N560-GTX-Ti-Hawk Corsair Vengeance 
Hard DriveCoolingOSPower
Samsung F4 Corsair H100 Windows 7 Ultimate x64 Corsair AX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #17 of 17
Quote:
Originally Posted by 5outh View Post

Bizarre. I mean, I don't understand why functions such as 'filter' even exist in that case. What's the point of having these functions if they're not really supposed to be used? I guess it doesn't make sense to me.
This is basically the exact instance I would see anyone using 'filter', I'm having a hard time figuring out its utility if you're supposed to iterate using a list comprehension. But I guess that's just Python. I've heard the term "pythonic" several times before, but I always assumed that was a way to express how idiomatic code is, not how "correct" it is. Huh.
Edit: As an aside, the most obvious way for me to tackle this problem was to use 'filter', as I'm coming from a strong functional programming background using Haskell, and in Haskell, we use 'filter' a lot!

My only question about using filter is that if you use it on a list does it destroy the old list and create a modified list?

If so that may be a problem if you still need the old list, but could also be considered a good thing if you want to use less data.
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › python: how to sort a list and print only words with two characters?