Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list to lower-case

I'm trying to get this column of words in input.txt:

Suzuki music
Chinese music
Conservatory
Blue grass
Rock n roll
Rhythm
Composition
Contra
Instruments 

into this format:

"suzuki music", "chinese music", "conservatory music", "blue grass", "rock n roll", "rhythm"...

This code:

with open ('artsplus_stuff.txt', 'r') as f:
    list.append(", ".join(['%s' % row for row in f.read().splitlines()]))
    for item in list:
        item.lower()

print list

returns a list, but the first letters are capitalized.

['Suzuki music, Chinese music, Conservatory, Blue grass, Rock n roll, Rhythm, Composition, Contra, Instruments ']

How do I get all the items lower-cased?

Thanks!


Answer isn't working on this list:

Chess
Guitar
Woodworking
Gardening
Car_restoration
Metalworking
Marksman
Camping
Backpacking_(wilderness)
Hunting
Fishing
Whittling
Geocaching
Sports
Model_Building
Leatherworking
Bowling
Archery
Hiking
Connoisseur
Photography
Pool_(cue_sports)
Mountaineering
Cooking
Blacksmith
Aviator
Magic_(illusion)
Foreign_language
Card_game
Blog
Paintball
Fencing
Brewing
Amateur_Astronomy
Genealogy
Adventure_racing
Knitting
Computer_Programming
Amateur_radio
Audiophile
Baking
Bboying
Baton_twirling
Chainmail
Constructed_language
Coloring
Crocheting
Creative_writing
Drawing
Fantasy_Football
Fishkeeping
Home_automation
Home_Movies
Jewelry
Knapping
Lapidary_club
Locksport
Musical_Instruments
Painting
RC_cars
Scrapbooking
Sculpting
Sewing
Singing
Writing
Air_sports
Boardsport
Backpacking
Bonsai
Canoeing
Cycling
Driving
Freerunning
Jogging
Kayaking
Motor_sports
Mountain_biking
Machining
Parkour
Rock_climbing
Running
Sailing
Sand_castle
Sculling
Rowing_(sport)
Human_swimming
Tai_Chi
Vehicle_restoration
Water_sports
Antiques
Coin_collecting
Element_collecting
Stamp_collecting
Vintage_car
Vintage_clothing
Record_Collecting
Antiquities
Car_audio
Fossil_collecting
Insect_collecting
Leaf
Metal_detectorist
Mineral_collecting
Petal
Rock_(geology)
Seaglass
Seashell
Boxing
Combination_puzzle
Contract_Bridge
Cue_sports
Darts
Table_football
Team_Handball
Airsoft
American_football
Association_football
Auto_racing
Badminton
Climbing
Cricket
Disc_golf
Figure_skating
Footbag
Kart_racing
Plank_(exercise)
Racquetball
Rugby_league
Table_tennis
Microscopy
Reading_(process)
Shortwave_listening
Videophile
Aircraft_spotting
Amateur_geology
Birdwatching
Bus_spotting
Gongoozler
Meteorology
Travel
Board_game
Airbrush
Advocacy
Acting
model_aircraft
Pets
Aquarium
Astrology
Astronomy
Backgammon
Base_Jumping
Sun_tanning
Beachcombing
Beadwork
Beatboxing
Campanology
Belly_dance
cycle_Polo
Bicycle_motocross
Boating
Boomerang
Volunteering
Carpentry
Butterfly_Watching
Button_Collecting
Cake_Decorating
Calligraphy
Candle
Cartoonist
Casino
Cave_Diving
Ceramic
Church
Cigar_Smoking
Cloud_Watching
Antique
Hat
album
Gramophone_record
trading_card
Musical_composition
Worldbuilding
Cosplay
Craft
Cross-Stitch
Crossword_Puzzle
Diecast
Digital_Photography
Dodgeball
Doll
Dominoes
Dumpster_Diving
restaurant
education
Electronics
Embroidery
Entertainment
physical_exercise
Falconry
List_of_fastest_production_cars
Felt
Poi_(performance_art)
Floorball
Floristry
Fly_Tying
off-roading
ultimate_(sport)
Game
Garage_sale
Ghost_Hunting
Glowsticking
Gunsmith
Gyotaku
Handwriting
Hang_gliding
Herping
HomeBrewing
Home_Repair
Home_Theater
Hot_air_ballooning
Hula_Hoop
Ice_skating
Impersonator
Internet
Invention
Jewellery
Jigsaw_Puzzle
Juggling
diary
skipping_rope
amateur_Chemistry
Kite
snowkiting
knot
Laser
Lawn_Dart
poker
Leather_crafting
Lego
Macramé
Model_Car
Matchstick_Model
Meditation
Metal_Detector
Rail_transport_modelling
Model_Rocket
ship_model
scale_model
Motorcycle
Needlepoint
Origami
Papermaking
Papier-mâché
Parachuting
Paragliding
Pinochle
Pipe_Smoking
Pottery
Powerbocking
Demonstration_(people)
Puppetry
Pyrotechnics
Quilting
pigeon_racing
Rafting
Railfan
Rapping
remote_control
Relaxation
Renaissance_Fair
Renting_movies
Robotics
Rock_Balancing
Role-playing
sand_art_and_play
Scuba_Diving
Self-Defense
Skeet_Shooting
Skiing
Shopping
choir
Skateboarding
Sketch_(drawing)
SlackLining
Sleep
Slingshot
Slot_Car_Racing
Snorkeling
Soap
Rubik's_Cube
caving
Family
Storm_Chasing
Storytelling
String_Figure
Surf_Fishing
Survival_skills
Tatting
Taxidermy
Tea_Tasting
Tesla_Coil
Tetris
Textile
stone_Rubbing
Antique_tool
Toy
Treasure_Hunting
Trekkie
tutoring
Urban_Exploration
Video_Game
Violin
Volunteer
Walking
Weightlifting
Windsurfing
WineMaking
Wrestling
Zip-line
traveling

error: list.append(", ".join(['"%s"' % row for row in f.read().splitlines()])) TypeError: descriptor 'append' requires a 'list' object but received a 'str' logout

like image 944
goldisfine Avatar asked Jun 26 '13 19:06

goldisfine


2 Answers

Instead of

for item in list:
    item.lower()

change the name of the variable list to l or whatever you like that isn't a reserved word in Python and use the following line, obviously substituting whatever you name the list for l.

l = [item.lower() for item in l]

The lower method returns a copy of the string in all lowercase letters. Once a string has been created, nothing can modify its contents, so you need to create a new string with what you want in it.

like image 77
Brien Avatar answered Nov 05 '22 07:11

Brien


Here is how it can be done:

In [6]: l = ['Suzuki music', 'Chinese music', 'Conservatory', 'Blue grass']

In [7]: map(str.lower, l)
Out[7]: ['suzuki music', 'chinese music', 'conservatory', 'blue grass']

One of the reasons your code doesn't behave as expected is that item.lower() doesn't modify the string (in Python, strings are immutable). Instead, it returns the lowercase version of the string, which your code then disregards.

like image 26
NPE Avatar answered Nov 05 '22 07:11

NPE