Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
group by property
#11
code?
Reply
#12
Okay, I have found the issue. I have to modify this:

Dict['m'][0]
But this solution also set n to each dictionary. Can we omit this?
Reply
#13
If I'm correct, that is from this line -
NewList[count][Dict['m'][0]].append(Dict)
What is does is looks in NewList with the index number of count which will lead us to the dictionary that shares the same number as the Dict we are saving. Dict['m'][0] is either letter 'g' or 'b'. We use that to determine whether to list it under 'g' or 'b'. I wonder why it would cause an issue. The only reason I can think of is a typo in the Dict dictionary. If you wrote something like "cood" instead of "good" since it said keyerror "c".
Reply
#14
Yeah, I've solved that already. And you're right, I was having a typo. But I'm asking for omitting n from each dictionary in NewList. Also good way to omit m in dictionary which is being appended (.append(Dict))
Reply
#15
Oh, I added the n in each dictionary to make it easily navigable. By omitting it from the dictionary the code have to rewritten.

(Jun-30-2019, 01:54 PM)bhojendra Wrote: Also good way to omit m in dictionary which is being appended (.append(Dict))
I don't know what you're trying to say here
Reply
#16
Anyways, let's forget about omitting m. I'll do that. Can you please re-write the code which will not have n in each dictionary?

Thanks a lot for helping me.
Reply
#17
This should work
NewList = []
for Dict in List:
    if not NewList:
        NewList.append({'g' : [], 'b' : []})
    new = False
    count = 0
    for SortedDict in NewList:
        if not SortedDict['g']:
            letter = 'b'
        else:
            letter = 'g'
        if SortedDict[letter]['n'] == Dict['n']:
            new = False
            NewList[count][Dict['m'][0]].append(Dict)
            break
        else:
            new = True
        count += 1
    if new:
        NewList.append({'g' : [], 'b' : []})
        NewList[len(NewList) - 1][Dict['m'][0]].append(Dict)
print(NewList)
Reply
#18
Did you try this? It's showing error:
Error:
if SortedDict[letter]['n'] == Dict['n']:\nTypeError: list indices must be integers or slices, not str
Reply
#19
sry, I actually didn't test it cause I'm so tired. do
if SortedDict[letter][0]['n'] == Dict['n']:
Reply
#20
Ah, sorry for that. I'm still getting error: list index out of range

Anyways, I'll try to resolve. But can you please suggest what to learn to improve coding skill? I'm beginner in programming.

Take rest. You may reply it tomorrow. I'm also going to sleep. (I'll mark this post as solved though)

Thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,754 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,639 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,315 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 3,031 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 3,142 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,500 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,034 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,347 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 2,837 Oct-26-2019, 05:33 PM
Last Post: engmoh
  what is the doc parameter of property? microphone_head 2 2,471 Apr-11-2019, 08:59 AM
Last Post: microphone_head

Forum Jump:

User Panel Messages

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