Finding if a string exist in list within a list - python

I am trying to find if a string Date is present in a list of items. If Date is not present i want to get a null list.
Code
data = [['Organizations', 'Name', 'San Franciso', 11, 32],
['CreativeTeamRoles', 'Description', 'Music Director', 945, 959],
['Persons', 'FullName', 'Salonen', 5761, 5778],
['CreativeTeamRoles', 'Description', 'Conductor', 7322, 7331],
['SoloistRoles', 'Description', 'Piano', 7627, 7632],
['Performances', 'Starttime', '2:00PM', 8062, 8068],
['Performances', 'Date', '2021-05-07', 8247, 8252],
['Performances', 'Endtime', '7:30PM', 8262, 8268]]
output_list = [item for items in data for item in items if 'Date' in item]
Since it has both strings and integers i am getting an error
TypeError: argument of type 'int' is not iterable

try this:
[d for d in data if 'Date' in d]

As from the question,
It seems like you want the Boolean value of the presence of a given string inside a nested list, you can try like this, which returns only True and False
print(any([True for i in data if 'Data' in i else False]))
If you want the list that contains the given string, then -
print([*i for i in data if 'Data' in i])
tell me if this is okay for you...

Related

How to update values in Python's dictionary?

My dictionary looks like below, and I am following this link to update the values in "Column_Type" key. Bascially, I would like to replace values "String" with "VARCHAR(256)", DATE with "NUMBER (4,0)", Int with "NUMBER" and Numeric with "Number". Whenever I run below code, my values are not getting updated to my dictionary.My desired output for updated dictionary is as below
Please note: The location of column_types might vary as well. For ex: Column_type[String] currently is at position 1, but It might be at position 3 later on .
{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['String', 'Numeric', 'Date', 'Int']}
Code:
for key1, key2 in my_dict.items():
if key2== 'String':
my_dict[key2] = "VARCHAR(256)"
print(my_dict)
Desired Output:
{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['VARCHAR(256)', 'NUMBER', 'NUMBER(4,0)', 'NUMBER']}
In your example, your keys are "Column_Name" and Column_Type". There is no key named "String" in your dict. Both values in your dict are of type list so neither are equal to the string String either.
What you want is to replace a specific value in a list.
Try like this:
for index, value in enumerate(my_dict["Column_Type"]):
if value == "String":
my_dict["Column_Type"][index] = "VARCHAR(256)"
This replaces the value in the list, not the dict. That is what you want.
If you need to replace multiple values you can use a dict, like #Jeremy suggested:
type_strs = {
'String': 'VARCHAR(256)',
'Numeric': 'NUMBER',
'Date': 'NUMBER(4,0)',
'Int': 'NUMBER'
}
for index, value in enumerate(my_dict["Column_Type"]):
my_dict["Column_Type"][index] = type_strs.get(value, value)
Here, the .get() function on a dict returns the value corresponding to the key given by the first argument, or the second argument if no such key exists.
type_strs = {
'String': 'VARCHAR(256)',
'Numeric': 'NUMBER',
'Date': 'NUMBER(4,0)',
'Int': 'NUMBER'
}
my_dict['Column_Type'] = [type_strs[t] for t in my_dict['Column_Type']]
I would recommend a dictionary instead of if statements for translating the type strings
Your are in this line comparing a list with an element of this list if key2== 'String':
key2 when you are traveling the variable contains the next ['String', 'Numeric', 'Date', 'Int'], so you will need to join to this value of the array for compare. You can do it with a for cycle
The program is the next:
my_dict={'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['String', 'Numeric', 'Date', 'Int']}
# We create this variable to save the position of the element
position=0
# We travel to the dictionary
for i in my_dict['Column_Type']:
# If the variable is equal to the string
if i == 'String':
# We assign the new information to the variable
my_dict['Column_Type'][position]="VARCHAR(256)"
#And add one to the position
position+=1
print(my_dict)
Output
{'Column_name': ['Name', 'Salary', 'Date', 'Phone'], 'Column_Type': ['VARCHAR(256)', 'Numeric', 'Date', 'Int']}
You can use list.update(val1, val2)
example:
# Dictionary of strings to ints
word_freq = {
"Hello": 56,
"at": 23,
"test": 43,
"this": 43
}
# Adding a new key value pair
word_freq.update({'before': 23})
print(word_freq)

Remove Duplicate 3 value Tuples from list of tuples with similar elements

I have a list of tuples where each tuple has 3 elements within it:
slices = [('location', 'region', 'sub_region'),
('location', 'sub_region', ' job_level'),
('sub_region', 'region', 'location')]
In the above example, the first tuple and the last tuple would be considered duplicates, because the elements within are the same (location, region, sub_region). I'd want to keep only one of them so that my desired output would become:
[('location', 'region', 'sub_region'),
('location', 'sub_region', ' job_level')]
I tried to do this with a list comprehension, but my output ends up being an empty list:
new_slices = [(x, y, z) for x, y, z in slices if (z, x, y) not in slices]
Current Output:
new_slices = []
Any thoughts on how I might be able to accomplish this?
slices = [('location', 'region', 'sub_region'),
('location', 'sub_region', ' job_level'),
('sub_region', 'region', 'location')]
set(tuple(sorted(s)) for s in slices)
Output- {(' job_level', 'location', 'sub_region'),
('location', 'region', 'sub_region')}
You can convert this to list again, if you want list type
Note, you mentioned the first and last tuples are some. Actually they are not - because "location" and " location" are not same
If changing the order of elements doesn't matter for you can do this without sorting.
(with sorting order is O((n^2)log(n)) but without sorting order is O(n))
you can use set then convert to tuple then get as set like below:
>>> set(tuple(set(slc)) for slc in slices)
{('location', 'sub_region', ' job_level'),
('region', 'location', 'sub_region')}
>>> list(set(tuple(set(slc)) for slc in slices))
[('region', 'location', 'sub_region'),
('location', 'sub_region', ' job_level')]

How to iterate through this nested dictionary within a list using for loop

I have a list of nested dictionaries that I want to get specific values and put into a dictionary like this:
vid = [{'a':{'display':'axe', 'desc':'red'}, 'b':{'confidence':'good'}},
{'a':{'display':'book', 'desc':'blue'}, 'b':{'confidence':'poor'}},
{'a':{'display':'apple', 'desc':'green'}, 'b':{'confidence':'good'}}
]
I saw previous questions similar to this, but I still can't get the values such as 'axe' and 'red'. I would like the new dict to have a 'Description', 'Confidence' and other columns with the values from the nested dict.
I have tried this for loop:
new_dict = {}
for x in range(len(vid)):
for y in vid[x]['a']:
desc = y['desc']
new_dict['Description'] = desc
I got many errors but mostly this error:
TypeError: string indices must be integers
Can someone please help solve how to get the values from the nested dictionary?
You don't need to iterate through the keys in the dictionary (the inner for-loop), just access the value you want.
vid = [{'a':{'display':'axe', 'desc':'red'}, 'b':{'confidence':'good'} },
{'a':{'display':'book', 'desc':'blue'}, 'b':{'confidence':'poor'}},
{'a':{'display':'apple', 'desc':'green'}, 'b':{'confidence':'good'}}
]
new_dict = {}
list_of_dicts = []
for x in range(len(vid)):
desc = vid[x]['a']['desc']
list_of_dicts.append({'desc': desc})
I have found a temporary solution for this. I decided to use the pandas dataframe instead.
df = pd.DataFrame(columns = ['Desc'])
for x in range(len(vid)):
desc = vid[x]['a']['desc']
df.loc[len(df)] = [desc]
so you want to write this to csv later so pandas will help you a lot for this problem using pandas you can get the desc by
import pandas as pd
new_dict = {}
df = pd.DataFrame(vid)
for index, row in df.iterrows() :
new_dict['description'] = row['a']['desc']
a b
0 {'display': 'axe', 'desc': 'red'} {'confidence': 'good'}
1 {'display': 'book', 'desc': 'blue'} {'confidence': 'poor'}
2 {'display': 'apple', 'desc': 'green'} {'confidence': 'good'}
this is how dataframe looks like a b are column of the dataframe and your nested dicts are rows of dataframe
Try using this list comprehension:
d = [{'Description': i['a']['desc'], 'Confidence': i['b']['confidence']} for i in vid]
print(d)

For loop that appends rows to dataframe, starting from a list output

I've got an output from an API call as a list:
out = client.phrase_this(phrase='ciao', database='it')
out
[{'Keyword': 'ciao',
'Search Volume': '673000',
'CPC': '0.05',
'Competition': '0',
'Number of Results': '205000000'}]
type(out)
list
I'd like to to create a dataframe and loop-append to that dataframe a new row, starting the API output from multiple keywords.
index = ['ciao', 'google', 'microsoft']
columns = ['Keyword', 'Search Volume', 'CPC', 'Competition', 'Number of Results']
df = pd.DataFrame(index=index, columns=columns)
For loop that is not working:
for keyword in index:
df.loc[keyword] = client.phrase_this(phrase=index, database='it')
Thanks!
The reason this is not working is because you are trying to assign a dictionary inside of a list to the data frame row, rather than just a list.
You are receiving a list containing a dictionary. If you only want to use the first entry of this list the following solution should work:
for keyword in index:
df.loc[keyword] = client.phrase_this(phrase=keyword, database='it')[0].values()
[0] gets the first entry of the list.
values() returns a list of all the values in the dictionary. https://www.tutorialspoint.com/python/dictionary_values.htm
for keyword in index:
df.loc[keyword] = client.phrase_this(phrase=keyword, database='it')
This passes the keyword to the phrase_this function, instead of the entire index list.
Thanks for the answers, I found a workaround:
index = ['ciao', 'google', 'microsoft']
columns = ['Keyword', 'Search Volume', 'CPC', 'Competition', 'Number of Results']
out = []
for query in index:
out.append(client.phrase_this(phrase=query, database='it')[0].values())
out
[dict_values(['ciao', '673000', '0.05', '0', '205000000']),
dict_values(['google', '24900000', '0.66', '0', '13020000000']),
dict_values(['microsoft', '110000', '0.12', '0.06', '77'])]
df = pd.DataFrame(out, columns=columns).set_index('Keyword')

Reading and manipulating JSON elements on Python

I'm a python newbie and I need to read and manipulate elements from a json file, but I keep getting errors and I have no idea how to move forward. This is my code:
import json
with open('file.txt', 'r') as json_data:
d = json.load(json_data)
An example of the dataset:
[
{
'id': 1,
'name': 'a',
'city': 'Paris'
},
{
'id': 2,
'name': 'b',
'city': 'Mons'
},
{
'id': 3,
'name': 'c.',
'city': 'Leuven'
}
]
When I try to get only id or name, I get this error:
city = d['city']
TypeError Traceback (most recent call last)
in ()
----> 1 city = d['city']
TypeError: list indices must be integers or slices, not str
Then I tried this:
city = d[:]['city']
TypeError Traceback (most recent call last)
in ()
----> 1 city = d[:]['city']
TypeError: list indices must be integers or slices, not str
Any ideas? Thanks!
You more likely don't want to know the array index of the element you are looking for.
With some pythonic flavor, you can create tuples with a list comprehension like this:
arr = [(dict['id'], dict['city']) for dict in d]
The output would be
[(1, 'Paris'),
(2, 'Mons'),
(3, 'Leuven')]
Then, you have the possibility to get only specific items in your tuples if needed.
Ex:
arr = [(dict['id'], dict['city']) for dict in d if 's' in dict['city']]
which would return id and name for every entry that contain 's' in the city property.
As this is a dictionary within a list you must provide a list index before calling value by the key. This should work:
dict = d[0]
city = dict['city']
Or you can simply use:
city = d[0]['city']
You can write a loop to go through each object
final=[]
for obj in d:
final.append(obj['city'])
Or you can try using this
final = [obj['city'] for obj in d]
Or if you only need the first value then
print(d[0]['city'])
Output
'Paris'
Since your data is a list of dictionaries, you'll have to use the index value to get the data in the dictionary

Categories