匹配2种不同类型数组中的项

时间:2017-06-02 21:06:41

标签: c# loops for-loop matching

我有

string[] ColorsArray = new string[12] { "Blue", "Red", "Green", 
"Yellow", "Blue", "Green", "Blue", "Yellow", "Red", "Green", 
 "Red", "Yellow" };

float[] LengthArray = new float[12] { 1.3f, 1.4f, 5.6f, 1.5f, 
3.5f, 5.4f, 1.2f, 6.5f, 4.4f, 4.1f, 3.3f, 4.9f };

我想匹配它们,以便每个索引等于另一个。所以ColorsArray [0]是蓝色,等于LengthArray [0],这是1.3f

颜色是鱼。所以每种颜色都是一条鱼,每个相应的数字都是那条鱼的长度。我需要能够拿出用户给我的东西(蓝色,红色,绿色黄色),然后找出最大的鱼是什么。

目标是将它们全部匹配,然后能够比较每种颜色的值。所以有4种颜色和3种重复,总共12种"鱼"。

显然可以使用For循环,但我不确定如何。

5 个答案:

答案 0 :(得分:0)

以下代码查找每种鱼类的最大长度:

import tensorflow as tf
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
import os

myimages = []

path_to_images = 'images_animation'
filenum = len([name for name in os.listdir(path_to_images) if os.path.isfile(os.path.join(path_to_images, name))])

#loops through available pngs
for p in range(1, filenum):

    ## Read in picture
    fname = "images_animation/image%03d.png" % p
    img = mpimg.imread(fname)
    imgplot = plt.imshow(img)

    # append AxesImage object to the list
    myimages.append([imgplot])

for n, im in enumerate(myimages):
    img = Image.open(fname).convert("L")
    arr = np.array(img
    print(arr)

答案 1 :(得分:0)

您可以在Dictionary中按颜色组合长度,然后只需访问它们。

// You can start or stop your timer at will
ticker.start();
ticker.stop();

// You can also change the interval while it's in progress
ticker.interval = 99;

答案 2 :(得分:0)

这是一个简单的扩展方法......

public static T2 ItemInOtherCollection<T1, T2>(this IEnumerable<T1> items, IEnumerable<T2> otherItems, T1 item)
{
    var list = items.ToList();
    return otherItems.ElementAt(list.IndexOf(item));
}

你可以像这样使用它......

var item1 = LengthArray.ItemInOtherCollection(ColorsArray, 5.6f);
var item2 = ColorsArray.ItemInOtherCollection(LengthArray, "Blue");

答案 3 :(得分:0)

这是一个LINQ解决方案:

Dictionary<string, float> biggestFishDictionary = ColorsArray
    .Zip(LengthArray, (color, length) => new { color, length })
    .GroupBy(pair => pair.color)
    .ToDictionary(g => g.Key, g => g.Max(pair => pair.length));

返回{ { "Blue", 3.5f }, { "Red", 4.4f }, { "Green", 5.6f }, { "Yellow", 6.5f } }

答案 4 :(得分:0)

由于这似乎是家庭作业,我们可能不应该帮助......

没有额外的集合等,可能符合.Net 1.0标准:

string[] userFishes = Console.ReadLine().Split(',');
string MaxFishName = "";
float MaxFishLength = 0;

foreach (var userFish in userFishes) {
    for (int j1 = 0; j1 < ColorsArray.Length; ++j1) {
        if (ColorsArray[j1] == userFish) {
            if (LengthArray[j1] > MaxFishLength) {
                MaxFishLength = LengthArray[j1];
                MaxFishName = userFish;
            }
        }
    }
}

Console.WriteLine(String.Format("User's largest fish is {0} at {1} feet", MaxFishName, MaxFishLength));

当然,我会将您的变量重命名为更友好(ColorsArray将更好地作为FishNamesLengthArray(单数?)作为FishLengths)并使用LINQ in现实生活:

var FishMaxLengthMap = FishNames.Zip(FishLengths, (fishName, fishLen) => new { fishName, fishLen }).GroupBy(fnl => fnl.fishName, (fishName, fnlg) => new { fishName, MaxLen = fnlg.Max(fnl => fnl.fishLen)}).ToDictionary(fnml => fnml.fishName.ToLower(), fnml => fnml.MaxLen);

var userFishNames = Console.ReadLine().Split(',').Select(fn => fn.Trim().ToLower());

foreach (var fnl in userFishNames.Select(fn => new { fishName = fn, fishLen = FishMaxLengthMap[fn] }).Where(fml => fml.fishLen == userFishNames.Select(fn => FishMaxLengthMap[fn]).Max()))
    Console.WriteLine($"Largest fish is {fnl.fishName} with length {fnl.fishLen} feet.");