简体   繁体   中英

Finding the index of an element in a list when there are duplicates in python

So for context, there is a popular problem called the "Fibbonaci Clock." Essentially, you have a list of colors, for example ["white","blue","red","green","white"]. The first item in the list holds a value, of 1, then the second holds again a value of 1, the third holds a value of 2, the fourth holds a value of 3, and the 5th holds a value of 5. [1,1,2,3,5]. To find the time of ["white","blue","red","green","white"], you would add the values of Red and Blue to get the hour, and do 5*(Green + Blue) for the minutes. In this case, the blue color is in the second box, meaning it holds a value of 1, and the red value is in the third box, meaning it holds a value of 2. so 1 + 2 = 3, so the hour is 3. The minute is 5*(G + B), green is in the 4th slot, holding a value of 3, and blue is in the second spot, holding a value of 1. 5(3 + 1) = 5(4) = 20. So the time is 3:20.

So I'm trying to write a program for this, but I have a problem. There can be repeats of Red, Green, and Blue. For example, ["Red","Red","Blue","Green","White]. In this case, when adding Red and Blue, you would have to add both values of Red, and Blue. This is where I'm confused on how to code it.

This is my code:

x = [1,1,2,3,5]
y = []
r = []
for t in range(1,6,1):
    print("give me a color")
    s = input()
    y.append(s)

if "r" in y:
    if "b" in y:
        if "g" in y:
            r_index = y.index("r")
            r_index2 = y.index("b")
            r_index3 = y.index("g")
            r.append(r_index)
            r.append(r_index2)

            
if r_index == 0:
    r_index = 1
if r_index == 4:
    r_index = 5
if r_index2 == 0:
    r_index2 = 1
if r_index2 == 4:
    r_index2 = 5
hour = int(r_index) + int(r_index2)
minute = 5*(r_index2 + r_index3)
print("The final time is",hour,":",minute)

If there are ever repeats of Red, Green, Or Blue, my code only adds the smallest value, resulting in the wrong time.

I would appreciate an answer on how to fix this, and a fixed code

fib = [
    1,
    1,
    2,
    3,
    5
]

colors = [
    "red",
    "red",
    "blue",
    "green",
    "white"
]

def get_sum_for(color):
    return sum(f for f, c in zip(fib, colors) if c == color)

hours = get_sum_for("red") + get_sum_for("blue")
minutes = 5 * (get_sum_for("green") + get_sum_for("blue"))

print(f"{hours}:{minutes}")

you could just put in a dic with indices:

from collections import defaultdict

def get_hour(hour):
    fib_val = [1,1,2,3,5]
    fib = defaultdict(list)
    for idx, h in enumerate(hour):
        fib[h.lower()].append(fib_val[idx])
        
    h = sum(fib['red']  + fib['blue'])
    m = 5 * sum(fib['green']  + fib['blue'])
    return f"{h:02d}:{m:02d}"

output:

get_hour(["Red","Red","Blue","Green","White"])

'04:25'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM