Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting one by one
#1
I'm going to explain what I want as simple as possible (brainfarts over here atm),

You probably know the game mikado, where you release a bunch of sticks on the table and you have to pick 'm up without touching any other. Well, my brainfart is about the game start: I want to know which sticks are parallel to each other.

So the start is:
n sticks with a direction

the desired result is:
dir1 = [stick1, stick33, stick42, stick2]
dir2 = [stick3, stick55]
dir3 = ...
etc


I'm using some program API so putting a example here would clutter my issue big time. Perhaps an easier example showcase the same which fails as the output should be [dir1, dir1, dir1] [dir2] [dir3].
I think it has something to do with the fact that lists are mutable and therefor the itteration grows?

mikado = ["dir1", "dir1", "dir2", "dir1", "dir3"]

count=0

# 
master_list = []

for stick in mikado:
    if count == 0:
        master_list.append([stick])        
        count += 1
    for index, e in enumerate(master_list):
        if stick == e[0]: # parallel check            
            master_list[index].append(stick)
        else:
            master_list.append([stick])

print master_list
# [['dir1', 'dir1', 'dir1'], ['dir2', 'dir2'], ['dir1', 'dir1'], ['dir3', 'dir3'], ['dir3', 'dir3'], ['dir3', 'dir3']]
Reply
#2
Your loop doesn't work because the inner loop is going over all the contents in master_list, and each time it doesn't find a match, it adds a new entry. So if master_list has 3 entries and you hit a new direction, you'll add 3 new lists because of the else clause.

If you were doing this, you'd loop over all the values and only after reaching an unsuccessful end, append to the list.

Modified:
mikado = ["dir1", "dir1", "dir2", "dir1", "dir3"]

count=0

master_list = [[mikado.pop(0)]]

for stick in mikado:
    for index, e in enumerate(master_list):
        if stick == e[0]: # parallel check
            master_list[index].append(stick)
            break
    else:
        master_list.append([stick])

print(master_list)
Output:
[['dir1', 'dir1', 'dir1'], ['dir2'], ['dir3']]
But I'm not sure how this helps with your real goal, because these items don't have "names". A better way would be to use a dictionary and append them based on their direction. Perhaps something like this:

from collections import defaultdict

mikado = [("stick1", "dir1"),
          ("stick2", "dir2"),
          ("stick3", "dir1"),
          ("stick4", "dir3"),
          ("stick5", "dir2"),
          ("stick6", "dir3"),
          ("stick7", "dir1"),
          ("stick8", "dir4"),
          ("stick9", "dir5"),
          ("stick10", "dir3"),
          ]

master_list = defaultdict(list)

for stick in mikado:
    master_list[stick[1]].append(stick[0])

for dir, sticks in master_list.items():
    print(f"dir {dir} has sticks {sticks}")
Output:
dir dir1 has sticks ['stick1', 'stick3', 'stick7'] dir dir2 has sticks ['stick2', 'stick5'] dir dir3 has sticks ['stick4', 'stick6', 'stick10'] dir dir4 has sticks ['stick8'] dir dir5 has sticks ['stick9']
Reply
#3
for a second I thought you indented wrong but oh my for else does exists! Awesome.

And yes, my example code doesn't solve it, and yes dictionaries are to be used but I first had to understand where my thoughts were derailing.

Thanks, also for your other example.
Reply
#4
(Nov-20-2020, 12:45 PM)3Pinter Wrote: for a second I thought you indented wrong but oh my for else does exists!

Yes, I saw one of Raymond's presentations where he said that if it weren't called "else" but "nobreak", more people would understand it. It handles the case of natural exit from a for or while, rather than a case where "break" was called.
Reply
#5
(Nov-20-2020, 04:41 PM)bowlofred Wrote:
(Nov-20-2020, 12:45 PM)3Pinter Wrote: for a second I thought you indented wrong but oh my for else does exists!

Yes, I saw one of Raymond's presentations where he said that if it weren't called "else" but "nobreak", more people would understand it. It handles the case of natural exit from a for or while, rather than a case where "break" was called.

Yes, and I'm glad you posted this, knowing this exists is very handy! Thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,047 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020