How to print elements in a list in new lines? - python

I have a list
L = Counter(mywords)
Where
mywords = ['Well', 'Jim', 'opportunity', 'I', 'Governor', 'University', 'Denver', 'hospitality', 'There', 'lot', 'points', 'I', 'make', 'tonight', 'important', '20', 'years', 'ago', 'I', 'luckiest', 'man', 'earth', 'Michelle', 'agreed', 'marry', '(Laughter)', 'And', 'I', 'Sweetie', 'happy']
It's much longer than that but that's a snippet.
Now what I do next is:
print ("\n".join(c.most_common(10)))
Because I want it to show the 10 most commonly used words in that list AND their counts, but I want it to print out into new lines for each item in the list, instead I get this error:
TypeError: sequence item 0: expected str instance, tuple found
Any help would be appreciated, using Python 3.

print ("\n".join(map(str, c.most_common(10))))
If you want more control over the format, you can use a format string like this
print ("\n".join("{}: {}".format(k,v) for k,v in c.most_common(10)))

The simplest is:
for item, freq in L.most_common(10):
print(item, 'has a count of', freq) # or
print('there are {} occurrences of "{}"'.format(freq, item))

If you just want the strings:
print("\n".join(element for element, count in c.most_common(10)))
If you want the strings and the counts printed in the form ('foo', 11):
print ("\n".join(str(element_and_count)
for element_and_count in c.most_common(10)))
If you want the strings and counts in some other format of your choice:
print ("\n".join("{}: {}".format(element, count)
for element, count in c.most_common(10)))
Why? The most_common function returns (element, count) pairs. Those things are tuples, not strings. You can't just join tuples together. You can, of course, convert it to a string (option #2 above), but that only works if you actually want the format ('foo', 11) for each line. To get the other two options, you want to ignore half the tuple and use the other, or write your own format expression.
In any case, you want to do something to each member of the sequence returned by most_common. The Pythonic way to do that is with a list comprehension or generator expression.
Meanwhile, you should learn how to debug these kinds of cases. When join gives you a TypeError, break it up into pieces until you find the one that stores working (and try it with 2 instead of 10, just so there's less to read):
>>> print("\n".join(c.most_common(2)))
TypeError: sequence item 0: expected str instance, tuple found
>>> c.most_common(2)
[('I', 4), ('man', 1)]
Aha! Each thing in the list is a tuple of two things, not just a string. Why?
>>> help(c.most_common)
most_common(self, n=None) method of collections.Counter instance
List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
OK, so it returns the most common elements and their counts. I just want the elements. So:
>>> [element for element, count in c.most_common(2)]
['I', 'man']
Now that's something I can join:
>>> '\n'.join([element for element, count in c.most_common(2)])
'I\nman'
And I don't need both brackets and parents (I can just use an expression instead of a list comprehension):
>>> '\n'.join(element for element, count in c.most_common(2))
'I\nman'
And now, I can print it:
>>> print('\n'.join(element for element, count in c.most_common(2)))
I
man
And now that it's working, print all 10:
>>> print('\n'.join(element for element, count in c.most_common(10)))

I'm surprised that nobody suggested using the unpacking operator *, since you say python3 so why not do the following, you can test it here too.
print(*[x[0]for x in L.most_common(10)], sep="\n")
Related questions
Python 3.3: separation argument (sep) giving an error
What does ** (double star) and * (star) do for parameters?
List comprehensions

Related

Find unique list of tuples irrespective of order inside tuple

Given input:
[('is','my','order'), ('my','order', 'is'), ('temp', 'ques'), ('ques','temp')]
Desired output:
[('is','my','order'), ('temp', 'ques')]
In the output, the order of the tuple or the order of the contents of tuple doesn't matter.
Because for you order doesn't matter, you can use set to achieve this:
>>> input_list = [('is','my','order'), ('my','order', 'is'), ('temp', 'ques'), ('ques','temp')]
>>> set(tuple(sorted(l)) for l in input_list)
set([('ques', 'temp'), ('is', 'my', 'order')])
Firstly sort the content of each nested tuple to ensure your set considers tuples with common items as same. Then I am type-casting it again to tuple because sorted returns list which are unhashable. Finally, set removes duplicate entries of your tuples.
Please refer "set" documentation for more details.
You can sort each of the sublists and then add them to a list, dropping all duplicates
output_list = []
for tup in map(sorted, tuple_list):
if tup not in output_list:
output_list.append(tup)
print(output_list)
mytuples = [('one','two'), ('two','one)]
myset=set()
for x in mytuples:
myset.update(x)
print(myset)
output {'two', 'one'}
this will give you a set of all the unique values in your dataset. maybe not helpful
so you may have to check each tuple against every other tuple with the same length assuming you want to keep the tuple structure

How to pass varying number of arguments from tuple to string format in python?

I am facing a difficulty in passing varying number of arguments from tuple to string format.
Sometimes my tuple consists of only one value, but sometimes up to 10 values.
If I want to print all of the values in print statement how do I do that?
I have tried:
tup = ('val1', 'val2', 'val3')
print('List consists of: {}'.format(*tup))
but it prints out only the first value. Unfortunately, each time I have varying number of arguments to print.
The special syntax *args (*mystrings in our case) in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.
The nice thing here that they are passed as a tuple by python which make this a
Classic and strait forward approach. see this approach in the code snippet bellow:
def foo(*mystrings): ## which ofen used as *args
for string in mystrings:
print(string) ## Or do what ever you want.
Now call it:
tup = ('val1', 'val2', 'val3')
foo(tup)
If you want just to construct a string from the tuple you can use join() method to do the job:
strt=' '.join(tup)
str.join(iterable)
Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.
Remove the * so you are not unpacking the tuple:
tup = ('val1', 'val2', 'val3')
print('List consists of: {}'.format(tup))
Output:
List consists of: ('val1', 'val2', 'val3')
You don't need .format() or .join() in this case. Just pass *tup as agument to the print function:
>>> tup = ('h', 'e', 'l', 'l', 'o', 'world')
>>> print(*tup)
h e l l o world
It works for number's too;
tup = (1, 2 ,3)
print('List consists of:', *tup)
[OUTPUT]: List consists of: 1 2 3
And you can add separators if you want;
print('List consists of:', *tup, sep=', ')
[OUTPUT]: List consists of: 1, 2, 3

How to get common prefix of strings in a list [duplicate]

This question already has answers here:
Determine prefix from a set of (similar) strings
(11 answers)
Closed 2 years ago.
I need to know how to identify prefixes in strings in a list. For example,
list = ['nomad', 'normal', 'nonstop', 'noob']
Its answer should be 'no' since every string in the list starts with 'no'
I was wondering if there is a method that iterates each letter in strings in the list at the same time and checks each letter is the same with each other.
Use os.path.commonprefix it will do exactly what you want.
In [1]: list = ['nomad', 'normal', 'nonstop', 'noob']
In [2]: import os.path as p
In [3]: p.commonprefix(list)
Out[3]: 'no'
As an aside, naming a list "list" will make it impossible to access the list class, so I would recommend using a different variable name.
Here is a code without libraries:
for i in range(len(l[0])):
if False in [l[0][:i] == j[:i] for j in l]:
print(l[0][:i-1])
break
gives output:
no
There is no built-in function to do this. If you are looking for short python code that can do this for you, here's my attempt:
def longest_common_prefix(words):
i = 0
while len(set([word[:i] for word in words])) <= 1:
i += 1
return words[0][:i-1]
Explanation: words is an iterable of strings. The list comprehension
[word[:i] for word in words]
uses string slices to take the first i letters of each string. At the beginning, these would all be empty strings. Then, it would consist of the first letter of each word. Then the first two letters, and so on.
Casting to a set removes duplicates. For example, set([1, 2, 2, 3]) = {1, 2, 3}. By casting our list of prefixes to a set, we remove duplicates. If the length of the set is less than or equal to one, then they are all identical.
The counter i just keeps track of how many letters are identical so far.
We return words[0][i-1]. We arbitrarily choose the first word and take the first i-1 letters (which would be the same for any word in the list). The reason that it's i-1 and not i is that i gets incremented before we check if all of the words still share the same prefix.
Here's a fun one:
l = ['nomad', 'normal', 'nonstop', 'noob']
def common_prefix(lst):
for s in zip(*lst):
if len(set(s)) == 1:
yield s[0]
else:
return
result = ''.join(common_prefix(l))
Result:
'no'
To answer the spirit of your question - zip(*lst) is what allows you to "iterate letters in every string in the list at the same time". For example, list(zip(*lst)) would look like this:
[('n', 'n', 'n', 'n'), ('o', 'o', 'o', 'o'), ('m', 'r', 'n', 'o'), ('a', 'm', 's', 'b')]
Now all you need to do is find out the common elements, i.e. the len of set for each group, and if they're common (len(set(s)) == 1) then join it back.
As an aside, you probably don't want to call your list by the name list. Any time you call list() afterwards is gonna be a headache. It's bad practice to shadow built-in keywords.

How to turn a list of bigrams to a list of tokens using Python

I want to turn a list of bigrams to a list of tokens using Python 3.6.
I have something like:
input_list = [(‘hi’, ‘my’), (‘my’, ‘name’), (‘name’, ‘is’), (‘is’, ‘x’)]
I want to turn this to:
output_list = [‘hi’, ‘my’, ‘name’, ‘is’, ‘x’]
You can start with using a list comprehension to flatten the list and then take a set of that:
flat_list = [x for sublist in input_list for x in sublist]
output_list = set(flat_list)
output_list
{'hi', 'is', 'my', 'name', 'x'}
If all input follow that structure I would extract first part of first tuple, then last element from every tuple, that is:
input_list = [("hi", "my"), ("my", "name"), ("name", "is"), ("is", "x")]
output_list = [input_list[0][0]]+[i[-1] for i in input_list]
print(output_list) # ['hi', 'my', 'name', 'is', 'x']
I used followed python features:
indexing, [0][0] means first element of first element (if that is not clear I suggest searching for nesting first), [-1] means last element (first element starting from end)
list comprehension, to get last element of every element of list
list concatenation (denoted by +) to "glue" two lists together
If you do not want to create a separate list to store the flattened values, and save space and avoid loops you may try this:
from itertools import chain
lst = [('hi', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'x')]
flattened = chain(*lst)
elems = list(dict.fromkeys(flattened).keys())
print(elems)
Here chain(*lst) basically unpacks the elements and flattens the list, and stores it in a iterator object, as opposed to actually storing as list. Then you can convert those to set and back, but they may mess the ordering. So you take all those values and try to convert them to keys of dictionary. As dictionaries cannot have duplicate keys, it will only take the unique elements. So if you take the keys of that dict, you will get the unique elements from the flattened list. NOTE: The order is guaranteed to be maintained from Python 3.7.

Can i extract a specific character from a string stored in a list?

str=['qIQNlQSLi', 'eOEKiVEYj', 'aZADnMCZq', 'bZUTkLYNg', 'uCNDeHSBj', 'kOIXdKBFh']
I want to extract the 5th index of each string in list i.e o/p is linked.
You could do with list comprehension,
string=['qIQNlQSLi', 'eOEKiVEYj', 'aZADnMCZq', 'bZUTkLYNg', 'uCNDeHSBj', 'kOIXdKBFh']
[item[5] for item in str]
Or you could do it much shorter like this,
>>> list(zip(*string))[5]
('Q', 'V', 'M', 'L', 'H', 'K')
>>>
If the string is really big, then using this iterator method would be better.
List comprehensions should work for this. However you have to check whether your string has 5th index or not. Adding a sample code below.
str_list = ['qIQNlQSLi', 'eOEKiVEYj', 'aZADnMCZq', 'bZUTkLYNg', 'uCNDeHSBj', 'kOIXdKBFh']
return [item[5] for item in str_list if len(item) > 5]
There is a lot you could do with List Comprehensions.
you can do :
5thChar=[i[5] for i in str]
which is equivalent to :
5thChar=[]
for i in str:
5thChar.append(i[5])

Categories

Resources