Converting object into array in python [duplicate] - python

This question already has answers here:
Selecting elements of a Python dictionary greater than a certain value
(3 answers)
Closed 11 months ago.
I need to convert a PHP/JSON "object" (Python dict) into an array for filler words and show filler words with no count that will not be shown. How can I do this?
This is written as an object:
filler_word = {'um': 9, 'uh': 4, 'hmm': 0, 'mhm': 0, 'uh huh': 0}
This is how I want to have it into an array:
filler_word = ['um': 9, 'uh': 4, 'hmm': 0, 'mhm': 0, 'uh huh': 0]
This is how I want to show those filler words with 0 count that will not be shown:
filler_word = ['um': 9, 'uh': 4]

You are bringing PHP thinking to your Python coding. The two are very different. What you have in the first line is a dictionary, which is the equivalent of a PHP array, and is used like a PHP array.
Python lists do not have keys. So, this is a list:
mylist = [ 9, 4, 0, 0 ]

Use a dict comprehension:
{key: value for key,value in filler_word.items() if value > 0 }
{'um': 9, 'uh': 4}

Related

How do I find the number of items in a duplicate within a list in python? [duplicate]

This question already has answers here:
python count duplicate in list
(7 answers)
Closed 1 year ago.
What I have already is a function that returns which numbers are duplicates like this:
X = [1, 1, 1, 2, 2]
def Dupe(l):
return set([x for x in l if l.count(x) > 1])
print(Dupe(X))
which will return {1, 2}.
What I am looking to do is have a function like this:
X = [1, 1, 1, 2, 2]
def DupeL(l):
(CODE HERE)
print(DupeL(X))
and that would return {3, 2} because there are three ones and two twos. Any ideas?
You were going correct. But I would recommend you to create a dict which shows you the element as well as the number of times it is repeated. Iterate the set and check for occurences for each element of set in the list. Just use .count() method to get the number of occurences in the list. Then add it to an empty dict which shows you the element and it's duplicates. Your code:
x=[1,1,1,2,2]
def Dupe(x):
di={}
c=set([v for v in x if x.count(v)>1])
for i in c:
di[i]=x.count(i)
return di
print(Dupe(x))

covert list of integers to string [duplicate]

This question already has answers here:
Easiest way to join list of ints in Python?
(3 answers)
How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
(9 answers)
Closed 1 year ago.
I want to convert a list to string e.g list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
would be list = '1234567890'
I tried ''.join() but this doesn't work since the list consists of integers
You need to convert each item to string first
''.join(str(x) for x in list)
As you have a list of int values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] (don't use builtin list for naming) you may convert them to str before with
a generator expression
result = ''.join(str(x) for x in values)
map function
result = ''.join(map(str, values))
you can try:
lst=[1,3,2,4,4]
list_string=''
for i in lst:
list_string+=str(i)
print(list_string)
note: you can not use list as variable.

Find-and-replace with python list [duplicate]

This question already has answers here:
Finding and replacing elements in a list
(10 answers)
Closed 2 years ago.
Is there a fancy method for replacing a specific value of a list with another value?
Like a shortcut for this:
>>> l = list(range(10))
>>> replacing = 3
>>> l[l.index(replacing)] = 4
>>> l
[0, 1, 2, 4, 4, 5, 6, 7, 8, 9]
With the example I gave it's easy enough to do via the [l.index()], but when the list reference is a few dots away it starts getting ugly.
It would be so much prettier to do something like this:
>>> some.thing.very.far.away.list = list(range(10))
>>> some.thing.very.far.away.list.replace(3, 4)
Edit:
I forgot to say why.
I want to edit the same list, and only edit one of the values.
I'm actually kind of supprized that lists doesn't have a built-in method like list.replace(old, new[, max), considering that strings do and it seems like python has built-ins for just about everying.
Build a new list with a list comprehension:
new_items = [4 if x==3 else x for x in l]
You can modify the original list in-place if you want, but it doesn't actually save time:
items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index, item in enumerate(items):
if (item ==3 2):
items[index] = 4
you can assign "some.thing.very.far.away.list" to a temporary variable and apply replace function
some.thing.very.far.away.list = temp
something.very.far.away.list = temp.replace(x,y)
This can be a trick:
First define a dictionary with values you want to replace, than use a list comprehension using dictionary get with a default value.
You are passing el both as a key of the dictionary and as default value. If the key is found the corresponding value will be replaced otherwise the default value itself.
>>> l = [0, 1, 2, 4, 4, 5, 6, 7, 8, 9]
>>> rpl = {1: 23, 6: 12}
>>> [rpl.get(el, el) for el in l]
[0, 23, 2, 4, 4, 5, 12, 7, 8, 9]
You can use map method to iterate and replace certain value.
syntax : map(function, iterable, ...)
The returned value from map() (map object) can then be passed to functions like list() (to create a list), set() (to create a set) and so on.
l = list(range(10))
replacing = 3
l = list(map(lambda x: 4 if x == replacing else x, l)) # iterating over list l and replacing certain value using map method and converting map object in to another list.
print(l)
output = [0, 1, 2, 4, 4, 5, 6, 7, 8, 9]
it takes two parameter function and iterable. map() passes each item of the iterable to this function.
where lambda function takes argument x from sequence (l), assign 4 if x is equal to 4 .
to know more about map method Python map()

How can I convert a list of numbers to list of sums of numbers up to current element in python, using a functional approach? [duplicate]

This question already has answers here:
How to find the cumulative sum of numbers in a list?
(25 answers)
Closed 7 years ago.
To clarify, my original list contains the difference between two consecutive values, starting from zero. I want to convert this list to a list of actual values using a functional style, without having to keep a global state during calculation, without for loops, using map, reduce, etc.
my_list = [0, 1, 0, 3, 2, 0, 0, 2, 1]
result = my_function(my_list)
print(result)
[0, 1, 1, 4, 6, 6, 6, 8, 9]
Just use itertools.accumulate.
def my_function(lst):
return [sum(lst[:i]) for i in range(len(lst))]

Efficient way to get the position info of the biggest item in a Python list [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pythonic way to find maximum value and its index in a list?
Say, in list [2, 3, 6, 9, 2, 3, 1, 5, 7], I want to get 3 (position of item 9) as output.
A similar question but for numpy array
My intuition is to build a tuple, and sort the tuple, and get the biggest item's position. I believe there are many better ways....
pos = mylist.index(max(mylist))
This includes all internal python logic - Therefore the best possible implementation.
Like this:
lst = [2, 3, 6, 9, 2, 3, 1, 5, 7]
maxval = max(lst)
maxpos = lst.index(maxval)

Categories