Counting frequency of element in a list and joining them - python-3.x

I have a list of list. I want to obtain the frequency of element in the inner list and concatenate that with the element in the outer list.
aa =['a', ['b', 'b', 'b', 'b', 'd', 'd']]
I try to use Counter to get the frequency of occurrence of each element in the inner list as :
from collections import Counter
Counter(aa[1])
It gives:
Counter({'b': 4, 'd': 2})
I want to concatenate this with the outer list element and obtain as follows:
'ab4d2'
I can also iterate through the Counter and get key, value in a list:
y = []
for k, v in surr.items():
y.append(str(k) + str(v))
Output: ['O4', 'Sb2']
There are many answers to get the frequency of occurrence but I did not find any which does this (the problem is joining with outer 'a' in an efficient way) . Could anyone please help me on this. Thanks in advance.

You can use a generator expression with str.join:
aa[0] + ''.join('%s%d' % t for t in Counter(aa[1]).items())
Given aa = ['a', ['b', 'b', 'b', 'b', 'd', 'd']], this returns:
ab4d2

Related

increment a dictionary value by one in Python with querying hash table by one time, like map.merge in Java

For example, given list of str: ['a', 'b', 'a', 'a', 'b'], I want to get the counts of distinct string {'a' : 3, 'b' : 2}.
the naive method is like following:
lst = ['a', 'b', 'a', 'a', 'b']
counts = dict()
for w in lst:
counts[w] = counts.get(w, 0) + 1
However, it needs twice Hash Table queries. In fact, when we firstly called the get method, we have already known the bucket location. In principle, we can modify the bucket value in-place
without searching the bucket location twice. I know in Java we can use map.merge to get this optimization: https://stackoverflow.com/a/33711386/10969942
How to do it in Python?
This is no such method in Python. Whether visible or not, at least under the covers the table lookup will be done twice. But, as the answer you linked to said about Java, nobody much cares - hash table lookup is typically fast, and since you just looked up a key all the info to look it up again is likely sitting in L1 cache.
Two ways of spelling your task that are more idiomatic, but despite that the double-lookup isn't directly visible in either, it still occurs under covers:
>>> lst = ['a', 'b', 'a', 'a', 'b']
>>> from collections import defaultdict
>>> counts = defaultdict(int) # default value is int(); i.e., 0
>>> for w in lst:
... counts[w] += 1
>>> counts
defaultdict(<class 'int'>, {'a': 3, 'b': 2})
and
>>> from collections import Counter
>>> Counter(lst)
Counter({'a': 3, 'b': 2})

python beginner - repeating value of a list for a couple of times

Trying to print the value in y for the number of times that shows in x.
How should I modify the following syntax to achieve the expected output?
x = 5
y = ['a', 'b']
z = []
for num in list(range(x)):
for idx, num1 in enumerate(y):
z.append(num1)
Output based on above:
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
Expected output:
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b']
First, you don't need the list() on range, it's already iterable. Also, you're not using the index, so don't bother with enumerate.
The problem is that you are repeating 5 times going through y and appending it z list. You need to do the opposite:
for num1 in y:
for num in range(x):
z.append(num1)
I also suggest to try a development environment that allows you to debug your program. Use it to get step by step into your code and understand exactly what the computer is doing when executing your code.
You can do it in a single pass.
z=[chr for chr in y for i in range(x)]

Fetching value from dataframe with certain condition

I have a dataframe which is containing 3 columns (['A','B','C]) and 3 rows in it.
We are using a for loop to fetch value(storing into variable) from above dataframe based upon certain condition from column B.
Further we are using list to store value present in variable.
Here question is upon checking list value, we are getting variable value, its type.
I'm not sure why it is happening. As list should contain only variable value only.
Please can anyone help us to get ideal solution for same.
Thanks,
Bhuwan
dataframe: columns-A,B,C rows value- a to i :df = ([a,b,c][d,b,f][g,b,i]).
list_1=[]
for i in range(0,9):
variable_1=df['A'][df.B == 'b']
list_1.append(variable_1)
print(list_1):
Ideal output: ['a','d','g']
while we are getting output as
['a type: object','d type: object','g type: object'].
You can get your ideal output like this:
import pandas as pd
df = pd.DataFrame({'A': ['a', 'd', 'g'], 'B': ['b', 'b', 'b'], 'C': ['c', 'f', 'i']})
list_1 = list(df[df['B'] == 'b']['A'].values) # <- this line
print(list_1)
> ['a', 'd', 'g']
You just need:
1) to filter your dataframe by column "B" df[df['B'] == 'b']
2) and only then take values of the resulted column "A", turning them into list

I need to convert a single input string into a dictionary

I need to convert a single input string into a dictionary with its place indices as keys and the letters as values in python. But I'm stuck here. Any help will be appreciated.
r = list("abcdef")
print (r)
for index,char in enumerate(r,0):
indd = str(index)
print(indd)
abc = indd.split(",")
list2 = list(abc)
d = dict(zip(list2,r))
print(d)
This is one approach using range.
Demo:
r = list("abcdef")
d = {}
for i in range(len(r)):
d[i] = r[i]
print(d)
Output:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
Or a simpler approach using dict().
r = list("abcdef")
d = dict(zip(range(len(r)), r))
print(d)
You have a string abcdef.
First make a list of tuples having first element of tuple as place index and second element of the tuple as the letter at that index. This can be done in this way:
tuples = list(enumerate("abcdef"))
Now, using the dictionary constructor, convert this list of tuples to a dictionary as given below:
dict(tuples)
Demo:
>>> dict(list(enumerate("abcdef")))
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}

Function doesn't change value, when applied to nested list

I have a function which iterates over list of lists. If it finds the value, which is a list itself, it should create a string from this value and insert it instead of the original one:
def lst_to_str(lst):
for x in lst:
for y in x:
i = 0
if type(y) == list:
x[i] = ",".join(y)
i +=1
return lst
The problem is, when I apply this function to pd.DataFrame column
df['pdns'] = df['pdns'].apply(lambda x: lst_to_str(x))
It returns me the original nested list:
[['a', 'b', 'c', 'd'], ['a1', 'b1', 'd1', 'c1'],['a2', 'b2', 'c2', ['d2_1', 'd2_2']]]
Instead of:
[['a', 'b', 'c', 'd'], ['a1', 'b1', 'd1', 'c1'],['a2', 'b2', 'c2', 'd2_1, d2_2']]
Your code is wrong. In your function definition, you're not making any change to 1st and in the end you return 1st. You're checking some condition and then you change the value of your counter (x). Correct this problem and try again

Resources