appending a line to a list based off of a string found in a previous list

Erik python at lucidity.plus.com
Sat Dec 12 17:20:44 EST 2015


Hi Pedro,

It would be _really useful_ if you included code that could be pasted
into an interpreter or file directly to show your problem. You are
referencing several data sources ("satellite_dataread" and "list2") that
we can only guess at.

On 12/12/15 21:48, Pedro Vincenty wrote:
> Hello, I'm wondering how to append a line from a file onto a
> list(easy part) provided that the line contains strings specific to a
> previous list I've already made(hard part).

The following is a complete version of your example with some guessed-at 
data:

"""
satellite_dataread = (
	"spam ham eggs,ctry0\n",
	"spam ham eggs foo,ctry1\n",
	"spam ham,ctry2\n",
	"spam bar ham eggs,ctry3\n",
)

list2 = ['foo', 'bar']
satellite_origin_list = []

for line in satellite_dataread:
	if any(i in line for i in list2):
		line = line.strip()
		satellite, country= line.split(',')
		satellite_origin_list.append(country)

print (satellite_origin_list)
"""

When I put that in a file and run it, I get:

$ python foo.py
['ctry1', 'ctry3']

$ python3 foo.py
['ctry1', 'ctry3']

So, it seems to work as you describe already - what is it that I am 
missing? Please explain what you expect to see (or change the guessed-at 
input data to an example of what you actually have).

E.



More information about the Python-list mailing list