Creating a dictionary from list of lists - list

I have a list of lists in the following format:
[['a'],['1'],['2'],['3'], ['b'],['4'],['5'],['6']]
My desired output is:
[['a', '1'], ['a', '2'], ['a','3'],['b', '4'],['b', '5'],['b', '6']]
or even better would be:
{'a':['1','2','3'], 'b':['4','5','6']}
Essentially, the "number values" are never the same size (think that a could include 1 2 and 3, and b could include 4 5 6 7 and 8, etc)
What would be the easiest way of doing this? Using regex?
Thanks

You can use a for loop and check if the element is a digit or not:
d = {}
for i in lst:
if not i[0].isdigit(): # Check if element is a digit. If not, add a key with the current value of i[0]
d[i[0]] = []
current = i[0]
else:
d[current].append(i[0])
Output:
>>> d
{'a': ['1', '2', '3'], 'b': ['4', '5', '6']}
This is assuming everything in the list is a string

Related

Convert a file into a list with integers

with open('askhsh11.txt', 'r') as f:
raw_list = f.read().split('\n')
for i in range(len(raw_list)):
print raw_list[i].split(',')
for i in range(len(raw_list)):
raw_list[i]=int(i)
print raw_list
the result is :
['1', '2', '3', '4']
['5', '6', '7', '8']
[0, '5,6,7,8']
[0, 1]
but i want the result to be:
['1', '2', '3', '4']
['5', '6', '7', '8']
[1, 2, 3, 4]
[5, 6, 7, 8]
How i convert a list of strings into a integers?
you can use:
result = [int(c) for s in raw_list for c in s.split(',')]
output:
[1, 2, 3, 4, 5, 6, 7, 8]
you can just replace the "'" character to remove them
with open('askhsh11.txt', 'r') as f:
raw_list = f.read().replace("'","").split('\n')
numbers = [int(num) for num in raw_list]
To be honnest . i'm biginner in this langage . but i can tell you that you can use the 2 first output to generate the 2 second output .
what i mean that it's easy to convert
this :
['1', '2', '3', '4']
to this :
[1, 2, 3, 4] .
just use the function that convert the string like '1' to the integer 1 .
sorry i gorget its name but u find it on python support
for evryone who know the function's name .plz make a comment and thank you so much

PYTHON 2.7 - Modifying List of Lists and Re-Assembling Without Mutating

