I have something to run.
One person who has many colors of the same model
I used the person's name , car model , Designed for people with the properties of the car year , color should be in the form of a list as car state , and car color attribute Because one person may have several cars of different colors, but can be of the same model.
How can I now find and print 2 different people who have the same model of the car and the car has the same color but the object-oriented words are from different states?
I'm new to Python.
How can I insert a list in the list of objects and how can I get it from the list? I know how to do this for a specialty, but I'm a bit confused about the inventory operation.
Data can be:
person1 ford [Red, blue California person 4 Ford [Red] California person 5 Honda [Red] New York [/ code] My preference can only be: [(person 1, person5)] (same model car, same color, different state)
You wanted to know something about manipulation in the list:
$ Python Python 2.6.5 (r265: 79063, April 16, 2010), 13:57:41) [GCC 4.4.3] Learn more at linux2 Minister of "help", "copyright" to write "credits" or "license" & Gt; & Gt; & Gt; L = []> gt; & Gt; & Gt; L.append ("Honda") & gt; & Gt; & Gt; L.append ("ford") & gt; & Gt; & Gt; L ['Honda', 'Ford'] & gt; & Gt; & Gt; L [0] 'Honda' & gt; & Gt; & Gt; L.pop (0) 'Honda' & gt; & Gt; & Gt; L.pop (0) 'Ford' & gt; & Gt; & Gt; L.pop (0) traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; Index Error: Pop from empty list If you want to find many people with matching features, then you can represent something in some psuedo-code (because here I think focusing on the algorithm is more useful than focusing on the dragon to make it.
If p1.state == p2.state next p1.car == p2.car foreach c1 p1.colors foreach c2 in p2.colors result.append ((p1, p2)) if c1 == C2
This code is for people Compare Add to not compare it to any person not against him, it was to live in a state which does not compare them because you asked "related" .. But different states. So let's filter those same-state people. It only compares those who own a similar type of car. (If people are owned by different types of cars, then you simply add nested to the two ends.) Then it notes the pair of people who have the same color of the car.
There is a potential bug in algorithm: it will report [(person1, person2), (person2, person1)] . Therefore entries are repeated. If you do not want this repetition, then it is possible to modify the algorithm to find only people upper or lower triangles:
for the result = [] i = 0; I & lt; People.last_index-1; J + I + 1 for I ++; Jammu & lt; People.last_index; J ++ p1 = people [i]; P2 = people [h] next if p1.state == p2.state next to p1.car == p2.car foreach c1 in p1.colors foreach c2 p2.colors result.append ((p1, p2)) if in C1 == c2 Note that if we can remove on the next page, check p1 == p2 because we explicitly i == J can not be found. j is defined to start with i + 1 .
Comments
Post a Comment