Generating two-dimensional array of binaries in Python - python

So this is what im trying
list(itertools.combinations_with_replacement('01', 2))
but this is generating [('0', '0'), ('0', '1'), ('1', '1')]
I still need a ('1','0') tuple, is there a way to make itertools also do combinations and order?

Use itertools.product instead:
>>> import itertools
>>> list(itertools.product('01', repeat=2))
[('0', '0'), ('0', '1'), ('1', '0'), ('1', '1')]

Related

How to add a counter element to *each* recurring value in a tuple?

I have a list of tuples like this:
[('bike', '1'), ('bike', '2'), ('car', '3'), ('car', '4')]
and I want to count each time the first element appears and append it to the end of the tuple
[('bike', '1', '1'), ('bike', '2', '2'), ('car', '3', '1'), ('car', '4', '2')]
I have researched methods like counter, but it appears that only works to count the total amount of times an item appears. I have also found other methods that only seem to work for lists and not tuples.
If you need the running count, you could use a defaultdict to keep it:
from collections import defaultdict
data = [('bike', '1'), ('bike', '2'), ('car', '3'), ('car', '4')]
result = []
counts = defaultdict(lambda: 1)
for ele in data:
k, v = ele
result.append((k, v, counts[k]))
counts[k] = counts[k] + 1
print(result)
Output
[('bike', '1', 1), ('bike', '2', 2), ('car', '3', 1), ('car', '4', 2)]
so you have a list of tuples
you can just loop over the data in the list, move it to another list and check the new list if it have that elem.

How to search unique tuples in multiple list of tuples in python

If I have
a = [("1","2"),("5","6")] [("3","4"), ("5","6")] [("5","7"), ("2","3")]
How do I search through the lists of tuples to get a ordered unique list of tuple say:
B = [("1","2"),("5","6"), ("3","4"), ("5","7"), ("2","3")]
Unique = []
for tuple in a:
If tuple not in Unique:
Unique.append (tuple)
gives list of tuples same as a.
EDIT:
Since none of the solution actually worked for me I am giving the actual problem:
I have a text file with four sentences,say:
This is utf-8 line1
This is eng line 1
This is utf line 2
This is eng line 2
I extract line 1 and line 3 to a variable (utfvar) and line 2 and 4 to (engvar). Then create tuples with:
UtfEng = list (zip (utfvar,engvar))
print UtfEng is:
[('This','This'), ('is','is')...('line','line'),('1','1')]
[('This','This'), ('is','is')...('line','line'),('2','2')]
I want to extract unique tuples from this as:
[('This','This'), ('is','is')...('line','line'),('1','1'),('2','2')]
as #jonrsharpe wrote on comment, this is not a valid python.
but let's say you have three lists.
>>> a = [("1","2"),("5","6")] ;b= [("3","4"), ("5","6")];c= [("5","7"), ("2","3")]
merging those,
>>> a+b+c
will give you,
[('1', '2'), ('5', '6'), ('3', '4'), ('5', '6'), ('5', '7'), ('2', '3')]
then you can
>>> list(set(a+b+c))
[('2', '3'), ('1', '2'), ('5', '7'), ('5', '6'), ('3', '4')]
which will give you unique tuples.
documentation
Assuming this format of you input data (since the code you posted was mangled):
a = [[("1","2"),("5","6")], [("3","4"), ("5","6")], [("5","7"), ("2","3")]]
First flatten the list, and then do what you did before:
flattened_list = [item for sublist in a for item in sublist]
Unique = []
for pair in flattened_list:
if pair not in Unique:
Unique.append(pair)
print(Unique)
They are list of lists containing tuples... I want to generate one
list of unique tuples
The solution using collections.Counter object:
import collections
a = [[("1","2"),("5","6")], [("3","4"), ("5","6")], [("5","7"), ("2","3")]]
c = collections.Counter([t for l in a for t in l])
unique_tuples = [t for t in c.keys()]
print(unique_tuples)
The output:
[('3', '4'), ('5', '7'), ('1', '2'), ('2', '3'), ('5', '6')]
https://docs.python.org/3.1/library/collections.html#collections.Counter

Sorting a list of tuples by the first value [duplicate]

