How to get each item in a list of lists?

I am making a heart game for my assignment, but I donโ€™t know how to get each item in the list list:

>>>Cards = [[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C],["JH"]],[["7D"]]] 

and what comes to my mind:

 for values in cards: for value in values: 

But I think that I have an element that has 2 lists. How to calculate the one that has 3 and 1 list in cards?

+6
source share
9 answers

Like this:

 >>> Cards = [[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],["7D"]] >>> from compiler.ast import flatten >>> flatten(Cards) ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D'] 

As noted, nacholibre compiler package is deprecated. This is the source of flatten :

 def flatten(seq): l = [] for elt in seq: t = type(elt) if t is tuple or t is list: for elt2 in flatten(elt): l.append(elt2) else: l.append(elt) return l 
+15
source

Slightly hides oneliner:

 >>> [a for c in Cards for b in c for a in b] ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7', 'D'] 

You might want to give a, b, and c more descriptive names.

+5
source

If your cards are embedded in an inconvenient way:

 >>> Cards = [[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],["7D"]] >>> def getCards(cardList,myCards=[]): #change this to myCards, and pass in a list to mutate this is just for demo if isinstance(cardList,list): for subList in cardList: getCards(subList) else: myCards.append(cardList) return myCards >>> getCards(Cards) ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D'] 

Will recursively go through the list and find all the elements. These are some points that I have performed comparing the performance of the selected flattern method with mine:

 >>> print(timeit.timeit(r'getCards([[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],["7D"]],[])',setup="from clas import getCards")) 5.24880099297 >>> timeit.timeit(r'flatten([[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],["7D"]])',setup="from compiler.ast import flatten") 7.010887145996094 
+4
source

Your list is an incomplete nested list, so you can first make it rectangular using the procedure described here , and then smooth out the resulting numpy.ndarray .

The "ifs" below is also not needed if the last element ['7D'] was [['7D']] (then other answers will also work).

 import numpy as np collector = np.zeros((3,3,3),dtype='|S20') for (i,j,k), v in np.ndenumerate( collector ): try: if not isinstance(cards[i], str): if not isinstance(cards[i][j], str): collector[i,j,k] = cards[i][j][k] else: collector[i,j,0] = cards[i][j] else: collector[i,0,0] = cards[i] except IndexError: collector[i,j,k] = '' print collector[collector<>''].flatten() 
+2
source

Using generators, you can write a much more readable implementation of flatten :

 def flatten(l): if isinstance(l, list): for e1 in l: for e2 in flatten(e1): yield e2 else: yield l 

Or, if you are using Python 3.3, which has added yield from syntax:

 def flatten(l): if isinstance(l, list): for e in l: yield from flatten(e) else: yield l 

Result:

 >>> list(flatten([[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],[["7D"]]])) ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D'] 
+2
source

Use 2 nested itertools.chain to flatten the list:

 In [32]: Cards Out[32]: [[['QS', '5H', 'AS'], ['2H', '8H'], ['7C']], [['9H', '5C'], ['JH']], ['7D']] In [33]: from itertools import chain In [34]: [k for k in chain.from_iterable([i for i in chain.from_iterable(Cards)])] Out[34]: ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7', 'D'] 
+2
source

This solution is very reliable for any type of nested lists or tuples (to add other duplicate types just add another or isinstance(...) in the code below.

It simply calls a function recursively, which expands itself:

 def unfold(lst): output = [] def _unfold(i): if isinstance(i, list) or isinstance(i, tuple): [_unfold(j) for j in i] else: output.append(i) _unfold(lst) return output print unfold(cards) #['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D'] 
+2
source

Using the Smooth list from Rosetta code, which you could do:

 >>> def flatten(lst): return sum( ([x] if not isinstance(x, list) else flatten(x) for x in lst), [] ) >>> Cards = [[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],["7D"]] >>> flatten(Cards) ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D'] >>> 

The solution only aligns the nested lists โ€” not tuples or rows.

+1
source
 from itertools import chain, imap l= [[["QS","5H","AS"],["2H","8H"],["7C"]],[["9H","5C"],["JH"]],[["7D"]]] k = list(chain.from_iterable(imap(list, l))) m = list(chain.from_iterable(imap(list, k))) print m 

: ['QS', '5H', 'AS', '2H', '8H', '7C', '9H', '5C', 'JH', '7D']

Itertools is awesome!

+1
source

Source: https://habr.com/ru/post/945777/


All Articles