Related
my_list = [
['common1', '112', '4000'],
['common1', '11', '11'],
['common1', '33', '33'],
['common1', '33', '900'],
['common2', '31', '400'],
['common2', '2', '2666']
]
I want to convert this list into
[
['common1', '112', '4000'],
['', '11', '11'],
['', '33', '33'],
['', '33', '900'],
['common2', '31', '400'],
['', '2', '2666']
]
Here what I want is replace the common1 with empty string '' and keep only the first value. Similarly for common2 value.
You can try this:
def replace_duplicate(my_list):
prev_val = ''
for lst in my_list:
if prev_val == lst[0]:
lst[0] = ''
else:
prev_val = lst[0]
my_list = [
['common1', '112', '4000'],
['common1', '11', '11'],
['common1', '33', '33'],
['common1', '33', '900'],
['common2', '31', '400'],
['common2', '2', '2666'],
['common3', '115', '5000'],
['common3', '12', '15'],
['common1', '222', '6000'],
['common1', '55', '66'],
['common1', '77', '99']
]
replace_duplicate(my_list)
print(my_list)
Output:
[
['common1', '112', '4000'],
['', '11', '11'],
['', '33', '33'],
['', '33', '900'],
['common2', '31', '400'],
['', '2', '2666'],
['common3', '115', '5000'],
['', '12', '15'],
['common1', '222', '6000'],
['', '55', '66'],
['', '77', '99']
]
Here's a super simple and easy solution
def remove_sec_dups(data):
seen = set() # keep track of first seen values
for sub in data: # get sublists
first = sub[0] # get first item
if first in seen:
sub[0] = '' # empty
else:
seen.add(first) # add this to our seen list
remove_sec_dups(my_list)
print(my_list)
This outputs [['common1', '112', '4000'], ['', '11', '11'], ['', '33', '33'], ['', '33', '900'], ['common2', '31', '400'], ['', '2', '2666']]
The following works for the general unsorted case, too:
seen = set()
for i, (head, *_) in enumerate(my_list):
if head in seen:
my_list[i][0] = ""
seen.add(head)
I'm having a problem on my schoolwork. My goal is I want to compare the first column, first row and second column, second row.
I'm new to pandas so I have tried browsing answers on the internet but sadly there isn't. I have think of a way which is I have a converted pandas into a array and store the first column to the first array and second column to the second array.
first_array = [4,10,17,24,82,93,35,40,49,71,78]
last_array = [9,16,23,29,89,97,39,48,57,77,85]
and after that i've popped the first number of first_array which is 4 then insert a 0 into the last which looks likes this
first_array = [10,17,24,82,93,35,40,49,71,78, 0]
last_array = [ 9,16,23,29,89,97,39,48,57,77,85]
and a little bit of for loop which is
if first_array < last_array:
print ("===")
but it seems that my logic is correct but there's a slight wrong with the output i'm expecting
['10', '17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['9', '16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
===
['17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['24', '82', '93', '35', '40', '49', '71', '78', '0']
['23', '29', '89', '97', '39', '48', '57', '77', '85']
['82', '93', '35', '40', '49', '71', '78', '0']
['29', '89', '97', '39', '48', '57', '77', '85']
['93', '35', '40', '49', '71', '78', '0']
['89', '97', '39', '48', '57', '77', '85']
['35', '40', '49', '71', '78', '0']
['97', '39', '48', '57', '77', '85']
===
['40', '49', '71', '78', '0']
['39', '48', '57', '77', '85']
['49', '71', '78', '0']
['48', '57', '77', '85']
['71', '78', '0']
['57', '77', '85']
['78', '0']
['77', '85']
['0']
['85']
===
At the first "===" 10 isn't < 9 which makes me think. Did i miss something? Thank you in advance.
This is a snippet of my code. The print(join_tag) is the output i needed for this.
normalized_text = []
first_array = [10,17,24,82,93,35,40,49,71,78, 0]
last_array = [ 9,16,23,29,89,97,39,48,57,77,85]
for word in normalized_text:
join_tag = ' '.join(word)
print (join_tag)
if first_array < last_array:
print ('===')
first_array.pop(0)
last_array.pop(0)
Expected output would be
['10', '17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['9', '16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['24', '82', '93', '35', '40', '49', '71', '78', '0']
['23', '29', '89', '97', '39', '48', '57', '77', '85']
['82', '93', '35', '40', '49', '71', '78', '0']
['29', '89', '97', '39', '48', '57', '77', '85']
['93', '35', '40', '49', '71', '78', '0']
['89', '97', '39', '48', '57', '77', '85']
['35', '40', '49', '71', '78', '0']
['97', '39', '48', '57', '77', '85']
===
['40', '49', '71', '78', '0']
['39', '48', '57', '77', '85']
['49', '71', '78', '0']
['48', '57', '77', '85']
['71', '78', '0']
['57', '77', '85']
['78', '0']
['77', '85']
['0']
['85']
===
if I understood your question correctly, you want to compare the 4 with the 16, right?
If so, putting the data back in a dataframe:
import numpy as np
import pandas as pd
first_array = np.array([4,10,17,24,82,93,35,40,49,71,78])
last_array = np.array([9,16,23,29,89,97,39,48,57,77,85])
df = pd.DataFrame(np.vstack((first_array, last_array)))
Then:
df.iloc[0, 0] < df.iloc[1, 1]
Will compare 4 < 16
To compare them all:
for i in range(len(df.columns)-1):
print(df.iloc[0, i] < df.iloc[1, i+1])
Your arrays contain strings. Convert them to numbers.
In the first case '10' < '9' is true because the string '10' is alphabetically less than the string '9'
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
print(open_csv()[1])
This is the output for open_csv():
['Name', 'Gender', 'Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6']
['Aisha', 'F', '0', '33', '67', '27', '12', '14']
['Alex', 'M', '12', '90', '34', '56', '93', '39']
['Bala', 'M', '13', '25', '58', '17', '49', '29']
['Denise', 'F', '13', '93', '84', '53', '65', '62']
['Farhan', 'M', '15', '5', '10', '62', '34', '11']
['Gopi', 'M', '21', '61', '39', '32', '91', '32']
['Irfan', 'M', '26', '36', '3', '95', '36', '39']
['Jun Ming', 'M', '29', '86', '77', '6', '91', '61']
['Lily', 'F', '30', '34', '46', '96', '100', '44']
['Mei Ling', 'F', '39', '58', '9', '61', '32', '46']
['Muthu', 'M', '39', '60', '13', '69', '55', '100']
['Nurul', 'F', '50', '35', '4', '27', '11', '97']
['Priya', 'F', '50', '25', '47', '15', '35', '86']
['Siti', 'F', '58', '71', '13', '19', '58', '30']
['Elisa', 'F', '59', '22', '73', '52', '77', '49']
['Dennis', 'M', '65', '94', '83', '67', '37', '22']
['Harry', 'M', '74', '75', '76', '82', '57', '1']
['Gary', 'M', '90', '12', '70', '86', '50', '59']
['Terry', 'M', '93', '84', '26', '99', '90', '72']
['Corinne', 'F', '100', '17', '88', '14', '33', '9']
When I run this code, I get TypeError:"NoneType" object is not subscriptable. Why is this so and how do I fix it?
If you want to print the second row in your CSV file, you could modify your function as follows:
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r', newline='') as f:
return list(csv.reader(f))
print(open_csv()[1])
This will return all the data as a list of rows, by adding [1] you will be displaying the second row (with [0] being the first row). The reason you were getting the error is that your code did not have a return statement in the function. The default is for Python to return None. So in effect your code was doing:
print(None[0])
Don't forget to add newline='' when using it for a csv.reader()
Turn this function into a generator.
def open_csv():
...
for row in reader:
print(row) // change this to yield(row)
list(open_csv())[0]
I am new to python. I need to create a list of integers from 1 to 70, but for each integer I want to make it a string and a comma after it and store it in another list.
Ex:
for i in range (1,71):
list_of_ints.append(i)
{ Some code
}
it should then be something like this
columns = ['1','2','3','4'.......'70']
Use [str(i) for i in range(1, 71)]. This gives you the list of str(i) for all i in range(1, 71). The function str(i) returns i as a str value instead of as an int
Seems like you want something like this,
>>> l = []
>>> for i in range(1,71):
l.append(str(i))
>>> l
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']
new_list = [str(x) for x in range(1, 71)]
Using a list comprehension to achieve the same result.
You can use map to help you here:
>>> list_of_ints = range(1, 71)
>>> list_of_ints = map(str, list_of_ints)
>>> print list_of_ints
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']
>>>
I like to move each item in this list to another nested list could someone help me?
a = [['AAA', '1', '1', '10', '92'], ['BBB', '262', '56', '238', '142'], ['CCC', '86', '84', '149', '30'], ['DDD', '48', '362', '205', '237'], ['EEE', '8', '33', '96', '336'], ['FFF', '39', '82', '89', '140'], ['GGG', '170', '296', '223', '210'], ['HHH', '16', '40', '65', '50'], ['III', '4', '3', '5', '2']]
On the end I will make list like this:
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF'.....],
['1', '262', '86', '48', '8', '39', ...],
['1', '56', '84', '362', '33', '82', ...],
['10', '238', '149', '205', '96', '89', ...],
...
...]
Use zip with * and map:
>>> map(list, zip(*a))
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Note that map returns a map object in Python 3, so there you would need list(map(list, zip(*a)))
Using a list comprehension with zip(*...), this would work as is in both Python 2 and 3.
[list(x) for x in zip(*a)]
NumPy way:
>>> import numpy as np
>>> np.array(a).T.tolist()
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Through list comprehension:
[[x[i] for x in mylist] for i in range(len(mylist[0]))]
You can also use
a= np.array(a).transpose().tolist()
You could also do:
row1 = [1,2,3]
row2 = [4,5,6]
row3 = [7,8,9]
matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix], [row[2] for row in matrix]]