This question already has answers here:
Sort a list of tuples by 2nd item (integer value) [duplicate]
(9 answers)
Closed 6 years ago.
I wish to sort a list by the first value in each tuple, however my code does not produce any real sorted list, although the order of the list does change. My list contains tuples with a value for the 'PlayerCode' which is a unique key for each player. And 'Average Points' which is the average amount of points that the player scores each game. I wish to sort this list by 'Average Points' although I cant seem to get it right. Here is my code
def getKey(item):
return item[0]
def sortByPoints():
foundPlayers = []
with open ('PlayerList.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
foundPlayers.append((row['Average PTS'], row['PlayerCode']))
print(foundPlayers)
sortedPlayers = sorted(foundPlayers, key = getKey)
print (sortedPlayers)
Like I said, this does change the order of the list, but not the way im looking for, nor can I see any real pattern to which it has changed it to.
I am using python 3.4
Here is the output of the two lists
unsorted:
[('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]
sorted:
[('0', '5'), ('10', '7'), ('10', '9'), ('18', '6'), ('2', '10'), ('2', '12'), ('2', '15'), ('28', '4'), ('3', '13'), ('4', '3'), ('4', '11'), ('4', '14'), ('7', '1'), ('8', '2'), ('9', '8')]
You can use itemgetter imported from the built in module operator
Example code:
from operator import itemgetter
tuples = [(10, 3), (1, 4), (5, 4)]
sorted_tuples = sorted(tuples, key=itemgetter(0))
print(sorted_tuples)
Output:
[(1, 4), (5, 4), (10, 3)]
The problem is that you have strings and so it's sorting them alphabetically. Convert to integers:
foundPlayers.append((int(row['Average PTS']), int(row['PlayerCode'])))
row['Average PTS'] and row['Player Code'] return strings. Python will compare strings with (possibly) surprising results
>>> '6' > '42'
True
Convert row['Average PTS'] and row['Player Code'] using int.
You may use lambda function as key to sorted as:
sorted(my_list, key=lambda x: (int(x[1]), int(x[0])))
Here:
the list will be sorted based on the content at 1st index. In case of same value, list will be sorted based on 0th index.
Also you need to type-cast the values in tuple to int. As sorted does the lexicographically sorting. That means, for string values, '11' will come before '2'.
Below is the sample example:
>>> my_list = [('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]
>>> sorted(my_list, key=lambda x: (int(x[1]), int(x[0])))
[('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]
Here is my answer. I believe in maintaining the OP's original code as much as possible to make as fewer assumptions as possible. What if it is a requirement to have strings? Also, with as fewer additional imports as possible. Thus my solution would be:
def sortByPoints():
foundPlayers = []
with open ('PlayerList.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
foundPlayers.append((row['Average PTS'], row['PlayerCode']))
print(foundPlayers)
sortedPlayers = sorted(foundPlayers, key=lambda x: float(x[0]))
print(sortedPlayers)
Remove redundant getKey function, and use a lambda instead for neatness. Using float just in case it may be required in the future (as you are only using for comparison; using int only limits you and has no advantages over float).
I also believe in copy and paste code ;)
FYI, to change direction:
sortedPlayers = sorted(foundPlayers, key=lambda x: float(x[0]), reverse=True)
Also note that, if the intention is indeed for integers only, then you should do as everyone suggests and convert your elements to int:
def sortByPoints():
foundPlayers = []
with open ('PlayerList.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
foundPlayers.append((int(row['Average PTS']), int(row['PlayerCode'])))
print(foundPlayers)
sortedPlayers = sorted(foundPlayers, key=lambda x: x[0])
print(sortedPlayers)

Replacing values in two dimensional list

I am trying to build a Pajek NET file for network visualization in python. My approach is to use one two dimensional list or dict as a key for a second one and either replace the values or create a new list altogether. Example input:
Vertices = [('1', 'test.com'), ('2', 'testb.com'), ('3', 'testc.com'), ('4', '1.1.1.1'), ('5', '2.2.2.2'), ('6', '3.3.3.3')]
matches = [('test.com', '1.1.1.1'), ('test.com', '3.3.3.3'), ('test.com', '2.2.2.2'), ('testb.com', '3.3.3.3'), ('testc.com', '3.3.3.3')]
Result, replacing both k and v in matches with k in Vertices. With the output list rendered as such
Edges = [('1', '4'), ('1', '6'), ('1', '5'), ('2', 6'), ('3', '6')]
Unsure about how to tackle this one. Appreciate any insight from the gurus
If you chose a different data structure, it would be trivial:
>>> Vertices = {'test.com': '1', 'testb.com': '2', 'testc.com': '3', '1.1.1.1': '4', '2.2.2.2': '5', '3.3.3.3': '6'}
>>> matches = [('test.com', '1.1.1.1'), ('test.com', '3.3.3.3'), ('test.com', '2.2.2.2'), ('testb.com', '3.3.3.3'), ('testc.com', '3.3.3.3')]
>>> Edges = [(Vertices[v1], Vertices[v2]) for v1,v2 in matches]
>>> Edges
[('1', '4'), ('1', '6'), ('1', '5'), ('2', '6'), ('3', '6')]
Using your own data structure, it gets convoluted:
>>> Edges = []
>>> for item in matches:
... for vertex in Vertices:
... if vertex[1] == item[0]:
... i = vertex[0]
... break
... for vertex in Vertices:
... if vertex[1] == item[1]:
... j = vertex[0]
... break
... Edges.append((i,j))
...
>>> Edges
[('1', '4'), ('1', '6'), ('1', '5'), ('2', '6'), ('3', '6')]

trying to create a dictionary but do not know how to deal with \n

subject_dic = {}
inputFile = open(filename)
for line in inputFile:
split_line = string.split(line, ',')
subject_dic[split_line[0]] = tuple(split_line[1:3])
print subject_dic
I'm getting
{'6.08': ('1', '10\n'), '6.09': ('3', '7\n'), '6.19': ('8', '19'), '6.10': ('8', '18\n'), '6.00': ('10', '1\n'), '6.01': ('5', '4\n'), '6.02': ('5', '6\n'), '6.03': ('2', '9\n'), '6.04': ('1', '2\n'), '6.05': ('1', '18\n'), '6.06': ('5', '19\n'), '6.07': ('2', '10\n'), '6.13': ('9', '16\n'), '6.18': ('10', '4\n'), '6.15': ('10', '6\n'), '6.16': ('6', '9\n'), '6.12': ('6', '3\n'), '6.17': ('9', '3\n'), '6.14': ('10', '8\n'), '6.11': ('6', '8\n')}
but I don't know how to remove '\n' from the ends of my tuples. This is really simple but I can't find out how to do this. It's because I'm reading vertically from a file (hence the newline) but I don't want that '\n' in my dictionary.
Thanks!
split_line = split_line.strip()
See here.
If you're not working with a ridiculously large file, you can actually avoid having to use .strip() at all. If you read in the entire file as a string using .read() and then perform .splitlines() on that string.
Here is an example. I commented out your code where I changed things. I changed the example not to use slicing in exchange for explicit variable assignment.
subject_dic = {}
inputFile = open(filename)
# Turn "line1\nline2\n" into ['line1', 'line2']
inputData = inputFile.read().splitlines()
#for line in inputFile:
for line in inputData:
#split_line = string.split(line, ',')
#subject_dic[split_line[0]] = tuple(split_line[1:3])
mykey, myval1, myval2 = line.split(',') # Strings always have .split()
subject_dic[mykey] = (myval1, myval2) # Explicit tuple assignment
print subject_dic
Outputs:
{'6.00': ('10', '1'),
'6.01': ('5', '4'),
'6.02': ('5', '6'),
'6.03': ('2', '9'),
'6.04': ('1', '2'),
'6.05': ('1', '18'),
'6.06': ('5', '19'),
'6.07': ('2', '10'),
'6.08': ('1', '10'),
'6.09': ('3', '7'),
'6.10': ('8', '18'),
'6.11': ('6', '8'),
'6.12': ('6', '3'),
'6.13': ('9', '16'),
'6.14': ('10', '8'),
'6.15': ('10', '6'),
'6.16': ('6', '9'),
'6.17': ('9', '3'),
'6.18': ('10', '4'),
'6.19': ('8', '19')}
Use strip on the lines to trim off the new lines.
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
line = line.strip()
at the beginning of the loop.
This should work:
subject_dic = {}
inputFile = open(filename)
for line in map(lambda x: x.strip(), inputFile):
split_line = string.split(line, ',')
subject_dic[split_line[0]] = tuple(split_line[1:3])
print subject_dic

Categories