Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
group by property
#21
It all depends on what you want to do. The two main path are gaming and web. I actually took gaming then the web path. Either way you should know about most of the python modules, functions, and everything about classes. Then for gaming I suggest pygame. For gaming you should learn techniques to shorten code. This will make games run smoother and the code less messy. For web development, just need to know the basics, but also need to learn html and css. The you can use Flask or Django. I personally like Flask better. Corey Schafer did great tutorial series on both Django and Flask. For learning pygame, I suggest the pygame tutorials here on the forum. Another thing that can help is looking at the pygame threads laying around in the Game Development tab.If you start to enjoy one over the other you can spread out. For example, I spread to C++, javascript (games and web), and Lua (Roblox games).
Reply
#22
My experience is that if more complexity is introduced to data structure then it is good time to return to the drawing board and rethink your approach.

Why there should be additional complexity? Why this structure is required? Source data is list of dictionaries and result is also list of (more complicated) dictionaries. In order to filter out something from this list you have to iterate anyway. Isin't it possible that it is easier to iterate original list instead of creating more complex structure and iterate over it?

Want to have dictionaries from list which are 'good', where n = 1 and without 'm'? List comprehension should be enough:

>>> orig = [{'name': 'John', 'm': 'good', 'n': 1},
...         {'name': 'Alina','m': 'good', 'n': 1},
...         {'name': 'Olivia', 'm': 'bad', 'n': 2},
...         {'name': 'Ruby', 'm': 'bad','n': 2}]
>>> [{k: v for k, v in row.items() if k != 'm'} for row in orig if row['n'] == 1 and row['m'] == 'good']
[{'name': 'John', 'n': 1}, {'name': 'Alina', 'n': 1}]
For me it's strange to name source data as 'result'. It may create confusion and confusion may lead to buggy code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#23
It looks great. But I'm not able to understand what's going on here. Anyways, this is only resulting for n = 1 only? - I have not tried it yet.
Reply
#24
(Jul-01-2019, 08:23 AM)bhojendra Wrote: It looks great. But I'm not able to understand what's going on here. Anyways, this is only resulting for n = 1 only? - I have not tried it yet.

In spoken language it something like: "give me dictionary except key 'm' for every dictionary in list if dictionary key 'n' has value 1 and dictionary key 'm' has value 'good'". With list comprehension you can filter out whatever you want. The question is what do you want to do with data? Will you query this data constantly (then it can make sense to create new object tailored for queries) or just once-twice?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#25
As in my initial post, I want the result as like:

result = [
  { // n is 1
    "g": [
      {
        "name": "John",
        "n": 1
      },
      {
        "name":"Alina",
        "n": 1
      }
    ],
    "b": [
      {
        "name": "...",
        "n": 1
      },
      {
        "name": "...",
        "n": 1
      }
    ]
  },
  { // n is 2
    "g": [...],
    "b": [...]
  },
  { // n is 3
   // ...
  }
]
And I think I understood one liner syntax. I will try this.
Reply
#26
(Jul-01-2019, 06:43 PM)bhojendra Wrote: As in my initial post, I want the result as like:

I asked why you want result like that? What are the benefits of new structure over old one?

old = [{'name': 'John', 'm': 'good', 'n': 1},
       {'name': 'Alina','m': 'good', 'n': 1},
       {'name': 'Olivia', 'm': 'bad', 'n': 2},
       {'name': 'Ruby', 'm': 'bad','n': 2}]

new = [{'g': [{'name': 'John', 'n': 1}, {'name': 'Alina', 'n': 1}]},
       {'b': [{'name': 'Olivia','n': 2},{'name': 'Ruby','n': 2}]}]
Old list dictionaries have exactly the same structure, new list dictionaries not (some have key 'g', some key 'b' and some probably both). You must be extra careful to not run into KeyError while accessing data.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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,685 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,564 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,256 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 2,993 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 3,092 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,418 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,019 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,312 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 2,802 Oct-26-2019, 05:33 PM
Last Post: engmoh
  what is the doc parameter of property? microphone_head 2 2,444 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