I am scripting in Python2.7.I have an array like k = ((12,12.356,40.365),(458,12.2258,42.69)) and I want to create a list out of the first element of each subset like:
for i in K:
l = k[i][0]
While I have encountered the error "tuple indices must be integers, not tuple".
Would you please let me know how can I come up with a solution?
You are trying to access k tuple item by tuple like k[(12,12.356,40.365)][0], while an item of ordered sequence should be accessed by its position/index (for ex. k[0][0]).
Use list comprehension to get a list out of the first element of each subset:
k = ((12,12.356,40.365),(458,12.2258,42.69))
result = [t[0] for t in k]
print(result)
The outtut:
[12, 458]
Related
alphabet = ['a','b','c']
numbers = [3,2,1]
print alphabet(numbers)
How do I use a list inside of another list? I am trying to get the output "cba" but am running into the following error
TypeError: 'list' object is not callable
You have to index the second list, so you're referencing individual integers rather than the list.
For example, print alphabet[numbers[1]] is the equivalent of print alphabet[2].
Remember that lists are indexed starting at 0, so for alphabet = ['a','b','c'], 'a' is at index 0, 'b' is at index 1, and so on.
To do what you want, you are going to need to use a loop to iterate through the numbers list. Something like:
alphabet = ['a','b','c']
numbers = [2,1,0]
for n in numbers:
print alphabet[n]
Let's say we have an existing empty list, m. I would like to append some elements (let's say three 7's) to m so that m is now [7,7,7]. How to do this using list iterators?
I have the following solution, which is not complete:
m = []
m.append([7 for i in range(3)])
I get m = [[7,7,7]] but not [7,7,7].
I am not looking for post-processing like making m flat such as:
flat_list = [item for sublist in l for item in sublist], because it is an extra step.
You may want to try extend rather than append.
m = []
m.extend([7 for i in range(3)])
Here is a post that may be helpful
I have written the following code snippet:
origilist = [6, 252, 13, 10]
decilist = list(xrange(256))
def yielding(decilist, origilist):
half_origi = (len(origilist)/2)
for n in decilist:
yield origilist[:half_origi] + n + origilist[half_origi:]
for item in yielding(decilist, origilist):
print item
when I run the code I get:
yield origilist[:half_origi] + n + origilist[half_origi:]
TypeError: can only concatenate list (not "int") to list
Is there anyway of joining an integer to another list, in a specific index?
Thanks for the answers
You can only concat lists and n is the item in your list, not list. If you want to concat it with lists, you need to use [n], which is basically creating a list of one item.
In general, you can use + also for strings, but every item you want to concat needs to be string then. So if you have two lists and want to add any item to them, you need to convert that item to list.
Try:
yield origilist[:half_origi] + [n] + origilist[half_origi:]
([n] is a list with one element that can be concatenated to other lists).
I have a list:
alist =
[('A','1','2','DEF'),('B','100,'11','XYZ'),('C','6','9','ABC')]
and I want to sort the list on 2nd and 3rd element, but before I do so I want to convert the type of these elements from string to integer. How can I do so in most pythonic way?
I know I can read the list, convert elements to integer, add all elements to a new list and finally sort:
newList = []
for i in alist:
a,b,c,d, = i
newList.append((a,int(b),int(c),d))
newList.sort(key=itemgetter(1,2))
but what if each tuple in my list has 100 elements (above list had just 4) and I just want to convert a few them (like in above list - b and c) to integer type?
Bade
There's no need to convert them if you only want to sort on them:
alist.sort(key=lambda x: (int(x[1]), int(x[2])))
...
newList = sorted(alist, key=lambda x: (int(x[1]), int(x[2])))
If I inderstand the question, you really want to sort tuples by key, which needs to be converted to integer.
I would do
sorted_list = sorted(alist, key=lambda tup: int(tup[1]))
I need to find a greatest element in the given COLUMN of the list:
myList = [(1,2,0), (3,5,8), (9,1,2)]
Something like this:
max(myList(:,2)) // maximal element in the 2nd column
In this example the answer should be 5.
I wrote the following code, but how can I put the 1st or 2nd column into the input of "max"?
fun findSum(myList:MyList) = max(#1 myList) + max(#2 myList)
fun max [] = 0
| max (x::xs) = foldl Int.max x xs
Thanks.
The simplest way would be to map the appropriate selector onto the list, then using max on the result of that.
max (map #2 myList)
Note, of course, there's no way to do it for tuples of arbitrary size (due to types), and no easy way to convert an integer into a selector. (Apart from creating a function where you manually map each integer onto the appropriate selector.)