Python List Loops, Print Satellite Names according to Number Enetered after Comma
Question:
UserInput: (“LANDSAT8,5,MODIS,3,SENTINAL2,6”).
User entered three satellites name and instruct the program that how many images of that
particular satellite should be downloaded. In this example, there are three pairs;
- LANDSAT8,5
- MODIS,3
- SENTINAL2,6
Preferred Output should be like:
Program Output:
LANDSAT8 image 1 downloaded
LANDSAT8 image 2 downloaded
LANDSAT8 image 3 downloaded
LANDSAT8 image 4 downloaded
LANDSAT8 image 5 downloaded
MODIS image 1 downloaded
MODIS image 2 downloaded
MODIS image 3 downloaded
SENTINAL2 image 1 downloaded
SENTINAL2 image 2 downloaded
SENTINAL2 image 3 downloaded
SENTINAL2 image 4 downloaded
SENTINAL2 image 5 downloaded
SENTINAL2 image 6 downloaded
Answers:
Overview
you need to get input, split the input on a ,
comma, and then zip them into pairs, then generate your output text and then you can print or do whatever you want with the output array, each element in the output array is a string. using
{x} image {y} downloaded
as the format
Code
def pairwise(iterable):
"s -> (s0, s1), (s2, s3), (s4, s5), ..."
a = iter(iterable)
return zip(a, a)
user_input = input("enter satelites: ")
splitted = user_input.split(',')
output = []
for x, y in pairwise(splitted):
output.append(f"{x} image {y} downloaded");
for x in output: # Do your downloading stuff
print(x)
Output (usage)
inp = input()
words = inp.split(sep=",")
satellites = {}
i = 0
while i < len(words):
satellites[words[i]] = int(words[i + 1])
i += 2
for key, value in satellites.items():
for i in range(1, value + 1):
print(f"{key} image {i} downloaded")
UserInput="LANDSAT8,5,MODIS,3,SENTINAL2,6"
numbers=[]
names=[]
for keyword in UserInput.split(","):
if keyword.isdigit():
numbers.append(keyword)
else:
names.append(keyword)
for i in range(len(names)):
temp=numbers[i]
for j in range(int(temp)):
print(names[i],"image "+str(j)+" downloaded",end=" ")
I think this may be what you’re trying to do:
user_input = "LANDSAT8,5,MODIS,3,SENTINAL2,6"
tokens = user_input.split(',')
for s, d in zip(tokens[::2], map(int, tokens[1::2])):
for i in range(d):
print(f'{s} image {i+1} downloaded')
Output:
LANDSAT8 image 1 downloaded
LANDSAT8 image 2 downloaded
LANDSAT8 image 3 downloaded
LANDSAT8 image 4 downloaded
LANDSAT8 image 5 downloaded
MODIS image 1 downloaded
MODIS image 2 downloaded
MODIS image 3 downloaded
SENTINAL2 image 1 downloaded
SENTINAL2 image 2 downloaded
SENTINAL2 image 3 downloaded
SENTINAL2 image 4 downloaded
SENTINAL2 image 5 downloaded
SENTINAL2 image 6 downloaded
UserInput: (“LANDSAT8,5,MODIS,3,SENTINAL2,6”).
User entered three satellites name and instruct the program that how many images of that
particular satellite should be downloaded. In this example, there are three pairs;
- LANDSAT8,5
- MODIS,3
- SENTINAL2,6
Preferred Output should be like:
Program Output:
LANDSAT8 image 1 downloaded
LANDSAT8 image 2 downloaded
LANDSAT8 image 3 downloaded
LANDSAT8 image 4 downloaded
LANDSAT8 image 5 downloaded
MODIS image 1 downloaded
MODIS image 2 downloaded
MODIS image 3 downloaded
SENTINAL2 image 1 downloaded
SENTINAL2 image 2 downloaded
SENTINAL2 image 3 downloaded
SENTINAL2 image 4 downloaded
SENTINAL2 image 5 downloaded
SENTINAL2 image 6 downloaded
Overview
you need to get input, split the input on a ,
comma, and then zip them into pairs, then generate your output text and then you can print or do whatever you want with the output array, each element in the output array is a string. using
{x} image {y} downloaded
as the format
Code
def pairwise(iterable):
"s -> (s0, s1), (s2, s3), (s4, s5), ..."
a = iter(iterable)
return zip(a, a)
user_input = input("enter satelites: ")
splitted = user_input.split(',')
output = []
for x, y in pairwise(splitted):
output.append(f"{x} image {y} downloaded");
for x in output: # Do your downloading stuff
print(x)
Output (usage)
inp = input()
words = inp.split(sep=",")
satellites = {}
i = 0
while i < len(words):
satellites[words[i]] = int(words[i + 1])
i += 2
for key, value in satellites.items():
for i in range(1, value + 1):
print(f"{key} image {i} downloaded")
UserInput="LANDSAT8,5,MODIS,3,SENTINAL2,6"
numbers=[]
names=[]
for keyword in UserInput.split(","):
if keyword.isdigit():
numbers.append(keyword)
else:
names.append(keyword)
for i in range(len(names)):
temp=numbers[i]
for j in range(int(temp)):
print(names[i],"image "+str(j)+" downloaded",end=" ")
I think this may be what you’re trying to do:
user_input = "LANDSAT8,5,MODIS,3,SENTINAL2,6"
tokens = user_input.split(',')
for s, d in zip(tokens[::2], map(int, tokens[1::2])):
for i in range(d):
print(f'{s} image {i+1} downloaded')
Output:
LANDSAT8 image 1 downloaded
LANDSAT8 image 2 downloaded
LANDSAT8 image 3 downloaded
LANDSAT8 image 4 downloaded
LANDSAT8 image 5 downloaded
MODIS image 1 downloaded
MODIS image 2 downloaded
MODIS image 3 downloaded
SENTINAL2 image 1 downloaded
SENTINAL2 image 2 downloaded
SENTINAL2 image 3 downloaded
SENTINAL2 image 4 downloaded
SENTINAL2 image 5 downloaded
SENTINAL2 image 6 downloaded