Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 2012, 03:40 AM
jepot jepot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 17 jepot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 17 m 23 sec
Reputation Power: 0
List Matching, and outputting to text from multiple lists

Say I have two lists like:

Code:
a = ['', '2', '3', '4', '']
b = ['1', '5']


List a has strings and has some items which are empty strings
List b has some strings but no empty ones. The length of b is always less than a.

How can I fill list a by checking every item in a to see if it is an empty string, taking the first item out of b (then removing it from b), and using this item in place of the empty string in a? so that I have a list like:

Code:
c = ['1', '2', '3', '4', '5']


Also, say I have 3 lists, all of the same length. How can I print to a text file the first item in list 1, a space, the first item in list 2, another space and the first item in list 3. Then do the same for the next line, only this time use the second item and so on...? Eg

Code:
a = ['1', '2', '3', '4', '5']
b = ['hi', 'are', 'you', 'ok', '?']
c = ['yes', 'i', 'am', 'y', '?']


and output to a txt file...
Code:
1 hi yes
2 are i
3 you am
4 ok y
5 ? ?

Reply With Quote
  #2  
Old July 15th, 2012, 08:18 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
The answer to your second question comes more easily:
Code:
>>> # you need zip
>>> # succinctly:
>>> print('\n'.join(' '.join(t)for t in zip(a,b,c)))
1 hi yes
2 are i
3 you am
4 ok y
5 ? ?
>>> # simplified
>>> for t in zip(a,b,c):
...     print(' '.join(t))
... 
1 hi yes
2 are i
3 you am
4 ok y
5 ? ?
>>> 


Code:
def jepot_substitute(a,b):
    result = a[:]
    j = 0
    for i in range(len(result)):
        if j == len(b):
            break
        if not result[i]:
            result[i] = b[j]
            j += 1
    return result

print(jepot_substitute(['', '2', '3', '4', ''],['1', '5']))
print(jepot_substitute(['', '2', '3', '4', ''],['short',]))
print(jepot_substitute(['', '2', '3', '4', ''],['1','bark','unused']))
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old July 15th, 2012, 10:11 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 18 m 4 sec
Reputation Power: 65
Or even:

Code:
from collections import deque
def jepot_substitute(a, b):
    b = deque(b)
    return [s if s != '' else b.popleft() for s in a]




(Sorry for the multiple edits of this post – I guess this is now the definitive version...)
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Last edited by SuperOscar : July 15th, 2012 at 01:19 PM.

Reply With Quote
  #4  
Old July 15th, 2012, 08:55 PM
jepot jepot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 17 jepot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 17 m 23 sec
Reputation Power: 0
Thanks guys, all your solutions worked and fixed both problems.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > List Matching, and outputting to text from multiple lists

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap