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]
Related
I want to use list name as nth0 function argument, which should give me nth element,
for example:
nth0(2,[1,2,3],X).
X = 3.
defined list:
L = [1,2,3].
nth0(2,L,X).
I want it to return X = 3. it returns random numbers in L, ̀L = [_10614, _10620, X|_10628].
I'm guessing you are somehow running these two instructions at different times.
If you run them as a joint expression:
L = [1,2,3], nth0(2,L,X).
You should get the right answer.
n = int(input())
arr = map(int, input().split())
print(arr) #error here
I have map function that splits out list items by comma.
I want to print out arr that contains list.
Then I have to find second highest number from the list.
Can you help?
n = int(input())
arr = list(map(int, input().split()))
print(arr)
define arr as a list.
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]
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]))