How to determine how many unique values are in a dictionary? - python

I have a dictionary containing lists that look something like this:
['33', '34', '34', '34', '35', '35', '36', '36', '38']
['34', '37', '38']
['33', '33', '35', '35', '38', '38']
I'm trying to get the number of unique values for each of these lists automatically (i.e. the third list would have a value of 3).
How should I do this?

print len(set([1,1,2,2,3,3]))
is that what you are looking for?
sets are just like lists ... except they only contain unique elements, and they have no order

Build a set and then get its length:
print len(set(yourlist))

len(set(x)) is the number of unique elements in collection x.
So for that dictionary:
d
Out[16]:
{'a': ['33', '34', '34', '34', '35', '35', '36', '36', '38'],
'b': ['34', '37', '38'],
'c': ['33', '33', '35', '35', '38', '38']}
You'd have
for k,v in d.items():
print('{}: {}'.format(k,len(set(v))))
a: 5
c: 3
b: 3
python 3 syntax.

Related

Numpy / Flatten a list

I have create this of character
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
result :
[['20', '20', '20'], ['35', '35'], ['40', '40', '40', '40'], ['10', '10'], ['15', '15', '15']]
I can convert it into a single list using list comprehension
charlist = [x for sublist in list1 for x in sublist]
print(charlist)
['20', '20', '20', '35', '35', '40', '40', '40', '40', '10', '10', '15', '15', '15']
I was wondering how to do that with numpy
listNP=np.array(list1)
gives as output :
array([list(['20', '20', '20']), list(['35', '35']),
list(['40', '40', '40', '40']), list(['10', '10']),
list(['15', '15', '15'])], dtype=object)
The fact is that listNP.flatten() gives as an output the same result. Probably I missed a step when converting the list into an numpy array
You can bypass all the extra operations and use np.repeat:
>>> np.repeat(['20', '35', '40', '10', '15'], [3, 2, 4, 2, 3])
array(['20', '20', '20', '35', '35', '40', '40', '40', '40',
'10', '10', '15', '15', '15'], dtype='<U2')
If you need dtype=object, make the first argument into an array first:
arr1 = np.array(['20', '35', '40', '10', '15'], dtype=object)
np.repeat(arr1, [3, 2, 4, 2, 3])
Use hstack()
import numpy as np
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
flatlist = np.hstack(list1)
print(flatlist)
['20' '20' '20' '35' '35' '40' '40' '40' '40' '10' '10' '15' '15' '15']
In trying to construct your ListNP with np.array as you do in the OP, I got a warning about jagged arrays and having to use dtype=object, but letting hstack construct it directly doesn't evoke a warning (thanks #Michael Delgado in the comments)

sorted() unable to sort list of chars [duplicate]

This question already has answers here:
How to sort a list of strings numerically?
(14 answers)
Closed 3 years ago.
I want to sort a list containing integers as chars. e.g:
l = ['1', '10', '11', '12', '16', '17', '2', '24', '26', '27', '28', '30', '32', '34', '35', '36', '43', '45', '47', '49', '50', '6', '9']
print(sorted(l))
is returning:
['1', '10', '11', '12', '16', '17', '2', '24', '26', '27', '28', '30', '32', '34', '35', '36', '43', '45', '47', '49', '50', '6', '9']
why sorted() is acting unusually?
Sorted is acting exactly as it should.
These are strings, not integers, so sorted sorts first by the first character, then by the second character.
If we want to sort ['1', '2', '12'], we get ['1', '12', '2']:
1
12
2
sorted first sorts by the first column, then by the second column.

How to compare the first column to the second column

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'

How do I do something to each element in a list?

I am currently making a bingo game that calls a number when enter is pressed. It does this until the player's bingo card is empty.
I can not get it so that it calls the first number in the list of numbers and goes on to the second then the third and so on without making a script for each of the 99 numbers.
What I want to do is get the program to find the first element of nums and then get the program to print the string and if the string is in the player_1 list then remove the string from player_1 and keep on doing the process until either the program has gone through all the strings or the player_1 list has no strings left in the list.
Here is my code:
import random
def player_1_card():
print(player_1[1:4])
print(player_1[5:8])
print(player_1[9:12])
print(player_1[13:16])
return
nums = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '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', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']
random.shuffle(nums)
player_1 = random.sample((nums), 16)
while len(player_1) != 0:
input("Press <enter> to call a number")
for i in nums:
print("Your current bingo card:")
player_1_card()
print("Number called is", # )
input("Press <enter> to call a number")
if nums(#) in player_1:
nums.remove(#)
I have placed hashes where I need some help with the code.
I am using Python 3.5.2.

Python - Convert each integer to string in list and add a comma

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']
>>>

Categories

Resources