Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 calculation with list in list
#11
gianniskampanakis Wrote:Σε ευχαριστώ το type(i) εκτός από int τι τιμες μπορεί να πάρει στην περίπτωση μας ;
I couldn't find a rule that enforces this, but try to use english in the forum as very few members read and understand greek. Note that if type(i) == int is not colloquial python code. The best way to do this is
if isinstance(i, int): ...
cvsae and micseydel like this post
Quote
#12
Thank you guys
Quote
#13
(Aug-09-2019, 06:39 AM)gianniskampanakis Wrote: Σε ευχαριστώ το type(i) εκτός από int τι τιμες μπορεί να πάρει στην περίπτωση μας ;
you are welcome, can take str list dict etc.. below an example with a dict inside a list, using isinstance as Gribouillis mention https://python-forum.io/Thread-calculati...9#pid88479

l = [1,2,3,[1,2], {"test": 8}]
 
def suml(l):
    s = 0 
    for i in l:
        if isinstance(i, int):
            s +=i 
        elif isinstance(i, list):
            for t in i:
                s +=t    
        elif isinstance(i, dict):
            for t in i:
                s +=i[t]
    return s
Quote
#14
That's nice guys. I have never heard of insinstace use
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 86 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Creating a list from a comprehension using a list rm197 6 108 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 266 Sep-28-2019, 04:58 AM
Last Post: perfringo
  convert list of dict to dict of array of list with pop up kk230689 1 104 Sep-13-2019, 07:29 AM
Last Post: perfringo
  removing quotes from a list and keep type list evilcode1 3 190 Aug-03-2019, 11:07 AM
Last Post: perfringo
  Changing elements of a list to match another list rockproper 2 167 Jul-11-2019, 02:13 PM
Last Post: ichabod801
  Create new list from another list based on condition khalidreemy 1 234 Jun-11-2019, 10:21 AM
Last Post: buran
  Iterating list of oredereddict for creating new list of ordereddict babypython 7 359 May-05-2019, 05:32 PM
Last Post: Larz60+
  reverse list, incl. nested list Livne_ye 3 275 May-04-2019, 12:34 PM
Last Post: Livne_ye
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 373 Apr-27-2019, 03:13 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)