The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
List Matching, and outputting to text from multiple lists
Discuss List Matching, and outputting to text from multiple lists in the Python Programming forum on Dev Shed. List Matching, and outputting to text from multiple lists Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 15th, 2012, 03:40 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 17
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 ? ?
|

July 15th, 2012, 08:18 AM
|
 |
Contributing User
|
|
|
|
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!
|

July 15th, 2012, 10:11 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
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.
|

July 15th, 2012, 08:55 PM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 17
Time spent in forums: 5 h 17 m 23 sec
Reputation Power: 0
|
|
Thanks guys, all your solutions worked and fixed both problems. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|