list

Modify a large list without any loops in python

心已入冬 提交于 2021-02-08 13:13:30
问题 My list is: a=[1,2,3,4] Now I want my list to be: a=[-1,-2,-3,-4] How can I change my list this way without using any loops? Update: this may be a large list, on the order of 10000 elements. 回答1: Use Python's map functionality a[:] = map(lambda x: -x, a) Here's the description of the map function from the above link: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many

How to split a compound word split by hyphen into two individual words

浪尽此生 提交于 2021-02-08 13:11:03
问题 I have the following list list1= ['Dodd-Frank', 'insurance', 'regulation'] I used the following to remove the hyphen new1 =[j.replace('-', ' ') for j in list1] The result I got new1= ['Dodd Frank', 'insurance', 'regulation'] The result that Ideally want is new1= ['Dodd', 'Frank', 'insurance', 'regulation'] How can I accomplish this in the most pythonic (efficient way) 回答1: list1 = ['Dodd-Frank', 'insurance', 'regulation'] new1 = '-'.join(list1).split('-') print(new1) Prints: ['Dodd', 'Frank',

How to split a compound word split by hyphen into two individual words

你离开我真会死。 提交于 2021-02-08 13:10:23
问题 I have the following list list1= ['Dodd-Frank', 'insurance', 'regulation'] I used the following to remove the hyphen new1 =[j.replace('-', ' ') for j in list1] The result I got new1= ['Dodd Frank', 'insurance', 'regulation'] The result that Ideally want is new1= ['Dodd', 'Frank', 'insurance', 'regulation'] How can I accomplish this in the most pythonic (efficient way) 回答1: list1 = ['Dodd-Frank', 'insurance', 'regulation'] new1 = '-'.join(list1).split('-') print(new1) Prints: ['Dodd', 'Frank',

Difference between list and tuple (minus immutability) in Python?

坚强是说给别人听的谎言 提交于 2021-02-08 13:09:59
问题 I have known for a while that the primary difference between lists and tuples in Python is that lists are mutable and tuples are not. Beyond that and the different methods available to them, I know very little about lists and tuples. Is there any other difference between them? Are there any advantages/disadvantages (aside from immutability) in using a tuple over a list in Python 3? Does one have a faster access time, or have a smaller memory size, or contain more methods, than the other? Are

Difference between list and tuple (minus immutability) in Python?

非 Y 不嫁゛ 提交于 2021-02-08 13:08:15
问题 I have known for a while that the primary difference between lists and tuples in Python is that lists are mutable and tuples are not. Beyond that and the different methods available to them, I know very little about lists and tuples. Is there any other difference between them? Are there any advantages/disadvantages (aside from immutability) in using a tuple over a list in Python 3? Does one have a faster access time, or have a smaller memory size, or contain more methods, than the other? Are

How to split a compound word split by hyphen into two individual words

痞子三分冷 提交于 2021-02-08 13:07:02
问题 I have the following list list1= ['Dodd-Frank', 'insurance', 'regulation'] I used the following to remove the hyphen new1 =[j.replace('-', ' ') for j in list1] The result I got new1= ['Dodd Frank', 'insurance', 'regulation'] The result that Ideally want is new1= ['Dodd', 'Frank', 'insurance', 'regulation'] How can I accomplish this in the most pythonic (efficient way) 回答1: list1 = ['Dodd-Frank', 'insurance', 'regulation'] new1 = '-'.join(list1).split('-') print(new1) Prints: ['Dodd', 'Frank',

Using zip_longest on unequal lists but repeat the last entry instead of returning None

我的梦境 提交于 2021-02-08 12:15:31
问题 There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But it's not quite I'm after. Instead of returning None , I need it to copy the previous entry on the list. Is this possible? a = ["bottle","water","sky"] b = ["red", "blue"] for i in itertools.izip_longest(a,b): print i #result # ('bottle', 'red') # ('water', 'blue') # ('sky', None) # What I want on the third line is # ('sky', 'blue') 回答1:

Prolog: Find even numbers add them on a list

陌路散爱 提交于 2021-02-08 11:59:32
问题 Write predicate evenNumbers(L1, L2) which is true if the list L1 containing random integers and the list L2 contains even integers from L1 . For example: ?-evenNumbers ([2,1,-3,6,8,9], L2). »Your program returns L2 = [2,6,8]. My code is: evenNumbers([],[]). evenNumbers([H|T],L):- integer(H), 0 is H mod 2, append([H],L,L); evenNumbers(T,L). 回答1: Your code has multiple issues append([H],L,L); will stop recursion and give you a wrong list also your if-then-else statement isn't right .So you

find pattern in matrix made of lists python 3

为君一笑 提交于 2021-02-08 11:55:56
问题 I have a list made of lists, to create a matrix, mostly filled with zeros and some other numbers like this a = [[0,0,0,1,0,0,0,0], [3,0,0,2,1,0,0,0], [3,0,0,0,0,1,0,0], [3,5,0,4,0,0,0,0]] how could I make a function that takes that variable, and find if there is a number repeated 3 times in the diagonals? For example that It returns True because of the 1. I tried to implement a function like this: def find_diagonal(matriz): for x in range(len(matrix)): for for a in range(len(x)): if matriz[x]

Do Python list comprehensions append at each iteration?

主宰稳场 提交于 2021-02-08 11:53:46
问题 I'm trying to understand the performance of list comprehensions in Python, and the trade-offs of using them versus for loops to create lists. One of the known performance costs of using a for loop to append elements to a list is that at each iteration it is O(k) (where k is the length of the list) because the append needs to get to the end of the list to add an additional element. How does this work for list comprehensions? At each iteration does it need to get to the end of the new list to