Related
Im trying to make a deck of cards using zip,
but what i got is the object code?
"<zip object at 0x0000025763A32200>, <zip object at 0x0000025763A32240>,...."
suits = ['diamond','club','heart','spade']
value = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
deck = [zip(suit, val) for val in value for suit in suits]
print(deck)
How do i make it as a list of tuples? (using zip)
print(deck)
output :
[('A','diamond'),('A','club')....('K','diamond')]
Is it really necessary to use zip? You can get what you want just by not using zip and leaving your code as it is, saving a tuple(val,suit), not zipping them:
>>> deck = [(val,suit) for val in value for suit in suits]
>>> print(deck)
[('A', 'diamond'), ('A', 'club'), ('A', 'heart'), ('A', 'spade'), ('2', 'diamond'), ('2', 'club'), ('2', 'heart'), ('2', 'spade'), ('3', 'diamond'), ('3', 'club'), ('3', 'heart'), ('3', 'spade'), ('4', 'diamond'), ('4', 'club'), ('4', 'heart'), ('4', 'spade'), ('5', 'diamond'), ('5', 'club'), ('5', 'heart'), ('5', 'spade'), ('6', 'diamond'), ('6', 'club'), ('6', 'heart'), ('6', 'spade'), ('7', 'diamond'), ('7', 'club'), ('7', 'heart'), ('7', 'spade'), ('8', 'diamond'), ('8', 'club'), ('8', 'heart'), ('8', 'spade'), ('9', 'diamond'), ('9', 'club'), ('9', 'heart'), ('9', 'spade'), ('10', 'diamond'), ('10', 'club'), ('10', 'heart'), ('10', 'spade'), ('J', 'diamond'), ('J', 'club'), ('J', 'heart'), ('J', 'spade'), ('Q', 'diamond'), ('Q', 'club'), ('Q', 'heart'), ('Q', 'spade'), ('K', 'diamond'), ('K', 'club'), ('K', 'heart'), ('K', 'spade')]
Remember, if you want the order by value the right order in your tuple must be (val,suit), and not (suit,val)
With this simple form you don't need any extra module.
zip won't work for this, but you can use itertools.product:
>>> import itertools
>>> deck = list(itertools.product(value, suits))
>>> print(deck)
[('A', 'diamond'), ('A', 'club'), ('A', 'heart'), ('A', 'spade'), ('2', 'diamond'), ('2', 'club'), ('2', 'heart'), ('2', 'spade'), ('3', 'diamond'), ('3', 'club'), ('3', 'heart'), ('3', 'spade'), ('4', 'diamond'), ('4', 'club'), ('4', 'heart'), ('4', 'spade'), ('5', 'diamond'), ('5', 'club'), ('5', 'heart'), ('5', 'spade'), ('6', 'diamond'), ('6', 'club'), ('6', 'heart'), ('6', 'spade'), ('7', 'diamond'), ('7', 'club'), ('7', 'heart'), ('7', 'spade'), ('8', 'diamond'), ('8', 'club'), ('8', 'heart'), ('8', 'spade'), ('9', 'diamond'), ('9', 'club'), ('9', 'heart'), ('9', 'spade'), ('10', 'diamond'), ('10', 'club'), ('10', 'heart'), ('10', 'spade'), ('J', 'diamond'), ('J', 'club'), ('J', 'heart'), ('J', 'spade'), ('Q', 'diamond'), ('Q', 'club'), ('Q', 'heart'), ('Q', 'spade'), ('K', 'diamond'), ('K', 'club'), ('K', 'heart'), ('K', 'spade')]
I am looking for a list comprehension in adding a range of numbers based on index position in a list of lists while keeping the second index alone.
search_keys = [('1', 'B'), ('1', 'K'), ('1', 'Y')]
Desired Result:
new_search_keys = [('11', 'B'), ('12', 'B'), ('13', 'B'), ('14', 'B'), ('15', 'B')...
...('11', 'K'), ('12', 'K'), ('13', 'K'), ('14', 'K'), ('15', 'K')...
...('11', 'Y'), ('12', 'Y'), ('13', 'Y'), ('14', 'Y'), ('15', 'Y')...]
My attempt brings a TypeError: 'type' object is not subscriptable:
new_search_keys = [search_keys[i][0] + list(range[1,9]) for i in range(len(search_keys))]
This should solve your problem:-
new_search_keys = [(search_keys[i][0] +str(j), search_keys[i][1]) for i in range(len(search_keys)) for j in range(1,9)]
Output:-
[('11', 'B'), ('12', 'B'), ('13', 'B'), ('14', 'B'), ('15', 'B'), ('16', 'B'), ('17', 'B'), ('18', 'B'), ('11', 'K'), ('12', 'K'), ('13', 'K'), ('14', 'K'), ('15', 'K'), ('16', 'K'), ('17', 'K'), ('18', 'K'), ('11', 'Y'), ('12', 'Y'), ('13', 'Y'), ('14', 'Y'), ('15', 'Y'), ('16', 'Y'), ('17', 'Y'), ('18', 'Y')]
this is how my data looks :ALL THESE DATA ARE IN SINGLE COLUMN
#columnA
(8,8)
(6,7)(7,7)(7,6)
(2,12)(12,3)(3,4)(4,12)(12,12)
(14,14)
(1,1)(1,12)(12,2)(2,2)(2,4)
(6,8)(8,8)(8,12)
(6,6)(6,3)(3,14)
(1,14)(14,14)(14,1)(1,1)(1,2)
(1,1)(1,14)
(2,2)(2,15)(15,5)(5,5)(5,16)
(1,11)(11,1)(1,2)(2,2)(2,14)
(5,5)(5,1)
(12,2)(2,2)(2,10)(10,10)
(9,9)(9,4)(4,4)
(13,13)
(1,1)
(11,14)(14,14)
for i in range(len(data)):
x.iloc[i]=data.iloc[i].unique()
output is to replace the duplicate values and find unique:
How about storing all the data in a list and find the set of the list.
for instance, if I have a list of tupples or list of lists in the form:
list = [(1,1), (2,3), (2,3), (1,3), (1,3), (1,1)]
set_of_list = set(list)
>>> {(1,3), (2,3), (1,1)}
IIUC, use pandas.Series.str.findall with explode and unique:
uniq = df['columnA'].str.findall("(\d+),(\d+)").explode().unique()
uniq
Output:
array([('8', '8'), ('6', '7'), ('7', '7'), ('7', '6'), ('2', '12'),
('12', '3'), ('3', '4'), ('4', '12'), ('12', '12'), ('14', '14'),
('1', '1'), ('1', '12'), ('12', '2'), ('2', '2'), ('2', '4'),
('6', '8'), ('8', '12'), ('6', '6'), ('6', '3'), ('3', '14'),
('1', '14'), ('14', '1'), ('1', '2'), ('2', '15'), ('15', '5'),
('5', '5'), ('5', '16'), ('1', '11'), ('11', '1'), ('2', '14'),
('5', '1'), ('2', '10'), ('10', '10'), ('9', '9'), ('9', '4'),
('4', '4'), ('13', '13'), ('11', '14')], dtype=object)
I was working on some other question . I have below list
[(['1', '2', '3'], 'abc'), (['4', '5', '6'], 'xyz')]
Output should be below
[('1', 'abc'), ('2', 'abc'), ('3', 'abc'), ('4', 'xyz'), ('5', 'xyz'), ('6', 'xyz')]
My attempt is
First i unlist the list inside it
l1=[ tuple(i[0])+(i[1],) for i in l ]
print (l1)
[('1', '2', '3', 'abc'), ('4', '5', '6', 'xyz')]
Then tried product from itertools , but it is not giving me the required result. Problem is 'abc' is getting splited in 'a','b','c' using product.
from itertools import product
[ list(product(i[:-1],i[-1])) for i in l1 ]
[[('1', 'a'),
('1', 'b'),
('1', 'c'),
('2', 'a'),
('2', 'b'),
('2', 'c'),
('3', 'a'),
('3', 'b'),
('3', 'c')],
[('4', 'x'),
('4', 'y'),
('4', 'z'),
('5', 'x'),
('5', 'y'),
('5', 'z'),
('6', 'x'),
('6', 'y'),
('6', 'z')]]
Use list comprehension:
L=[(['1', '2', '3'], 'abc'), (['4', '5', '6'], 'xyz')]
In: [ (n,s) for l,s in L for n in l ]
Out:
[('1', 'abc'),
('2', 'abc'),
('3', 'abc'),
('4', 'xyz'),
('5', 'xyz'),
('6', 'xyz')]
As you can write:
rslt=[]
for l,s in L:
for n in l:
rslt.append((n,s))
product from itertools is working as intended. The issue is that Python strings are iterable, so product is iterating through the elements of the string. If you want to treat the string as a single element, you can put it into a list and feed the list to product
You can use itertools.product as long as you wrap the string in an iterable so that it is handled as a single element of an iterable rather than iterated.
from itertools import product
data = [(['1', '2', '3'], 'abc'), (['4', '5', '6'], 'xyz')]
combos = [combo for a, b in data for combo in product(a, [b])]
print(combos)
# [('1', 'abc'), ('2', 'abc'), ('3', 'abc'), ('4', 'xyz'), ('5', 'xyz'), ('6', 'xyz')]
You can use list comprehensions:
ls = [(['1', '2', '3'], 'abc'), (['4', '5', '6'], 'xyz')]
ls_new = [(a,b) for n,b in ls for a in n]
print(ls_new)
Given the following lists:
letters = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
numbers = ('1', '2', '3', '4')
How can I produce an iterated list that produces the following:
output = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'),
('e', '1'), ('f', '2'), ('g', '3'), ('a', '4'),
('b', '1'), ('c', '2'), ('d', '3'), ('e', '4'),
('f', '1'), ('g', '2')...]
I feel like I should be able to produce the desired output by using
output = (list(zip(letters, itertools.cycle(numbers))
But this produces the following:
output = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'),
('e', '1'), ('f', '2'), ('g', '3')]
Any help would be greatly appreciated.
If you are looking for an infinite generator, you can use cycle with zip for both lists, in the form of zip(itertools.cycle(x), itertools.cycle(y)). That would supply you with the required generator:
>>> for x in zip(itertools.cycle(letters), itertools.cycle(numbers)):
... print(x)
...
('a', '1')
('b', '2')
('c', '3')
('d', '4')
('e', '1')
('f', '2')
('g', '3')
('a', '4')
('b', '1')
('c', '2')
('d', '3')
...
If you want a finite list of elements, this should work
import itertools
letters = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
numbers = ('1', '2', '3', '4')
max_elems = 10
list(itertools.islice((zip(itertools.cycle(letters), itertools.cycle(numbers))), max_elems))
results in
[('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'), ('e', '1'), ('f', '2'), ('g', '3'), ('a', '4'), ('b', '1'), ('c', '2')]