I currently have a list of lists that looks like this:
My_List = [[This, Is, A, Sample, Text, Sentence] [This, too, is, a, sample, text] [finally, so, is, this, one]]
Now what I need to do is "tag" each of these words with one of 3, in this case arbitrary, tags such as "EE", "FF", or "GG" based on which list the word is in and then reassemble them into the same order they came in. My final code would need to look like:
GG_List = [This, Sentence]
FF_List = [Is, A, Text]
EE_List = [Sample]
My_List = [[(This, GG), (Is, FF), (A, FF), (Sample, "EE), (Text, FF), (Sentence, GG)] [*same with this sentence*] [*and this one*]]
I tried this by using for loops to turn each item into a dict but the dicts then got rearranged by their tags which sadly can't happen because of the nature of this thing... the experiment needs everything to stay in the same order because eventually I need to measure the proximity of tags relative to others but only in the same sentence (list).
I thought about doing this with NLTK (which I have little experience with) but it looks like that is much more sophisticated then what I need and the tags aren't easily customized by a novice like myself.
I think this could be done by iterating through each of these items, using an if statement as I have to determine what tag they should have, and then making a tuple out of the word and its associated tag so it doesn't shift around within its list.
I've devised this.. but I can't figure out how to rebuild my list-of-lists and keep them in order :(.
for i in My_List: #For each list in the list of lists
for h in i: #For each item in each list
if h in GG_List: # Check for the tag
MyDicts = {"GG":h for h in i} #Make Dict from tag + word
Thank you so much for your help!
Putting the tags in a dictionary would work:
My_List = [['This', 'Is', 'A', 'Sample', 'Text', 'Sentence'],
['This', 'too', 'is', 'a', 'sample', 'text'],
['finally', 'so', 'is', 'this', 'one']]
GG_List = ['This', 'Sentence']
FF_List = ['Is', 'A', 'Text']
EE_List = ['Sample']
zipped = zip((GG_List, FF_List, EE_List), ('GG', 'FF', 'EE'))
tags = {item: tag for tag_list, tag in zipped for item in tag_list}
res = [[(word, tags[word]) for word in entry if word in tags] for entry in My_List]
Now:
>>> res
[[('This', 'GG'),
('Is', 'FF'),
('A', 'FF'),
('Sample', 'EE'),
('Text', 'FF'),
('Sentence', 'GG')],
[('This', 'GG')],
[]]
Dictionary works by key-value pairs. Each key is assigned a value. To search the dictionary, you search the index by the key, e.g.
>>> d = {1:'a', 2:'b', 3:'c'}
>>> d[1]
'a'
In the above case, we always search the dictionary by its keys, i.e. the integers.
In the case that you want to assign the tag/label to each word, you are searching by the key word and finding the "value", i.e. the tag/label, so your dictionary would have to look something like this (assuming that the strings are words and numbers as tag/label):
>>> d = {'a':1, 'b':1, 'c':3}
>>> d['a']
1
>>> sent = 'a b c a b'.split()
>>> sent
['a', 'b', 'c', 'a', 'b']
>>> [d[word] for word in sent]
[1, 1, 3, 1, 1]
This way the order of the tags follows the order of the words when you use a list comprehension to iterate through the words and find the appropriate tags.
So the problem comes when you have the initial dictionary indexed with the wrong way, i.e. key -> labels, value -> words, e.g.:
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> [d[word] for word in sent]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
Then you would have to reverse your dictionary, assuming that all elements in your value lists are unique, you can do this:
>>> from collections import ChainMap
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
>>> d_inv
{'h': 2, 'c': 3, 'a': 1, 'x': 3, 'b': 2, 'd': 1}
But the caveat is that ChainMap is only available in Python3.5 (yet another reason to upgrade your Python ;P). For Python <3.5, solutions, see How do I merge a list of dicts into a single dict?.
So going back to the problem of assigning labels/tags to words, let's say we have these input:
>>> d = {1:['a', 'd'], 2:['b', 'h'], 3:['c', 'x']}
>>> sent = 'a b c a b'.split()
First, we invert the dictionary (assuming that there're one to one mapping for every word and its tag/label:
>>> d_inv = dict(ChainMap(*[{value:key for value in values} for key, values in d.items()]))
Then, we apply the tags to the words through a list comprehension:
>>> [d_inv[word] for word in sent]
[1, 2, 3, 1, 2]
And for multiple sentences:
>>> sentences = ['a b c'.split(), 'h a x'.split()]
>>> [[d_inv[word] for word in sent] for sent in sentences]
[[1, 2, 3], [2, 1, 3]]

how t find second greatest digit in a number ? Python

The Program must take a number and print the second largest digit in the number.
i have tried this ==>
a=raw_input("Please enter the nummber =")
l=list()
for i in a:
l.append(int(i))
great=0
for i in range(0,len(l)):
for j in l:
if l[i]>j:
if l[i]>great:
great=l[i]
print great
for i in range(20):
great-=1
for j in l:
if j==great:
print "Second largest number is ",j
break
But this gave Output
Please enter the nummber =1459
9
Second largest number is 5
Second largest number is 4
Second largest number is 1
this easily accomplished using the build-in function of python map and sorted
once you have the number from raw_input you can do this
>>> a
'1459'
>>> ordnum = sorted(map(int,a),reverse=True)
>>> ordnum[1]
5
>>> ordnum
[9, 5, 4, 1]
>>>
first with map convert every digit to a number, and result is passed to sorted that order them in ascending order by default, but with the reverse key-word argument the orden is reversed, then you get a list with the digit in order so the second elements is the desire digit.
Or as you already do the map part, you can do the sort part directly to the list
>>> l
[1, 4, 5, 9]
>>> l.sort(reverse=True)
>>> l
[9, 5, 4, 1]
>>>
to the same efect.
The problem with your code is that you don't break the first for-loop, only the second, to break both you can do as #cjahangir show, or make that into a function that return when find it like this
def second_great(great,num):
for i in range(10):
great -= 1
for j in num:
if j == great:
return j
and call it as
print great
print "Second largest number is ", second_great(great,l)
as pointed out by #Blckknght, the conversion to int is unneeded in this case because the digit are order correctly when handled individually, also you can use set to remove repetitions in any of the versions.
>>> b="1459934"
>>> sorted(b,reverse=True)
['9', '9', '5', '4', '4', '3', '1']
>>> sorted(set(b),reverse=True)
['9', '5', '4', '3', '1']
>>> sorted(map(int,set(b)),reverse=True)
[9, 5, 4, 3, 1]
>>>
I think this will work:
print great
flag = 0
for i in range(20):
great-=1
for j in l:
if j==great:
print "Second largest number is ",j
flag = 1
break
if flag:
break

Python Pandas replacing nan's in one column conditional on observations in another column

I have the following data frame in python pandas:
current_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [np.nan, 4, 5, np.nan, 8, np.nan]})
What I want to get is:
needed_data = pd.DataFrame({'X': ['3'+'*NY', '3', '2', '2'+'*NY', '1', '7'], 'Y': [4, 4, 5, 5, 8, np.nan]})
So, I want to replace nan's in Y column that correspond to observations in X with "*NY" part, to numbers in Y that correspond to observations in X that have the same numeric part but without "*NY"
This was a bit more annoying to code, basically we can apply a custom function that performs the lookup for you:
In [106]:
# define our function
def func(x):
# test to see if the asterisk is present
if x.find('*') > 0:
# perform a lookup on a slice of the passed in string
return(current_data.loc[current_data.X==x[0:x.find('*')],'Y'].values.max())
# using loc assign to column 'Y' where it is null the returned calculation of the apply
current_data.loc[current_data.Y.isnull(),'Y'] = current_data[current_data.Y.isnull()]['X'].apply(func)
current_data
Out[106]:
X Y
0 3*NY 4
1 3 4
2 2 5
3 2*NY 5
4 1 8
5 7 NaN

how to unpack a nested list in Python

How to unpack a list, which is nested inside another list.
Practically to transform this:
l=['aa','bb',['ccc','ddd'],'ee']
to
l=['aa','bb','ccc','ddd','ee']
See this thread and e. g. the solution of elqott
>>> from compiler.ast import flatten
>>> l = ['1','2',['3','4'],'5']
>>> flatten(l)
Following your edit
['1', '2', '3', '4', '5']
>>> l = ['aa','bb',['ccc','ddd'],'ee']
>>> flatten(l)
['aa', 'bb', 'ccc', 'ddd', 'ee']