access elements in list of lists
I am not sure if you are using the best data structure for your problem. But if you do this with lists of lists you could do:
from itertools import chain
serialized = []
for syn_words in syn_cluster:
words = [w[0] for w in syn_words]
freqs = list(chain.from_iterable([f[1] for f in syn_words]))
min_max = [min(freqs), max(freqs)]
# or: min_max = list({min(freqs), max(freqs)}) if you want [1] instead of [1, 1]
serialized.append([words, min_max])
serialized
>>> [[['Jack'], [1, 1]],
[['small', 'modest', 'little'], [1, 3]],
[['big', 'large'], [1, 2]]]
Access n:th element of every list in list of lists
when you are enumerating over the list in the for loop:
for word in words:
pass // do something
You have already accessed the element in the list and stored it in word
.
As such, word[0]
in your loop would be accessing the first element in your word tuple which is not what you'd like to do.
Instead, you'd like to access word[2]
in your tuple, so something like this should work:
first_list = [(0, 'Gallery', 'PROPN', 'nsubj'), (1, 'unveils', 'VERB', 'root'), (2, 'interactive', 'ADJ', 'amod')]
second_list = [(0, 'A', 'DET' 'det'), (1, 'Christmas' , 'PROPN', 'compound'), (2, 'tree' ,'NOUN', 'nsubjpass')]
def print_word_pos(words):
for word in words:
print(word[2])
print_word_pos(first_list)
print_word_pos(second_list)
Another thing is that you should not be naming your lists as list
since list
is a reserved python keyword and might (will) cause conflict later down the line.
Now if the first two lists were combined, you'd want to loop over each list and then for each word in that list, print out the part of speech.
def print_word_pos(list_of_words):
for words in list_of_words:
for word in words:
print(word[2])
Access an element in a list of lists in python
actually split will return a list
more over you don't require word_list variable
for line in reference:
line_list.append(line[:-1].split("\t\t"))
print line_list[2][0]
Access to an element of List of lists Python
You could do something like the following:
def recursiveAccess(list, elem):
if len(elem) == 1:
return list[elem[0]]
return recursiveAccess(list[elem[0]], elem[1:])
listElement = [ "a" , ["b","c"] , [["d"],["e","f"]] ]
tab = [ [2,1,0] , [0] ] # "e" and "a" position
for e in tab:
print(recursiveAccess(listElement, e))
# e
# a
Due to the fact you don't know in advance the access tree to an element of the list a recursive solution is in place. Notice that you should add some checks to validate that the input are always valid.
How to access a column in a list of lists in python
List comprehensions are your friend when working with lists of lists:
In [111]: alist
Out[111]:
[[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
In [112]: [row[1] for row in alist]
Out[112]: [1, 7, 13, 19]
There's also a handy 'idiom' for transposing a nested list, turning 'columns' into 'rows':
In [113]: tlist = list(zip(*alist))
In [114]: tlist
Out[114]:
[(0, 6, 12, 18),
(1, 7, 13, 19),
(2, 8, 14, 20),
(3, 9, 15, 21),
(4, 10, 16, 22),
(5, 11, 17, 23)]
In [115]: tlist[1]
Out[115]: (1, 7, 13, 19)
Access list of lists by list identifier
If you know that you have exactly two indices, you could just hardcode this as:
>>> l[indexes[0]][indexes[1]]
'F'
If you want to handle an arbitrarily long list of indices for an arbitrarily deep tree you'll need a loop:
>>> def traverse(tree, indices):
... for i in indices:
... tree = tree[i]
... return tree
...
>>> traverse(l, indexes)
'F'
Extract first item of each sublist
Using list comprehension:
>>> lst = [['a','b','c'], [1,2,3], ['x','y','z']]
>>> lst2 = [item[0] for item in lst]
>>> lst2
['a', 1, 'x']
Related Topics
Writing to Existing Workbook Using Xlwt
Efficient Numpy 2D Array Construction from 1D Array
Access Item in a List of Lists
What's the Best Practice Using a Settings File in Python
Integrating MySQL with Python in Windows
Check If String Contains Only Whitespace
How to Display Tooltips in Tkinter
Selenium: Firefoxprofile Exception Can't Load the Profile
Why Do Some Built-In Python Functions Only Have Pass
Tkinter Vanishing Photoimage Issue
Exif Manipulation Library for Python
How to Create an Encrypted Zip File
Pytz Localize VS Datetime Replace
What Are Data Classes and How Are They Different from Common Classes