Home > Software engineering >  Add value to a list, extracting the value from a loop and automatically assigning it
Add value to a list, extracting the value from a loop and automatically assigning it

Time:01-02

After the final result of an algorithm which is the list x, i would like to print a variable z where i add to the list x the corresponding values of y_and_value

For example y_and_value is this:

('Hokkaido-Nikko', [1.36])
('Timra-Orebro', [1.44])
('Vaxjo-Lulea', [1.12])
('Yokohama-Tohoku', [1.23])

I would like to print the variable z like this and get these output, because these two hockey matchs are the only two in x:

('Hokkaido Nikko', [1.36])
('Yokohama-Tohoku', [1.23])

How can I get 1.36 and 1.23 assigned to these two matches? (obviously I don't want to manually specify 1.36 and 1.23, because this is just an example and the numbers in the loop can be many, so they have to assign themselves automatically)

My code:

#Final result of an algorithm
x = {('Hokkaido-Nikko', 'JAP'),
     ('Yokohama City-Tohoku', 'JAP')
    }

y = {('Vaxjo-Lulea'),
     ('Hokkaido-Nikko'),
     ('Timra-Orebro'),
     ('Yokohama-Tohoku')}

prox_list = [row for row in y]
print(" ")

for row in prox_list:
    numbers = {(1.23),(1.44),(1.36),(1.12)}

#Assign numbers to y 
result={}
for key, value in zip(prox_list, numbers):
    result.setdefault(key, []).append(value)       
for key, value in result.items():
    y_and_value = key, value
    print(y_and_value)

CodePudding user response:

What you want to achieve is highly unclear, but I'm guessing something like:

x = [('Hokkaido-Nikko', 'JAP'),
     ('Yokohama-Tohoku', 'JAP'),
    ]
y = ['Vaxjo-Lulea',
     'Hokkaido-Nikko',
     'Timra-Orebro',
     'Yokohama-Tohoku',
     ]
numbers = [1.23, 1.44, 1.36, 1.12]

keep = {t[0] for t in x}


result = []
for key, value in zip(y, numbers):
    if key in keep:
        result.append((key, [value]))

print(result)

Output:

[('Hokkaido-Nikko', [1.44]),
 ('Yokohama-Tohoku', [1.12])]

CodePudding user response:

Apologies if I misunderstood your questions.

Assuming that x is an output from another function, I cannot modify it. However, I changed 'Yokohama City-Tohoku' to 'Yokohama-Tohoku' in x to match your question description. I also changed number to a list and reordered the elements inside to match your desired output.

I always find that list comprehension is easier to understand, so I created city_in_japan with it. You actually don't need to create y_and_value.

You may want to research more on the differences between a set (unordered) and a list (ordered) in Python to understand better when to use which.

# x is a set (unordered) with tuples (immutable) inside
x = {('Hokkaido-Nikko', 'JAP'),
     ('Yokohama-Tohoku', 'JAP')
     }

# I changed y to a list (ordered)
y = ['Vaxjo-Lulea',
     'Hokkaido-Nikko',
     'Timra-Orebro',
     'Yokohama-Tohoku']

# I changed numbers to a list (ordered)
numbers = [1.12, 1.36, 1.44, 1.23]

# create list city_in_japan with if-condition
city_in_japan = [(city, [number]) for city, number in zip(y, numbers) if (city, 'JAP') in x]

print(z)

Output:

[('Hokkaido-Nikko', [1.36]), ('Yokohama-Tohoku', [1.23])]
  • Related