Removing duplicated numbers within tuples - python

For example I want to remove the extra 1s and 2s in this tuple ( '1',
'1',
'1',
'1',
'1',
'1',
'1',
'2',
'2',
'2') to return ('1', '2')
How can I do this?

You can't modify tuple in place, So definitely you have to get new tuple. You can use set to remove duplicate elements.
>>> tp = ( '1', '1', '1', '1', '1', '1', '1', '2', '2', '2')
>>> tp = tuple(set(tp))
>>> tp
('1', '2')

As Alok correctly mentions, you have to create a new tuple. As Alok demonstrates, you can of course assign it to the variable that previously held the original tuple.
If you don't care about order, use set as Alok suggests. If you however want to preserve the order (of first occurrence of each unique value), you can use a very similar trick with an OrderedDict:
from collections import OrderedDict
# Different example to demonstrate preserved order
tp = ('2', '2', '2', '1', '1', '1', '1', '1', '1', '1')
tp = tuple(OrderedDict.fromkeys(tp).keys())
# Now, tp == ('2', '1')

They are correct, tuples in python are immutable therefore you cannot update the current tuple. This function loops through data creating a list of indexes that do not currently exist! It then returns a tuple of the list! Good luck!
data = ( '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '3', '4','4')
def removeDoubles(data):
out = []
for i in data:
if i in out:
continue
out.append(i)
del data
return tuple(out)
print removeDoubles(data)

You can do this using a set:
a = ( '1', '1', '1', '1', '1', '1', '1', '2', '2', '2')
b = tuple(set(a))
If the order is important you could sort b.

Related

Check if multiple elements are in a list

i have 2 lists :
A = ['1', '2', '3', '4', '5']
B = ['0', '1', '9', '3', '0']
and i want to check if elements in list B are in A and return a list, if so it should return the same number, if not it should return empty string '', here is the result i'm looking for :
C = ['', '1', '', '3', '']
i Tried using for loop and append result to an empty list, but i got this :
C = ['', '1', '', '', '', '', '', '', '3', ''...]
it doubled the number of elements in the list cuz it looks for the first number in the entire list then move to second one, which makes sense since i'm using for loop, what should i use instead to get back a 5 elements list please ?
thanks in advance.
To get your required output, you can just loop through B and check if the item exists in A. If it does, then append it in c otherwise append an empty string to c.
A = ['1', '2', '3', '4', '5']
B = ['0', '1', '9', '3', '0']
c = []
for i in B:
if i in A:
c.append(i)
else:
c.append('')
print(c)
The output of the above code
['', '1', '', '3', '']

Taking a column from a csv file and putting it into a list in python

I need to write some code in python that takes a column from an csv file and makes it a list. Here is my code until now.
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('Team1_BoMInput.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
y = (columns['Quantity'])
x = (columns[('Actual Price')])
b = ['2', '2', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '3', '4', '1', '1', '1', '8', '2', '2', '1', '1', '1', '1', '4', '1', '2', '2', '2', '1', '2', '2', '2', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '3', '2', '1', '1', '1']
a = ['$6.41', '$14.97', '$6.78', '$11.44', '$22.61', '$1.58', '$11.68', '$19.99', '$12.99', '$3.66', '$24.99', '$1.04', '$0.09', '$1.92', '$4.80', '$1.50', '$17.92', '$1.36', '$65.52', '$24.38', '$1.91', '$3.40', '$13.79', '$39.55', '$1.94', '$3.38', '$11.34', '$18.33', '$21.13', '$8.24', '$30.14', '$125.97', '$26.54', '$8.58', '$12.77', '$11.42', '$1.32', '$2.63', '$8.58', '$0.40', '$0.57', '$2.54', '$2.83', '$1.41', '$9.03', '$3.38', '$5.98', '$4.51', '$2.54', '$6.76', '$4.51', '$1.13', '$14.24']
for i in range(0, len(b)):
b[i] = float(b[i])
print(b)
x = ([s.strip('$') for s in a])
for i in range(0, len(x)):
x[i] = float(x[i])
print(x)
instead of having the values of a and b listed in the program, I want it to take the column from the csv file and use the values of that.
Thanks in advance
Try this:
import pandas as pd
df=pd.read_csv("Team1_BoMInput.csv")
y=list(df['Quantity'])
x=list(df['Actual Price'])
Refer the Below Code, leveraging the pandas library for faster computations and lesser code
import pandas as pd
df=pd.read_csv("Team1_BoMInput.csv")
quantity_list_value=list(df.loc[:,"Quantity"].astype(float).values)
price_list_value=list(df.loc[:,"Actual Price"].apply(lambda x: x.replace("$","")).astype(float).values)
I think you code will not run unless you change it to this:
import csv
from collections import defaultdict
columns = defaultdict(list)
with open('Team1_BoMInput.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
if k not in columns:
columns[k] = list()
columns[k].append(v)
# Rest of your code

python slice set in list

i would like to slice a set within a list, but every time i do so, i get an empty list in return.
what i try to accomplish (maybe there is an easier way):
i got a list of sets
each set has 5 items
i would like to compare a new set against the list (if the set already exists in the list)
the first and the last item in the set is irrelevant for the comparison, so only the positions 2-4 are valid for the search of already existing sets
here is my code:
result_set = ['1', '2', '3', '4', '5']
result_matrix = []
result_matrix.append(result_set)
slicing the set is no problem:
print result_set[1:4]
['2', '3', '4']
print result_matrix[:][1:4]
[]
i would expect:
[['2', '3', '4']]
I think this is what you want to do:
>>> target_set = ['2', '3', '4']
>>> any([l for l in result_matrix if target_set == l[1:-1]])
True
>>> target_set = ['1', '2', '3']
>>> any([l for l in result_matrix if target_set == l[1:-1]])
False
Generalising and making that a function:
def is_set_in_matrix(target_set, matrix):
return any(True for l in matrix if list(target_set) == l[1:-1])
>>> result_matrix = [['1', '2', '3', '4', '5']]
>>> is_set_in_matrix(['1', '2', '3'], result_matrix)
False
>>> is_set_in_matrix(['2', '3', '4'], result_matrix)
True
# a quirk - it also works with strings...`
>>> s = '234'
>>> is_set_in_matrix(s, result_matrix)
True
Note that I have used l[1:-1] to ignore the first and last elements of the "set" in the comparison. This is more flexible should you ever need sets of different lengths.
>>> result_set = ['1', '2', '3', '4', '5']
>>> print result_set[1:4]
['2', '3', '4']
>>> result_matrix.append(result_set[1:4])
>>> result_matrix
[['2', '3', '4']]
Using result_matrix[:] returns the whole matrix as it is. You need to treat the result you want as a part of the array.
>>> result_matrix.append(result_set)
>>> result_matrix[:]
[['1', '2', '3', '4']]
>>> result_matrix[:][0]
['1', '2', '3', '4']
>>> result_matrix[0][1:4]
['2', '3', '4']
Also, as pointed out by falsetru:
>>> result_matrix.extend(result_set)
>>> result_matrix
['1', '2', '3', '4']
>>> result_matrix[1:4]
['2', '3', '4']

List circulation in Python for Project Euler 37

So, I was doing Project Euler 37
I need to circulate a list
input: 2345 # converted to list inside function
expected output: [[3,4,5,2],[4,5,2,3],[5,2,3,4],[2,3,4,5]]
Here is my function for that
def circulate(n): #2345
lst=list(str(n)) #[2,3,4,5]
res=[]
for i in range(len(lst)):
temp=lst.pop(0)
lst.append(temp)
print lst #print expected list
res.append(lst) #but doesn't append as expected
return res
print circulate(2345)
My output is:
['3', '4', '5', '2']
['4', '5', '2', '3']
['5', '2', '3', '4']
['2', '3', '4', '5']
[['2', '3', '4', '5'], ['2', '3', '4', '5'], ['2', '3', '4', '5'], ['2', '3', '4', '5']]
The function prints lst correct every time, but doesn't append as expected.
What I am doing wrong?
You need to append copies of your list to res:
res.append(lst[:])
You were appending a reference to the list being altered instead; all references reflect the changes made to the one object.
You may want to look at collections.deque() instead; this double-ended list object supports efficient rotation with a .rotate() method:
from collections import deque
def circulate(n):
lst = deque(str(n))
res = []
for i in range(len(lst)):
lst.rotate(1)
res.append(list(lst))
return res

Zipping two lists gives me proper pairs but shuffles the order of items

I am having problem with the zip command. I am fairly new to python and use it to manipulate inputs and outputs from the computaitonal chemistry software Gaussian.
My problem is the following. I extract that atomic number and the coordinates of the corresponding atom as two separate lists that i then zip together.
The thing is, the newly created dictionnary, while correctly associating the atomic number to the corresponding coordinates, appears to shuffle the order of those pairs.
This is my code:
str1 = lines[best_i + count].split()
str1 = list(filter(None, str1))
li_atoms.append(str1[1])
li_lines.append(str1[3] + " " + str1[4] + " " + str1[5] + "\n")
dict_lines = dict(zip(li_lines, li_atoms))
new_line = str1[1] + " " + str1[3] + " " + str1[4] + " " + str1[5] + "\n"
print(li_atoms)
print(li_lines)
print(new_line)
The first 4 lines are there to show how I create the lists I then zip.
This is what I am getting:
['6', '6', '6', '6', '6', '6', '1', '1', '1', '1', '1', '8', '1', '8', '1', '1', '8', '1', '1', '8', '1', '1']
['-0.934843 0.899513 0.316846\n',
'-2.190300 1.027357 -0.281071\n',
'-2.985223 -0.106295 -0.461617\n',
'-2.532226 -1.360236 -0.051572\n',
'-1.273703 -1.473149 0.546850\n',
'-0.466792 -0.350395 0.736201\n',
'-2.544435 2.004569 -0.604798\n',
'-3.961332 -0.002247 -0.927404\n',
'-3.152766 -2.239891 -0.193462\n',
'-0.912684 -2.443561 0.876875\n',
'0.509490 -0.433361 1.204588\n',
'-0.107548 1.983966 0.517635\n',
'-0.544217 2.786118 0.189973\n',
'1.956069 -1.097186 -1.539946\n',
'0.995285 -1.167127 -1.420910\n',
'2.137200 -0.128588 -1.433795\n',
'2.906601 -0.987077 1.012487\n',
'3.770505 -1.401214 1.155911\n',
'2.616140 -1.289889 0.114737\n',
'2.585476 1.353513 -0.478949\n',
'1.755805 1.714987 -0.116377\n',
'2.904746 0.753703 0.231017\n']
{'-1.273703 -1.473149 0.546850\n': '6',
'-2.190300 1.027357 -0.281071\n': '6',
'-2.544435 2.004569 -0.604798\n': '1',
'2.585476 1.353513 -0.478949\n': '8',
'2.906601 -0.987077 1.012487\n': '8',
'-3.961332 -0.002247 -0.927404\n': '1',
'0.995285 -1.167127 -1.420910\n': '1',
'3.770505 -1.401214 1.155911\n': '1',
'-2.985223 -0.106295 -0.461617\n': '6',
'-0.544217 2.786118 0.189973\n': '1',
'2.616140 -1.289889 0.114737\n': '1',
'-0.912684 -2.443561 0.876875\n': '1',
'-2.532226 -1.360236 -0.051572\n': '6',
'1.755805 1.714987 -0.116377\n': '1',
'2.137200 -0.128588 -1.433795\n': '1',
'1.956069 -1.097186 -1.539946\n': '8',
'0.509490 -0.433361 1.204588\n': '1',
'-3.152766 -2.239891 -0.193462\n': '1',
'-0.934843 0.899513 0.316846\n': '6',
'2.904746 0.753703 0.231017\n': '1',
'-0.107548 1.983966 0.517635\n': '8',
'-0.466792 -0.350395 0.736201\n': '6'}
And I would like to have that dictonnary in the same order as both lists above.
Thanks foir looking into it.
Python dictionaries have no set order. The behaviour you see is entirely normal and correct.
Quoting the Python documentation for the dict.items() method:
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.
See Why is the order in dictionaries and sets arbitrary? for why that is.
If you require ordering to be preserved, use a collections.OrderedDict() instead:
from collections import OrderedDict
dict_lines = OrderedDict(zip(li_lines, li_atoms))

Categories